Skip to content

Commit

Permalink
[feat] 일단 만료됐을때를 대비해서..
Browse files Browse the repository at this point in the history
  • Loading branch information
zestlee1106 committed Oct 8, 2023
1 parent c0b76c0 commit 1dcebfb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
16 changes: 16 additions & 0 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import { signOut } from 'next-auth/react';

const baseURL = process.env.NEXT_PUBLIC_API_HOST;

export const fetchData = async <T>(url: string, options?: RequestInit): Promise<T | null> => {
try {
const response = await fetch(`${baseURL}${url}`, options);

if (response.status === 401) {
try {
const data = await response.json();

if (data.error.includes('expired') || data.error.includes('만료')) {
await signOut();
}
} catch (e) {
throw Error('[401] 에러 응답값이 없습니다');
} finally {
return null;
}
}

if (response.status === 204) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export interface CustomUser {
id: string;
name: string;
token: string;
exp: number;
}

export interface CustomSession extends Session {
Expand Down
14 changes: 12 additions & 2 deletions pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ export default NextAuth({

const jwt = parseJWT(data.accessToken);

const user = { id: jwt.email, email: jwt.email, accessToken: data.accessToken, token: data.accessToken };
const user = {
id: jwt.email,
email: jwt.email,
token: data.accessToken,
exp: jwt.exp,
};

return user;
},
Expand All @@ -51,8 +56,13 @@ export default NextAuth({
if (user) {
// eslint-disable-next-line no-param-reassign
token.user = user;
console.log('%c 🤩🤩🤩 영우의 로그 User: ', 'font-size: x-large; color: #bada55;', '', user);
}
return token;
return {
...token,
// accessTokenExpires: Number((user as CustomUser)?.exp) * 1000,
// accessTokenExpires: Date.now() * 1000,
};
},
},
});

0 comments on commit 1dcebfb

Please sign in to comment.