Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isTokenValid helper for pages router #51

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export declare function KindeProvider({

export declare function handleAuth(): any;

export declare function isTokenValid(): boolean;

export type User = {
id: string;
name: string | null;
Expand Down
14 changes: 2 additions & 12 deletions src/handlers/pageRouter/callback.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jwt_decode from 'jwt-decode';
import {config} from '../../config/index';
import {isTokenValid} from '../../utils/pageRouter/isTokenValid';
import {version} from '../../utils/version';

var cookie = require('cookie');
Expand Down Expand Up @@ -31,18 +31,8 @@ export const callback = async (req, res) => {
}
);
const data = await response.json();
const accessTokenHeader = jwt_decode(data.access_token, {header: true});
const accessTokenPayload = jwt_decode(data.access_token);
let isAudienceValid = true;
if (config.audience)
isAudienceValid = accessTokenPayload.aud == config.audience;

if (
accessTokenPayload.iss == config.issuerURL &&
accessTokenHeader.alg == 'RS256' &&
accessTokenPayload.exp > Math.floor(Date.now() / 1000) &&
isAudienceValid
) {
if (isTokenValid(data)) {
res.setHeader(
'Set-Cookie',
cookie.serialize(`kinde_token`, JSON.stringify(data), {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {KindeProvider, useKindeAuth} from './frontend/index';
export {handleAuth} from './handlers/pageRouter/index';
export {isTokenValid} from './utils/pageRouter/isTokenValid';
export * from './session/pagesRouter/index';
27 changes: 27 additions & 0 deletions src/utils/pageRouter/isTokenValid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import jwt_decode from 'jwt-decode';
import {config} from '../../config/index';

const isTokenValid = (token) => {
const accessToken = (token && token.access_token) || token;
if (!accessToken) return false;

const accessTokenHeader = jwt_decode(accessToken, {header: true});
const accessTokenPayload = jwt_decode(accessToken);
let isAudienceValid = true;
if (config.audience) {
isAudienceValid = accessTokenPayload.aud == config.audience;
}

if (
accessTokenPayload.iss == config.issuerURL &&
accessTokenHeader.alg == 'RS256' &&
accessTokenPayload.exp > Math.floor(Date.now() / 1000) &&
isAudienceValid
) {
return true;
} else {
return false;
}
};

export {isTokenValid};