feat: add security package

This commit is contained in:
Karol Sójko
2022-07-06 11:24:32 +02:00
parent 06fc077f1b
commit d86928f1b4
65 changed files with 710 additions and 39 deletions
@@ -0,0 +1,15 @@
import { sign } from 'jsonwebtoken'
import { TokenEncoderInterface } from './TokenEncoderInterface'
export class TokenEncoder<T> implements TokenEncoderInterface<T> {
constructor(private jwtSecret: string) {}
encodeExpirableToken(data: T, expiresIn: string | number | undefined): string {
return sign(data as Record<string, unknown>, this.jwtSecret, { algorithm: 'HS256', expiresIn })
}
encodeToken(data: T): string {
return sign(data as Record<string, unknown>, this.jwtSecret, { algorithm: 'HS256' })
}
}