Skip to content

Commit

Permalink
chore: make cookie expiration same as jwt (#1554)
Browse files Browse the repository at this point in the history
* chore: make cookie expiration same as jwt

* fix: update package-lock

* chore: remove package-lock.json

---------

Co-authored-by: BrickheadJohnny <[email protected]>
  • Loading branch information
dominik-stumpf and BrickheadJohnny authored Nov 20, 2024
1 parent 1f089e2 commit 198f502
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"clsx": "^2.1.1",
"foxact": "^0.2.41",
"jotai": "^2.10.2",
"jwt-decode": "^4.0.0",
"next": "15.0.3",
"next-themes": "^0.4.3",
"react": "19.0.0-rc-66855b96-20241106",
Expand Down
30 changes: 17 additions & 13 deletions src/actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { GUILD_AUTH_COOKIE_NAME } from "@/config/constants";
import { env } from "@/lib/env";
import { jwtDecode } from "jwt-decode";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { z } from "zod";
Expand All @@ -12,6 +13,12 @@ const authSchema = z.object({
userId: z.string().uuid(),
});

const tokenSchema = z.object({
userId: z.string().uuid(),
exp: z.number().positive().int(),
iat: z.number().positive().int(),
});

export const signIn = async ({
message,
signature,
Expand All @@ -38,26 +45,23 @@ export const signIn = async ({
requestInit,
);

let json: unknown;
if (signInRes.status === 401) {
const registerRes = await fetch(
`${env.NEXT_PUBLIC_API}/auth/siwe/register`,
requestInit,
);
const json = await registerRes.json();

const registerData = authSchema.parse(json);
cookieStore.set(GUILD_AUTH_COOKIE_NAME, registerData.token);

return registerData;
json = await registerRes.json();
} else {
json = await signInRes.json();
}
const authData = authSchema.parse(json);
const { exp } = tokenSchema.parse(jwtDecode(authData.token));

const json = await signInRes.json();

const signInData = authSchema.parse(json);

cookieStore.set(GUILD_AUTH_COOKIE_NAME, signInData.token);

return signInData;
cookieStore.set(GUILD_AUTH_COOKIE_NAME, authData.token, {
expires: new Date(exp * 1000),
});
return authData;
};

export const signOut = async (redirectTo?: string) => {
Expand Down

0 comments on commit 198f502

Please sign in to comment.