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

Feature: refresh token, session log 작업 #90

Merged
merged 4 commits into from
Oct 11, 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
21 changes: 15 additions & 6 deletions src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const authOptions: NextAuthOptions = {
provider: 'credential',
});

console.log('authorize', data);

if (data.nickname && data.accessToken) {
const user = {
accessToken: data.accessToken,
Expand All @@ -65,6 +67,7 @@ export const authOptions: NextAuthOptions = {
],
session: {
strategy: 'jwt',
maxAge: 7 * 24 * 60 * 60, // 7 days
},
jwt: {
secret: process.env.NEXT_PUBLIC_SECRET_KEY,
Expand Down Expand Up @@ -106,9 +109,13 @@ export const authOptions: NextAuthOptions = {
// accessToken 만료를 검사합니다.
if (token.accessToken && isTokenExpired(token.accessToken as string)) {
// 만료된 경우 refreshToken으로 새 accessToken을 발급
const newAccessToken = await refreshAccessToken(token.accessToken as string);
if (newAccessToken) {
token.accessToken = newAccessToken; // 새로운 accessToken으로 업데이트
const newToken = await refreshAccessToken(token.accessToken as string);

console.log('newAccessToken -- jwt', newToken);

if (newToken) {
token.accessToken = newToken.accessToken; // 새로운 accessToken으로 업데이트
token.refreshToken = newToken.refreshToken; // 새로운 accessToken으로 업데이트
} else {
// TODO: refresh token 만료시 추가 처리
// refreshToken도 만료되었거나 문제가 있을 경우
Expand All @@ -125,10 +132,12 @@ export const authOptions: NextAuthOptions = {

if (session.accessToken && isTokenExpired(session.accessToken)) {
// 만료된 경우 refreshToken으로 새 accessToken을 발급받습니다.
const newAccessToken = await refreshAccessToken(session.refreshToken as string);
const newToken = await refreshAccessToken(token.accessToken as string);
console.log('newAccessToken -- Session', newToken);

if (newAccessToken) {
session.accessToken = newAccessToken;
if (newToken) {
token.accessToken = newToken.accessToken; // 새로운 accessToken으로 업데이트
token.refreshToken = newToken.refreshToken; // 새로운 refreshToken 업데이트
}
// TODO: refresh token 만료시 추가 처리
// refreshToken도 만료되었거나 문제가 있을 경우
Expand Down
49 changes: 0 additions & 49 deletions src/pages/api/chat/[character_id].tsx

This file was deleted.

35 changes: 0 additions & 35 deletions src/pages/api/users/testapi.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/pages/profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@ import { css } from '@emotion/react';
import BottomNavBar from '@/components/common/bottomNavBar/BottomNavBar';
import SEO from '@/components/common/head/SEO';
import { useSession } from 'next-auth/react';
import { useEffect } from 'react';

const Profile = () => {
const { data: session }: any = useSession();
console.log(session);

useEffect(() => {
if (session?.accessToken) {
fetch('/api/users/testapi', {
headers: {
Authorization: `Bearer ${session.accessToken}`,
},
})
.then((res) => res.json())
.then((data) => {
console.log(data);
});
}
}, [session]);

return (
<>
<SEO title="Profile" />
Expand Down
6 changes: 3 additions & 3 deletions src/utils/api/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import defaultInstance from '@/utils/axiosInstance/defaultInstance';
import clientInstance from '../axiosInstance/clientInstance';
import clientInstance from '@/utils/axiosInstance/clientInstance';

interface Credentials {
email: string, password: string, provider: string
Expand Down Expand Up @@ -63,7 +63,7 @@ export const credentialsSignupAPI = async ({

export const refreshAccessToken = async (refreshToken: string) => {
const result = await defaultInstance.post('members/refreshToken', { refreshToken });
console.log('refresh', result.data.accessToken);
console.log('refresh', result.data);

return result.data.accessToken;
return result.data;
};