Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Oct 9, 2024
1 parent 5bd9393 commit a532184
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/server/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { encrypt } from "@/lib/jwt";
import prisma from "@/lib/prisma";
import { serverEnv } from "@/utils/env/server";
import { Elysia, InternalServerError } from "elysia";
import { cookies } from "next/headers";
import { authUser } from "./typebox";

/**
Expand All @@ -28,8 +29,9 @@ export const authRoute = new Elysia({ prefix: "/auth" })
});

// Set authentication cookie
ctx.cookie[serverEnv.AUTH_COOKIE].set({
value: await encrypt(user),
cookies().set({
name: serverEnv.AUTH_COOKIE,
value: (await encrypt(user))!,
path: "/",
httpOnly: true,
maxAge: serverEnv.SEVEN_DAYS,
Expand All @@ -52,9 +54,9 @@ export const authRoute = new Elysia({ prefix: "/auth" })

if (!user) throw new InternalServerError("User not found");

// Set authentication cookie
ctx.cookie[serverEnv.AUTH_COOKIE].set({
value: await encrypt(user),
cookies().set({
name: serverEnv.AUTH_COOKIE,
value: (await encrypt(user))!,
path: "/",
httpOnly: true,
maxAge: serverEnv.SEVEN_DAYS,
Expand All @@ -66,11 +68,5 @@ export const authRoute = new Elysia({ prefix: "/auth" })
)
.get("/logout", (ctx) => {
// Clear authentication cookie
ctx.cookie[serverEnv.AUTH_COOKIE].set({
value: "",
path: "/",
httpOnly: true,
maxAge: 0,
});
return "success";
return !!cookies().delete(serverEnv.AUTH_COOKIE);
});

0 comments on commit a532184

Please sign in to comment.