-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
97 lines (85 loc) · 2.74 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
export async function loadSiteConf (config) {
const r = await axios(config.dataUrl + 'config.yaml')
return jsyaml.load(r.data)
}
const isVector = (url) => url.match(/.*.svg$/)
export function mediaUrl (siteconf, media, params) {
const murl = media.match(/^https?:\/\//) ? media : `${siteconf.cdn}/${media}`
if (isVector(murl) || (!params && !murl.match(/^https?:\/\//))) {
// je to vektor, nebo nechci modifier
return murl
}
return `${siteconf.cdnapi}/resize/?url=${encodeURIComponent(murl)}&${params}`
}
const KEY = window.location.hostname + '_BBB_token'
const USERKEY = window.location.hostname + '_BBB_user'
export function getMethods(siteconf, store) {
function addToast (message, type = 'success') {
this.$data.toast = message
setTimeout(() => {
this.$data.toast = null
}, 3000)
}
async function setToken(newToken) {
localStorage.setItem(KEY, newToken)
const user = await makeRequest('get', siteconf.profileURL, { withCredentials: true })
localStorage.setItem(USERKEY, JSON.stringify(user))
store.commit('profile', user)
siteconf.user = user
}
function makeRequest(method, url, opts = {}) {
const cfg = { method }
const token = localStorage.getItem(KEY)
token && opts.withCredentials && Object.assign(cfg, {
headers: { Authorization: `Bearer ${token}` }
})
opts.data
&& Object.assign(cfg, { body: JSON.stringify(opts.data) })
&& Object.assign(cfg.headers, { 'Content-Type':'application/json' })
return fetch(url, cfg)
.then(res => {
if (res.status === 401 && localStorage.getItem(KEY) !== token) {
return doLogout()
}
const ctype = res.headers.get('Content-Type')
return ctype && ctype.indexOf('json') >= 0 ? res.json() : res.text()
})
}
function login () {
window.location.href = siteconf.api + '/nia/login'
}
async function logout () {
const url = siteconf.api + '/nia/logout'
const logoutURL = await makeRequest('get', url, { withCredentials: true })
doLogout()
window.location.href = logoutURL
}
function doLogout () {
store.commit('profile', null)
siteconf.user = null
localStorage.removeItem(KEY)
localStorage.removeItem(USERKEY)
}
async function initUser () {
const userS = localStorage.getItem(USERKEY)
if (!userS) return
try {
const user = JSON.parse(userS)
const now = new Date()
const exp = new Date(user.exp * 1000)
if (now > exp) return doLogout()
store.commit('profile', user)
siteconf.user = user
} catch (_) {
doLogout()
}
}
return {
request: makeRequest,
login, logout,
setToken,
initUser,
addToast,
mediaUrl: (media, params) => mediaUrl(siteconf, media, params)
}
}