Skip to content

Commit

Permalink
[SWM-363] Fix : next auth refresh logic
Browse files Browse the repository at this point in the history
oikkoikk committed Oct 25, 2023
1 parent e0f33c2 commit bce3f40
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Endpoints } from '@/src/api/Endpoints';
import { fetchUserAuthWithCredential } from '@/src/api/members/members';
import {
fetchUserAuthWithCredential,
fetchUserAuthWithRefreshToken
} from '@/src/api/members/members';
import {
ONE_MINUTE_IN_MS,
ONE_SECOND_IN_MS,
@@ -12,27 +15,17 @@ import CredentialProvider from 'next-auth/providers/credentials';

async function refreshAccessToken(token: JWT) {
try {
const headers = getHeaders();
const body = JSON.stringify({
const refreshedToken = await fetchUserAuthWithRefreshToken({
refresh_token: token.session.refresh_token
});

const response = await fetch(`${Endpoints.MEMBERS}/refresh`, {
method: 'POST',
headers,
body
});
const refreshedToken = await response.json();

if (!response.ok) throw refreshedToken;

const newToken = {
...token,
session: {
...token.session,
access_token: refreshedToken.access_token,
expires_at: refreshedToken.expires_at,
refresh_token: refreshedToken.refresh_token
access_token: refreshedToken?.access_token,
expires_at: refreshedToken?.expires_at,
refresh_token: refreshedToken?.refresh_token
}
};
return newToken;
@@ -79,7 +72,7 @@ export const authOptions: AuthOptions = {
if (expireTime - now > 10 * ONE_MINUTE_IN_MS) {
return token;
} else {
return await refreshAccessToken(token);
return refreshAccessToken(token);
}
},
async session({ session, token }) {

0 comments on commit bce3f40

Please sign in to comment.