Added useful helper functions
This commit is contained in:
@@ -66,18 +66,6 @@ export function AuthProvider({children}) {
|
||||
}
|
||||
}
|
||||
|
||||
const getTokenPayload = () => {
|
||||
if (!token) {
|
||||
console.warn(`Token is ${null}/${undefined}`)
|
||||
return {}
|
||||
}
|
||||
|
||||
const informativePart = token.split('.')[1]
|
||||
const payload = JSON.parse(window.atob(informativePart))
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
// This side effect keeps local storage updated with recent token value,
|
||||
// making sure it can be re-used upon refresh or re-open browser
|
||||
useEffect(() => {
|
||||
@@ -104,7 +92,6 @@ export function AuthProvider({children}) {
|
||||
register,
|
||||
login,
|
||||
logout,
|
||||
getTokenPayload,
|
||||
}}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
|
||||
19
client/src/functions/get-query.js
Normal file
19
client/src/functions/get-query.js
Normal file
@@ -0,0 +1,19 @@
|
||||
export default function getQuery(queryStr = '') {
|
||||
if (queryStr) {
|
||||
const queryObj = {}
|
||||
const queryArr = (
|
||||
queryStr[0] === '?' ? queryStr.substring(1, queryStr.length) : queryStr
|
||||
).split('&')
|
||||
|
||||
queryArr.forEach((str) => {
|
||||
const [key, val] = str.split('=')
|
||||
|
||||
queryObj[key] = val
|
||||
})
|
||||
|
||||
return queryObj
|
||||
} else {
|
||||
console.warn('Query string is not defined')
|
||||
return {}
|
||||
}
|
||||
}
|
||||
11
client/src/functions/get-token-payload.js
Normal file
11
client/src/functions/get-token-payload.js
Normal file
@@ -0,0 +1,11 @@
|
||||
export default function getTokenPayload(token = '') {
|
||||
if (token) {
|
||||
const informativePart = token.split('.')[1]
|
||||
const payload = JSON.parse(window.atob(informativePart))
|
||||
|
||||
return payload
|
||||
} else {
|
||||
console.warn('Token is not defined')
|
||||
return {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user