Answer the question
In order to leave comments, you need to log in
What alternative would you recommend?
Good evening!
To simplify working with tokens, I created a class. The class looks like this:
class TokenService {
constructor() {
}
handle(token) {
this.set(token)
this.payload(token)
}
set(token) {
localStorage.setItem("jwtToken", token)
}
get() {
return localStorage.getItem("jwtToken")
}
remove() {
localStorage.removeItem("jwtToken")
}
isValid() {
const token = this.get()
if (token) {
const payload = this.payload(token)
}
}
payload(token) {
return token.split('.')[1];
}
}
export default TokenService
const Token = new TokenService()
Token.handle(sometoken)
Answer the question
In order to leave comments, you need to log in
TokenService.js
class TokenService {
...
}
export default new TokenService()
import TokenService from '/path/TokenService'
Token.set('токен')
console.log(Token.get())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question