-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #247 from kinde-oss/fix/refresh-token
Fix/refresh token
- Loading branch information
Showing
15 changed files
with
198 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,44 @@ | ||
import {config} from '../config'; | ||
import {sessionManager} from '../session/sessionManager'; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { validateToken } from './validateToken'; | ||
import {NextApiRequest, NextApiResponse} from 'next'; | ||
import {kindeClient} from '../session/kindeServerClient'; | ||
import {validateToken} from './validateToken'; | ||
|
||
export const getAccessToken = async(req: NextApiRequest, res: NextApiResponse) => { | ||
try { | ||
const session = await sessionManager(req, res); | ||
const token = await session.getSessionItem('access_token'); | ||
export const getAccessToken = async ( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) => { | ||
try { | ||
const session = await sessionManager(req, res); | ||
const token = await session.getSessionItem('access_token'); | ||
|
||
if (!token || typeof token !== 'string') { | ||
if (config.isDebugMode) { | ||
console.error('getAccessToken: invalid token or token is missing'); | ||
} | ||
return null; | ||
} | ||
if (!token || typeof token !== 'string') { | ||
if (config.isDebugMode) { | ||
console.error('getAccessToken: invalid token or token is missing'); | ||
} | ||
return null; | ||
} | ||
|
||
const isTokenValid = await validateToken({ | ||
token | ||
}); | ||
const isTokenValid = await validateToken({ | ||
token | ||
}); | ||
|
||
if (!isTokenValid) { | ||
if (config.isDebugMode) { | ||
console.error('getAccessToken: invalid token'); | ||
} | ||
return null; | ||
} | ||
if (!isTokenValid) { | ||
// look for refresh token | ||
if (await kindeClient.refreshTokens(session)) { | ||
return await session.getSessionItem('access_token'); | ||
} | ||
|
||
return token | ||
} catch (error) { | ||
if (config.isDebugMode) { | ||
console.error('getAccessToken', error); | ||
} | ||
return null; | ||
if (config.isDebugMode) { | ||
console.error('getAccessToken: invalid token'); | ||
} | ||
return null; | ||
} | ||
return token; | ||
} catch (error) { | ||
if (config.isDebugMode) { | ||
console.error('getAccessToken', error); | ||
} | ||
} | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,56 @@ | ||
import {config} from '../config'; | ||
import {sessionManager} from '../session/sessionManager'; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { validateToken } from './validateToken'; | ||
import {NextApiRequest, NextApiResponse} from 'next'; | ||
import {validateToken} from './validateToken'; | ||
import {kindeClient} from '../session/kindeServerClient'; | ||
|
||
export const getIdToken = async(req: NextApiRequest, res: NextApiResponse) => { | ||
try { | ||
const session = await sessionManager(req, res); | ||
const token = await session.getSessionItem('id_token'); | ||
export const getIdToken = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const tokenKey = 'id_token'; | ||
try { | ||
const session = await sessionManager(req, res); | ||
const token = await session.getSessionItem(tokenKey); | ||
|
||
if (!token || typeof token !== 'string') { | ||
if (config.isDebugMode) { | ||
console.error('getIdToken: invalid token or token is missing'); | ||
} | ||
return null; | ||
} | ||
if (!token || typeof token !== 'string') { | ||
if (config.isDebugMode) { | ||
console.error('getIdToken: invalid token or token is missing'); | ||
} | ||
return null; | ||
} | ||
|
||
const isTokenValid = await validateToken({ | ||
token | ||
}); | ||
const isTokenValid = await validateToken({ | ||
token | ||
}); | ||
|
||
if (!isTokenValid) { | ||
if (config.isDebugMode) { | ||
console.error('getIdToken: invalid token'); | ||
} | ||
return null; | ||
if (!isTokenValid) { | ||
try { | ||
const refreshSuccess = await kindeClient.refreshTokens(session); | ||
if (refreshSuccess) { | ||
const newToken = await session.getSessionItem(tokenKey); | ||
const isNewTokenValid = await validateToken({token: newToken as string}); | ||
if (isNewTokenValid) { | ||
return newToken; | ||
} | ||
} | ||
|
||
return token | ||
} catch (error) { | ||
if (config.isDebugMode) { | ||
console.error('getIdToken', error); | ||
console.error('getIdToken: token refresh failed'); | ||
} | ||
} catch (error) { | ||
if (config.isDebugMode) { | ||
console.error('getIdToken: error during token refresh', error); | ||
} | ||
return null; | ||
} | ||
|
||
if (config.isDebugMode) { | ||
console.error('getIdToken: invalid token'); | ||
} | ||
return null; | ||
} | ||
|
||
return token; | ||
} catch (error) { | ||
if (config.isDebugMode) { | ||
console.error('getIdToken', error); | ||
} | ||
} | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {validateToken} from '@kinde/jwt-validator'; | ||
import {config} from '../config'; | ||
import {kindeClient} from '../session/kindeServerClient'; | ||
import {SessionManager} from '@kinde-oss/kinde-typescript-sdk'; | ||
|
||
export const refreshTokens = async ( | ||
session: SessionManager | ||
): Promise<boolean> => { | ||
try { | ||
const refreshResult = await kindeClient.refreshTokens(session); | ||
if (!refreshResult) { | ||
return false; | ||
} | ||
|
||
const token = await session.getSessionItem('access_token'); | ||
|
||
if (token && typeof token === 'string') { | ||
const validationResult = await validateToken({ | ||
token, | ||
domain: config.issuerURL | ||
}); | ||
|
||
return validationResult.valid; | ||
} | ||
return false; | ||
} catch (error) { | ||
if (config.isDebugMode) { | ||
console.error('refreshTokens', error); | ||
} | ||
return false; | ||
} | ||
}; |
Oops, something went wrong.