Skip to content

Commit

Permalink
fix: refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRivers committed Nov 21, 2024
1 parent e19c505 commit 7f82398
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/utils/getAccessToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { validateToken } from "@kinde/jwt-validator";
import {config} from '../config';
import {sessionManager} from '../session/sessionManager';
import { NextApiRequest, NextApiResponse } from "next";
import { kindeClient } from "../session/kindeServerClient";

export const getAccessToken = async(req: NextApiRequest, res: NextApiResponse) => {
try {
Expand All @@ -22,9 +23,20 @@ export const getAccessToken = async(req: NextApiRequest, res: NextApiResponse) =

if (!validationResult.valid) {
// look for refresh token
// use client.refreshToken to get new access token
await kindeClient.refreshTokens (session);

const token = await session.getSessionItem('access_token');

if (token && typeof token === 'string') {
const validationResult = await validateToken({
token,
domain: config.issuerURL
});

return (validationResult.valid) ? token : null;
}

if (config.isDebugMode) {
console.error('getAccessToken: invalid token');
}
Expand Down

0 comments on commit 7f82398

Please sign in to comment.