Skip to content

Commit e078361

Browse files
committed
feat(49): update auth
1 parent a178364 commit e078361

10 files changed

Lines changed: 36 additions & 22 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function LoginPage() {
2+
return <div className="">login</div>;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function SignupPage() {
2+
return <div className="">signup</div>;
3+
}

src/app/(web)/dashboard/page.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function DashboardPage() {
2+
return <div className=""></div>;
3+
}

src/app/api/auth/logout/route.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ export async function POST(_request: NextRequest) {
77
await logout();
88
return sargeApiResponse({ message: 'Logged out successfully' }, 200);
99
} catch (error) {
10-
// Even if logout fails, we want to clear the session
11-
// This is a security best practice
1210
const message = error instanceof Error ? error.message : String(error);
1311
return sargeApiResponse({ message: 'Logged out', error: message }, 200);
1412
}

src/app/api/users/[id]/route.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ export async function DELETE(
99
{ params }: { params: Promise<{ id: string }> }
1010
) {
1111
try {
12-
const session = await requireAuth();
13-
1412
const id = (await params).id;
1513
const user = await userController.delete(id);
1614
return sargeApiResponse(user, 200);

src/lib/auth/auth-service.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ import { userController } from '../controllers/user.controller';
66
import { prisma } from '../prisma';
77
import bcrypt from 'bcrypt';
88
import { AuthorizationError } from '../schemas/errors';
9+
import { type User } from '@/generated/prisma';
910

1011
export interface LoginCredentials {
1112
email: string;
1213
password: string;
1314
}
1415

15-
export interface User {
16-
id: string;
17-
email: string;
18-
role?: string;
19-
}
20-
2116
export async function login(_credentials: LoginCredentials): Promise<User> {
2217
const user = await prisma.user.findUnique({
2318
where: {

src/lib/auth/auth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export const config = {
1111
secure: process.env.NODE_ENV === 'production',
1212
};
1313

14-
const secretKey = createSecretKey(process.env.JWT_SECRET!, 'utf-8');
14+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
15+
export const secretKey = createSecretKey(process.env.JWT_SECRET!, 'utf-8');
1516

1617
export interface SessionPayload {
1718
userId: string;
18-
email?: string;
19-
exp?: number;
19+
email: string;
2020
}
2121

2222
export async function createSession(payload: SessionPayload) {

src/lib/auth/permissions.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'server-only';
2+
import { type User } from '@/generated/prisma';
3+
4+
export async function canView(target: User, requester: User): Promise<boolean> {
5+
if (target.id === requester.id) {
6+
return true;
7+
} else if (target.orgId === requester.orgId) {
8+
return true;
9+
}
10+
return false;
11+
}
12+
13+
export async function canModify(target: User, requester: User): Promise<boolean> {
14+
return false;
15+
}

src/lib/schemas/user.schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const createUserSchema = z.object({
1717
.toLowerCase()
1818
.trim()
1919
.max(255, 'Email must be less than 255 characters'),
20+
hashedPassword: z.string(),
2021
});
2122

2223
export const UserSchema = z.object({

src/middleware.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { type NextRequest, NextResponse } from 'next/server';
22
import { jwtVerify } from 'jose';
3-
import { createSecretKey } from 'node:crypto';
4-
import { config as authConfig } from './lib/auth/auth';
53

6-
const secretKey = createSecretKey(process.env.JWT_SECRET!, 'utf-8');
7-
8-
// Define protected routes
9-
const protectedRoutes = ['/api'];
4+
const protectedRoutes = ['/dashboard'];
105

116
export async function middleware(request: NextRequest) {
127
const { pathname } = request.nextUrl;
13-
const sessionCookie = request.cookies.get(authConfig.cookieName)?.value;
8+
const sessionCookie = request.cookies.get('sarge.session')?.value;
149

1510
// Check if the route requires authentication
1611
const isProtectedRoute = protectedRoutes.some((route) => pathname.startsWith(route));
@@ -19,8 +14,11 @@ export async function middleware(request: NextRequest) {
1914
let isAuthenticated = false;
2015
if (sessionCookie) {
2116
try {
22-
await jwtVerify(sessionCookie, secretKey, {
23-
issuer: authConfig.issuer,
17+
// Create secret key directly in middleware (Edge Runtime compatible)
18+
const secret = new TextEncoder().encode(process.env.JWT_SECRET);
19+
20+
await jwtVerify(sessionCookie, secret, {
21+
issuer: 'sargenu',
2422
});
2523
isAuthenticated = true;
2624
} catch {

0 commit comments

Comments
 (0)