How to parse opaque token to get user when using socket io #3467
Unanswered
tuandungbrse
asked this question in
Help
Replies: 1 comment
-
Here's a function I use to extract ID from an OAT: /**
* Function to get id of token from token generated at time of login.
* Note that this is not the hash stored in DB, but the token generated when login method is called.
* Token can be accessed from request headers
* @param {string} token Token from request Authorization Header
* @return {string | null} Id of token as string or null if token extraction failed
*/
export const getTokenId = (token?: string | null) => {
if (!token) {
return null;
}
const tokenParts = token.split('.');
if (tokenParts.length !== 2) {
return null;
}
const parsedWordArray = CryptoJS.enc.Base64.parse(tokenParts[0]);
const tokenId: number = +parsedWordArray.toString(CryptoJS.enc.Utf8);
if (!tokenId || Number.isNaN(tokenId)) {
return null;
}
return tokenId;
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm implementing authentication on socket connection, getting token from the client but do not know how to parse an opaque token like parse token with JWT, anyone can help? Much appreciate.
Beta Was this translation helpful? Give feedback.
All reactions