Skip to content

Commit

Permalink
feat: update logout server route
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenyWas committed Jun 10, 2024
1 parent 872b08c commit f3721d3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
13 changes: 3 additions & 10 deletions server/routes/logout.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { H3Event } from 'h3';

import { userIdentitySchema } from '../schemas';
import { AUTH_PROVIDERS, COOKIE_NAMES } from '~/configs/properties';
import { googleOAuthClient, octokitOAuthApp } from '~/server/services';
import type { UserIdentity } from '~/server/types';
import { cleanAuth } from '~/server/utils';
import { base64ToString } from '~/utils/converters';

const DEFAULT_LOCATION = '/';

const clean = (event: H3Event) => {
setResponseHeader(event, 'Authorization', '');
deleteCookie(event, COOKIE_NAMES.refreshToken);
deleteCookie(event, COOKIE_NAMES.userIdentity);
};

export default defineEventHandler(async (event) => {
const location = getRequestHeader(event, 'Location') ?? DEFAULT_LOCATION;
const identityCookie = getCookie(event, COOKIE_NAMES.userIdentity) ?? '';
Expand All @@ -22,7 +15,7 @@ export default defineEventHandler(async (event) => {
try {
identity = userIdentitySchema.parse(JSON.parse(base64ToString(identityCookie)));
} catch (error) {
clean(event);
cleanAuth(event);

return await sendRedirect(event, location, 302);
}
Expand All @@ -44,7 +37,7 @@ export default defineEventHandler(async (event) => {
}
}

clean(event);
cleanAuth(event);

return await sendRedirect(event, location, 302);
});
9 changes: 9 additions & 0 deletions server/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { H3Event } from 'h3';

import { COOKIE_NAMES } from '~/configs/properties';
import { stringToBase64 } from '~/utils/converters';

export async function fileToDataURI(file: File) {
Expand All @@ -12,3 +15,9 @@ export async function fileToDataURI(file: File) {

return 'data:' + file.type + ';base64,' + base64;
}

export function cleanAuth(event: H3Event): void {
setResponseHeader(event, 'Authorization', '');
deleteCookie(event, COOKIE_NAMES.refreshToken);
deleteCookie(event, COOKIE_NAMES.userIdentity);
}

0 comments on commit f3721d3

Please sign in to comment.