Done setup template
This commit is contained in:
14
frontend/src/utils/auth.js
Normal file
14
frontend/src/utils/auth.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { STORE_KEY } from './enum'
|
||||
import { Helpers } from './helper'
|
||||
|
||||
export default class UserHelper {
|
||||
#user
|
||||
|
||||
constructor() {
|
||||
this.#user = Helpers.decrypt(localStorage.getItem(STORE_KEY))
|
||||
}
|
||||
|
||||
get isAdmin() {
|
||||
return this.#user?.userInfo?.isAdmin
|
||||
}
|
||||
}
|
5
frontend/src/utils/enum.js
Normal file
5
frontend/src/utils/enum.js
Normal file
@ -0,0 +1,5 @@
|
||||
// const PRODUCTION = import.meta.env.NODE_ENV === 'production'
|
||||
|
||||
export const SECRET_KEY = 'bGV0IGRvIGl0IGZvciBlbmNyeXRo'
|
||||
export const STORE_KEY = 'dXNlciBsb2dpbiBpbmZv'
|
||||
export const LOGIN_KEY = '7fo24CMyIc'
|
50
frontend/src/utils/helper.js
Normal file
50
frontend/src/utils/helper.js
Normal file
@ -0,0 +1,50 @@
|
||||
import { AES, enc } from 'crypto-js'
|
||||
import { LOGIN_KEY, SECRET_KEY, STORE_KEY } from './enum'
|
||||
|
||||
export class Helpers {
|
||||
static setCookie = (cname, cvalue, exdays) => {
|
||||
const d = new Date()
|
||||
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000)
|
||||
let expires = `expires=${d.toUTCString()}`
|
||||
document.cookie = `${cname}=${cvalue};${expires};path=/`
|
||||
}
|
||||
|
||||
static getCookie = (cname) => {
|
||||
let name = cname + '='
|
||||
let ca = document.cookie.split(';')
|
||||
for (let i = 0; i < ca.length; i++) {
|
||||
let c = ca[i]
|
||||
while (c.charAt(0) == ' ') {
|
||||
c = c.substring(1)
|
||||
}
|
||||
if (c.indexOf(name) == 0) {
|
||||
return c.substring(name.length, c.length)
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
static deleteCookie = (cname) => {
|
||||
document.cookie = `${cname}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`
|
||||
}
|
||||
|
||||
static clearStorage = () => {
|
||||
localStorage.removeItem(LOGIN_KEY)
|
||||
localStorage.removeItem(STORE_KEY)
|
||||
}
|
||||
|
||||
static checkTokenExpired = (exp) => {
|
||||
const currentTime = Math.floor(new Date().getTime() / 1000)
|
||||
return exp < currentTime
|
||||
}
|
||||
|
||||
static encrypt = (obj) => {
|
||||
return AES.encrypt(JSON.stringify(obj), SECRET_KEY).toString()
|
||||
}
|
||||
|
||||
static decrypt = (hash, defaultValue = {}) => {
|
||||
return hash
|
||||
? JSON.parse(AES.decrypt(hash, SECRET_KEY).toString(enc.Utf8))
|
||||
: defaultValue
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user