Created
March 9, 2024 23:21
-
-
Save antonkalik/6df12d7fead95b5f8d9551ab76f8d236 to your computer and use it in GitHub Desktop.
Token Service without check of secret
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
import jsonwebtoken from 'jsonwebtoken'; | |
export class TokenService { | |
private static jwt_secret = process.env.JWT_SECRET!; | |
public static verify<TokenPayload>(token: string): Promise<TokenPayload> { | |
return new Promise((resolve, reject) => { | |
jsonwebtoken.verify(token, TokenService.jwt_secret, (error, decoded) => { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(decoded as TokenPayload); | |
} | |
}); | |
}); | |
} | |
public static sign(payload: string | object | Buffer): Promise<string> { | |
return new Promise((resolve, reject) => { | |
try { | |
resolve(jsonwebtoken.sign(payload, TokenService.jwt_secret)); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment