Skip to content

Commit

Permalink
Improve naming
Browse files Browse the repository at this point in the history
  • Loading branch information
leerob committed Sep 16, 2024
1 parent 5b48910 commit 71520b0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ type SessionData = {
expires: string;
};

export async function encrypt(payload: SessionData) {
export async function signToken(payload: SessionData) {
return await new SignJWT(payload)
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('1 day from now')
.sign(key);
}

export async function decrypt(input: string) {
export async function verifyToken(input: string) {
const { payload } = await jwtVerify(input, key, {
algorithms: ['HS256'],
});
Expand All @@ -40,7 +40,7 @@ export async function decrypt(input: string) {
export async function getSession() {
const session = cookies().get('session')?.value;
if (!session) return null;
return await decrypt(session);
return await verifyToken(session);
}

export async function setSession(user: NewUser) {
Expand All @@ -49,7 +49,7 @@ export async function setSession(user: NewUser) {
user: { id: user.id! },
expires: expiresInOneDay.toISOString(),
};
const encryptedSession = await encrypt(session);
const encryptedSession = await signToken(session);
cookies().set('session', encryptedSession, {
expires: expiresInOneDay,
httpOnly: true,
Expand Down
4 changes: 2 additions & 2 deletions lib/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { desc, and, eq, isNull } from 'drizzle-orm';
import { db } from './drizzle';
import { activityLogs, teamMembers, teams, users } from './schema';
import { cookies } from 'next/headers';
import { decrypt } from '@/lib/auth/session';
import { verifyToken } from '@/lib/auth/session';

export async function getUser() {
const sessionCookie = cookies().get('session');
if (!sessionCookie || !sessionCookie.value) {
return null;
}

const sessionData = await decrypt(sessionCookie.value);
const sessionData = await verifyToken(sessionCookie.value);
if (
!sessionData ||
!sessionData.user ||
Expand Down
6 changes: 3 additions & 3 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { encrypt, decrypt } from '@/lib/auth/session';
import { signToken, verifyToken } from '@/lib/auth/session';

const protectedRoutes = '/dashboard';

Expand All @@ -17,12 +17,12 @@ export async function middleware(request: NextRequest) {

if (sessionCookie) {
try {
const parsed = await decrypt(sessionCookie.value);
const parsed = await verifyToken(sessionCookie.value);
const expiresInOneDay = new Date(Date.now() + 24 * 60 * 60 * 1000);

res.cookies.set({
name: 'session',
value: await encrypt({
value: await signToken({
...parsed,
expires: expiresInOneDay.toISOString(),
}),
Expand Down

0 comments on commit 71520b0

Please sign in to comment.