fix(types): decodedToken in secret callback

close #21
This commit is contained in:
divlo 2021-01-28 18:47:38 +01:00
parent 4d73584e4e
commit c1a9213a52

View File

@ -21,7 +21,15 @@ type SocketIOMiddleware = (
next: (err?: ExtendedError) => void next: (err?: ExtendedError) => void
) => void ) => void
type SecretCallback = (decodedToken: null | { [key: string]: any } | string) => Promise<string> interface CompleteDecodedToken {
header: {
alg: Algorithm
[key: string]: any
}
payload: any
}
type SecretCallback = (decodedToken: CompleteDecodedToken) => Promise<string>
export interface AuthorizeOptions { export interface AuthorizeOptions {
secret: string | SecretCallback secret: string | SecretCallback
@ -58,8 +66,8 @@ export const authorize = (options: AuthorizeOptions): SocketIOMiddleware => {
if (typeof secret === 'string') { if (typeof secret === 'string') {
keySecret = secret keySecret = secret
} else { } else {
decodedToken = jwt.decode(encodedToken, { complete: true }) const completeDecodedToken = jwt.decode(encodedToken, { complete: true })
keySecret = await secret(decodedToken) keySecret = await secret(completeDecodedToken as CompleteDecodedToken)
} }
try { try {
decodedToken = jwt.verify(encodedToken, keySecret, { algorithms }) decodedToken = jwt.verify(encodedToken, keySecret, { algorithms })