chore: move files to frontend
This commit is contained in:
92
frontend/src/utils/auth.js
Normal file
92
frontend/src/utils/auth.js
Normal file
@@ -0,0 +1,92 @@
|
||||
import store from '@/store'
|
||||
import router from '@/router'
|
||||
import { Base64 } from 'js-base64'
|
||||
import { baseURL } from '@/utils/constants'
|
||||
|
||||
export function parseToken (token) {
|
||||
const parts = token.split('.')
|
||||
|
||||
if (parts.length !== 3) {
|
||||
throw new Error('token malformed')
|
||||
}
|
||||
|
||||
const data = JSON.parse(Base64.decode(parts[1]))
|
||||
|
||||
if (Math.round(new Date().getTime() / 1000) > data.exp) {
|
||||
throw new Error('token expired')
|
||||
}
|
||||
|
||||
localStorage.setItem('jwt', token)
|
||||
store.commit('setJWT', token)
|
||||
store.commit('setUser', data.user)
|
||||
}
|
||||
|
||||
export async function validateLogin () {
|
||||
try {
|
||||
if (localStorage.getItem('jwt')) {
|
||||
await renew(localStorage.getItem('jwt'))
|
||||
}
|
||||
} catch (_) {
|
||||
console.warn('Invalid JWT token in storage') // eslint-disable-line
|
||||
}
|
||||
}
|
||||
|
||||
export async function login (username, password, recaptcha) {
|
||||
const data = { username, password, recaptcha }
|
||||
|
||||
const res = await fetch(`${baseURL}/api/login`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
|
||||
const body = await res.text()
|
||||
|
||||
if (res.status === 200) {
|
||||
parseToken(body)
|
||||
} else {
|
||||
throw new Error(body)
|
||||
}
|
||||
}
|
||||
|
||||
export async function renew (jwt) {
|
||||
const res = await fetch(`${baseURL}/api/renew`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Auth': jwt,
|
||||
}
|
||||
})
|
||||
|
||||
const body = await res.text()
|
||||
|
||||
if (res.status === 200) {
|
||||
parseToken(body)
|
||||
} else {
|
||||
throw new Error(body)
|
||||
}
|
||||
}
|
||||
|
||||
export async function signup (username, password) {
|
||||
const data = { username, password }
|
||||
|
||||
const res = await fetch(`${baseURL}/api/signup`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
|
||||
if (res.status !== 200) {
|
||||
throw new Error(res.status)
|
||||
}
|
||||
}
|
||||
|
||||
export function logout () {
|
||||
store.commit('setJWT', '')
|
||||
store.commit('setUser', null)
|
||||
localStorage.setItem('jwt', null)
|
||||
router.push({path: '/login'})
|
||||
}
|
||||
66
frontend/src/utils/buttons.js
Normal file
66
frontend/src/utils/buttons.js
Normal file
@@ -0,0 +1,66 @@
|
||||
function loading (button) {
|
||||
let el = document.querySelector(`#${button}-button > i`)
|
||||
|
||||
if (el === undefined || el === null) {
|
||||
console.log('Error getting button ' + button) // eslint-disable-line
|
||||
return
|
||||
}
|
||||
|
||||
el.dataset.icon = el.innerHTML
|
||||
el.style.opacity = 0
|
||||
|
||||
setTimeout(() => {
|
||||
el.classList.add('spin')
|
||||
el.innerHTML = 'autorenew'
|
||||
el.style.opacity = 1
|
||||
}, 100)
|
||||
}
|
||||
|
||||
function done (button) {
|
||||
let el = document.querySelector(`#${button}-button > i`)
|
||||
|
||||
if (el === undefined || el === null) {
|
||||
console.log('Error getting button ' + button) // eslint-disable-line
|
||||
return
|
||||
}
|
||||
|
||||
el.style.opacity = 0
|
||||
|
||||
setTimeout(() => {
|
||||
el.classList.remove('spin')
|
||||
el.innerHTML = el.dataset.icon
|
||||
el.style.opacity = 1
|
||||
}, 100)
|
||||
}
|
||||
|
||||
function success (button) {
|
||||
let el = document.querySelector(`#${button}-button > i`)
|
||||
|
||||
if (el === undefined || el === null) {
|
||||
console.log('Error getting button ' + button) // eslint-disable-line
|
||||
return
|
||||
}
|
||||
|
||||
el.style.opacity = 0
|
||||
|
||||
setTimeout(() => {
|
||||
el.classList.remove('spin')
|
||||
el.innerHTML = 'done'
|
||||
el.style.opacity = 1
|
||||
|
||||
setTimeout(() => {
|
||||
el.style.opacity = 0
|
||||
|
||||
setTimeout(() => {
|
||||
el.innerHTML = el.dataset.icon
|
||||
el.style.opacity = 1
|
||||
}, 100)
|
||||
}, 500)
|
||||
}, 100)
|
||||
}
|
||||
|
||||
export default {
|
||||
loading,
|
||||
done,
|
||||
success
|
||||
}
|
||||
24
frontend/src/utils/constants.js
Normal file
24
frontend/src/utils/constants.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const name = window.FileBrowser.Name || 'File Browser'
|
||||
const disableExternal = window.FileBrowser.DisableExternal
|
||||
const baseURL = window.FileBrowser.BaseURL
|
||||
const staticURL = window.FileBrowser.StaticURL
|
||||
const recaptcha = window.FileBrowser.ReCaptcha
|
||||
const recaptchaKey = window.FileBrowser.ReCaptchaKey
|
||||
const signup = window.FileBrowser.Signup
|
||||
const version = window.FileBrowser.Version
|
||||
const logoURL = `/${staticURL}/img/logo.svg`
|
||||
const noAuth = window.FileBrowser.NoAuth
|
||||
const loginPage = window.FileBrowser.LoginPage
|
||||
|
||||
export {
|
||||
name,
|
||||
disableExternal,
|
||||
baseURL,
|
||||
logoURL,
|
||||
recaptcha,
|
||||
recaptchaKey,
|
||||
signup,
|
||||
version,
|
||||
noAuth,
|
||||
loginPage
|
||||
}
|
||||
4
frontend/src/utils/cookie.js
Normal file
4
frontend/src/utils/cookie.js
Normal file
@@ -0,0 +1,4 @@
|
||||
export default function (name) {
|
||||
let re = new RegExp('(?:(?:^|.*;\\s*)' + name + '\\s*\\=\\s*([^;]*).*$)|^.*$')
|
||||
return document.cookie.replace(re, '$1')
|
||||
}
|
||||
28
frontend/src/utils/css.js
Normal file
28
frontend/src/utils/css.js
Normal file
@@ -0,0 +1,28 @@
|
||||
export default function getRule (rules) {
|
||||
for (let i = 0; i < rules.length; i++) {
|
||||
rules[i] = rules[i].toLowerCase()
|
||||
}
|
||||
|
||||
let result = null
|
||||
let find = Array.prototype.find
|
||||
|
||||
find.call(document.styleSheets, styleSheet => {
|
||||
result = find.call(styleSheet.cssRules, cssRule => {
|
||||
let found = false
|
||||
|
||||
if (cssRule instanceof window.CSSStyleRule) {
|
||||
for (let i = 0; i < rules.length; i++) {
|
||||
if (cssRule.selectorText.toLowerCase() === rules[i]) {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return found
|
||||
})
|
||||
|
||||
return result != null
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
12
frontend/src/utils/url.js
Normal file
12
frontend/src/utils/url.js
Normal file
@@ -0,0 +1,12 @@
|
||||
function removeLastDir (url) {
|
||||
var arr = url.split('/')
|
||||
if (arr.pop() === '') {
|
||||
arr.pop()
|
||||
}
|
||||
|
||||
return arr.join('/')
|
||||
}
|
||||
|
||||
export default {
|
||||
removeLastDir: removeLastDir
|
||||
}
|
||||
55
frontend/src/utils/vue.js
Normal file
55
frontend/src/utils/vue.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import Vue from 'vue'
|
||||
import Noty from 'noty'
|
||||
import i18n from '@/i18n'
|
||||
import { disableExternal } from '@/utils/constants'
|
||||
|
||||
Vue.config.productionTip = true
|
||||
|
||||
const notyDefault = {
|
||||
type: 'info',
|
||||
layout: 'bottomRight',
|
||||
timeout: 1000,
|
||||
progressBar: true
|
||||
}
|
||||
|
||||
Vue.prototype.$noty = (opts) => {
|
||||
new Noty(Object.assign({}, notyDefault, opts)).show()
|
||||
}
|
||||
|
||||
Vue.prototype.$showSuccess = (message) => {
|
||||
new Noty(Object.assign({}, notyDefault, {
|
||||
text: message,
|
||||
type: 'success'
|
||||
})).show()
|
||||
}
|
||||
|
||||
Vue.prototype.$showError = (error) => {
|
||||
let btns = [
|
||||
Noty.button(i18n.t('buttons.close'), '', function () {
|
||||
n.close()
|
||||
})
|
||||
]
|
||||
|
||||
if (!disableExternal) {
|
||||
btns.unshift(Noty.button(i18n.t('buttons.reportIssue'), '', function () {
|
||||
window.open('https://github.com/filebrowser/filebrowser/issues/new/choose')
|
||||
}))
|
||||
}
|
||||
|
||||
let n = new Noty(Object.assign({}, notyDefault, {
|
||||
text: error.message || error,
|
||||
type: 'error',
|
||||
timeout: null,
|
||||
buttons: btns
|
||||
}))
|
||||
|
||||
n.show()
|
||||
}
|
||||
|
||||
Vue.directive('focus', {
|
||||
inserted: function (el) {
|
||||
el.focus()
|
||||
}
|
||||
})
|
||||
|
||||
export default Vue
|
||||
Reference in New Issue
Block a user