Skip to content

Commit

Permalink
chore: 로그아웃시 이전 토큰 무효화
Browse files Browse the repository at this point in the history
  • Loading branch information
Xvezda committed Apr 27, 2024
1 parent 94f040e commit 639daac
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions apps/api/src/services/auth/v1/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,25 @@ app.get('/logout', withPrevUrl, async (c) => {
const result = await response.json() as DeleteTokenRespone;
*/

deleteSessionCookies(c);

return c.redirect(c.var.prevUrl);
try {
// access token 갱신 요청으로 이전 토큰을 무효화
const sessionSid = getCookie(c, 'session_sid')!;
const securedToken = await decryptToken(c, sessionSid);
const securedPayload = await verifyToken<SecuredSessionPayload>(c, securedToken);
const { refreshToken } = securedPayload['http:cheda.kr/user'];

const url = new URL('https://nid.naver.com/oauth2.0/token');
url.searchParams.append('grant_type', 'refresh_token');
url.searchParams.append('client_id', c.env.OAUTH_CLIENT_ID_NAVER);
url.searchParams.append('client_secret', c.env.OAUTH_CLIENT_SECRET_NAVER);
url.searchParams.append('refresh_token', refreshToken);

const response = await fetch(url);
await response.json() as RefreshTokenResponse;
} finally {
deleteSessionCookies(c);
return c.redirect(c.var.prevUrl);
}
});

app.get('/login', withPrevUrl, async (c) => {
Expand Down Expand Up @@ -453,13 +469,20 @@ app.get('/callback', async (c) => {
app.get('/me', withSession, async (c) => {
const { user } = c.var.session;

const response = fetch('https://openapi.naver.com/v1/nid/me', {
const response = await fetch('https://openapi.naver.com/v1/nid/me', {
headers: {
'Authorization': `Bearer ${user.accessToken}`,
},
});

const result = await response.then(r => r.json()) as NidMeResponse;
if (!response.ok) {
if (response.status === 401) {
throw new HTTPException(401, { message: 'Unauthorized' });
}
throw new HTTPException(500, { message: 'Internal Server Error' });
}

const result = await response.json() as NidMeResponse;

return c.json({
name: result.response.nickname,
Expand Down

0 comments on commit 639daac

Please sign in to comment.