-
Notifications
You must be signed in to change notification settings - Fork 0
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 #216 from sullog-official/fix/auth
fix: 인증 관련 코드 수정, 토큰이 유효하지 않은 경우 대응
- Loading branch information
Showing
10 changed files
with
147 additions
and
122 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { deleteRefreshToken } from '@/shared/utils/auth'; | ||
import { deleteAccessToken, deleteRefreshToken } from '@/shared/utils/auth'; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
deleteAccessToken({ req, res }); | ||
deleteRefreshToken({ req, res }); | ||
res.redirect(307, '/login'); | ||
} |
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,50 @@ | ||
import axios from 'axios'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { | ||
ACCESS_TOKEN_KEY, | ||
NEXT_PUBLIC_API_BASE_URI, | ||
REFRESH_TOKEN_KEY, | ||
} from '@/shared/constants'; | ||
import { | ||
getRefreshToken, | ||
setAccessToken, | ||
setRefreshToken, | ||
} from '@/shared/utils/auth'; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
try { | ||
const refreshToken = getRefreshToken({ req, res }); | ||
|
||
if (!refreshToken) { | ||
throw new Error('refreshToken is undefined'); | ||
} | ||
|
||
const response = await axios({ | ||
baseURL: NEXT_PUBLIC_API_BASE_URI, | ||
url: '/token/refresh', | ||
method: 'get', | ||
headers: { | ||
Authorization: `Bearer ${refreshToken}`, | ||
}, | ||
validateStatus: null, | ||
}); | ||
|
||
const newAccessToken = response.headers[ACCESS_TOKEN_KEY]; | ||
const newRefreshToken = response.headers[REFRESH_TOKEN_KEY]; | ||
|
||
if (!newAccessToken || !newRefreshToken) { | ||
throw new Error('Invalid tokens.'); | ||
} | ||
|
||
setAccessToken(newAccessToken, { req, res }); | ||
setRefreshToken(newRefreshToken, { req, res }); | ||
|
||
res.status(200).json({ result: true }); | ||
} catch (err) { | ||
res.status(200).json({ result: false }); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
import axios from 'axios'; | ||
|
||
export const refreshTokens = async () => { | ||
return await axios<{ result: boolean }>({ | ||
url: '/api/refresh', | ||
method: 'get', | ||
}); | ||
}; |
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
Oops, something went wrong.