J
J
Jedi2019-02-15 17:28:23
JavaScript
Jedi, 2019-02-15 17:28:23

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

I am using this piece of code in my React app. I don't like the fact that every time you have to call the class.
const Token = new TokenService()
Token.handle(sometoken)

What alternative would you recommend so that it is competent?

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Pautov, 2019-02-15
@PHPjedi

TokenService.js

class TokenService {
...
}

export default new TokenService()

some.js
import TokenService from '/path/TokenService'

Token.set('токен')
console.log(Token.get())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question