Skip to content

Commit

Permalink
feat(api): logout user route (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayllyz authored Apr 16, 2024
1 parent 1d090e4 commit a2ba659
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
16 changes: 13 additions & 3 deletions apps/api/src/handlers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import { getCookie, setCookie } from 'hono/cookie';
import { deleteCookie, getCookie, setCookie } from 'hono/cookie';
import { HTTPException } from 'hono/http-exception';
import { supabase } from '../libs/supabase.js';
import { zodErrorHook } from '../libs/zodError.js';
import { loginUser, refreshTokens, signupUser } from '../routes/auth.js';
import { loginUser, logoutUser, refreshTokens, signupUser } from '../routes/auth.js';
import type { Variables } from '../validators/general.js';

export const auth = new OpenAPIHono({
export const auth = new OpenAPIHono<{ Variables: Variables }>({
defaultHook: zodErrorHook,
});

Expand Down Expand Up @@ -106,3 +107,12 @@ auth.openapi(refreshTokens, async (c) => {

return c.json({ message: 'Token refreshed' });
});

auth.openapi(logoutUser, async (c) => {
if (getCookie(c, 'refresh_token') && getCookie(c, 'access_token')) {
deleteCookie(c, 'refresh_token');
deleteCookie(c, 'access_token');
}

return c.json({ message: 'Logged out' });
});
19 changes: 19 additions & 0 deletions apps/api/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,22 @@ export const refreshTokens = createRoute({
},
tags: ['auth'],
});

export const logoutUser = createRoute({
method: 'delete',
path: '/logout',
summary: 'Logout a user',
description: 'Logout a user',
responses: {
200: {
description: 'Successful response',
content: {
'application/json': {
schema: { type: 'string' },
},
},
},
500: serverErrorSchema,
},
tags: ['auth'],
});

0 comments on commit a2ba659

Please sign in to comment.