Skip to content

Commit 1618fc8

Browse files
committed
Refactor authentication services to use server imports and enhance error handling in PrismaAuthService
1 parent bac24ea commit 1618fc8

File tree

14 files changed

+401
-57
lines changed

14 files changed

+401
-57
lines changed

apps/web/app/api/auth/login/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function POST(req: NextRequest) {
1616
const validatedData = loginSchema.parse(body);
1717

1818
// Dynamic import to keep server-only
19-
const { PrismaAuthService } = await import('@codervisor/devlog-core/auth');
19+
const { PrismaAuthService } = await import('@codervisor/devlog-core/server');
2020
const authService = PrismaAuthService.getInstance();
2121
await authService.initialize();
2222
const result = await authService.login(validatedData);

apps/web/app/api/auth/me/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function GET(req: NextRequest) {
1616
const token = authHeader.substring(7); // Remove 'Bearer ' prefix
1717

1818
// Dynamic import to keep server-only
19-
const { PrismaAuthService } = await import('@codervisor/devlog-core/auth');
19+
const { PrismaAuthService } = await import('@codervisor/devlog-core/server');
2020
const authService = PrismaAuthService.getInstance();
2121
await authService.initialize();
2222

apps/web/app/api/auth/refresh/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function POST(req: NextRequest) {
1515
const validatedData = refreshSchema.parse(body);
1616

1717
// Dynamic import to keep server-only
18-
const { PrismaAuthService } = await import('@codervisor/devlog-core/auth');
18+
const { PrismaAuthService } = await import('@codervisor/devlog-core/server');
1919
const authService = PrismaAuthService.getInstance();
2020
const newTokens = await authService.refreshToken(validatedData.refreshToken);
2121

apps/web/app/api/auth/register/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function POST(req: NextRequest) {
1717
const validatedData = registrationSchema.parse(body);
1818

1919
// Dynamic import to keep server-only
20-
const { PrismaAuthService } = await import('@codervisor/devlog-core/auth');
20+
const { PrismaAuthService } = await import('@codervisor/devlog-core/server');
2121
const authService = PrismaAuthService.getInstance();
2222
await authService.initialize();
2323
const result = await authService.register(validatedData);

apps/web/app/api/auth/reset-password/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function POST(req: NextRequest) {
2121
const action = searchParams.get('action');
2222

2323
// Dynamic import to keep server-only
24-
const { PrismaAuthService } = await import('@codervisor/devlog-core/auth');
24+
const { PrismaAuthService } = await import('@codervisor/devlog-core/server');
2525
const authService = PrismaAuthService.getInstance();
2626

2727
if (action === 'request') {

apps/web/app/api/auth/sso/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function POST(req: NextRequest) {
1717
const { provider, returnUrl } = authorizationSchema.parse(body);
1818

1919
// Dynamic import to keep server-only
20-
const { SSOService } = await import('@codervisor/devlog-core/auth');
20+
const { SSOService } = await import('@codervisor/devlog-core/server');
2121
const ssoService = SSOService.getInstance();
2222

2323
// Generate state for CSRF protection
@@ -64,7 +64,7 @@ export async function POST(req: NextRequest) {
6464
export async function GET(req: NextRequest) {
6565
try {
6666
// Dynamic import to keep server-only
67-
const { SSOService } = await import('@codervisor/devlog-core/auth');
67+
const { SSOService } = await import('@codervisor/devlog-core/server');
6868
const ssoService = SSOService.getInstance();
6969

7070
// Get available providers

apps/web/app/api/auth/verify-email/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function POST(req: NextRequest) {
1515
const validatedData = verifyEmailSchema.parse(body);
1616

1717
// Dynamic import to keep server-only
18-
const { PrismaAuthService } = await import('@codervisor/devlog-core/auth');
18+
const { PrismaAuthService } = await import('@codervisor/devlog-core/server');
1919
const authService = PrismaAuthService.getInstance();
2020
const user = await authService.verifyEmail(validatedData.token);
2121

apps/web/lib/auth-middleware.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ export async function withAuth<T>(
2828
const token = authHeader.substring(7); // Remove 'Bearer ' prefix
2929

3030
// Import PrismaAuthService dynamically to avoid initialization issues
31-
const { PrismaAuthService } = await import('@codervisor/devlog-core/auth');
31+
const { PrismaAuthService } = await import('@codervisor/devlog-core/server');
3232
const authService = PrismaAuthService.getInstance();
3333

34-
const user = await authService.verifyToken(token);
34+
const user = await authService.validateToken(token);
3535

3636
// Attach user to request
3737
const authenticatedReq = req as AuthenticatedRequest;
@@ -59,11 +59,11 @@ export async function withOptionalAuth<T>(
5959
if (authHeader && authHeader.startsWith('Bearer ')) {
6060
const token = authHeader.substring(7);
6161

62-
const { PrismaAuthService } = await import('@codervisor/devlog-core/auth');
62+
const { PrismaAuthService } = await import('@codervisor/devlog-core/server');
6363
const authService = PrismaAuthService.getInstance();
6464

6565
try {
66-
const user = await authService.verifyToken(token);
66+
const user = await authService.validateToken(token);
6767
(req as any).user = user;
6868
} catch {
6969
// Ignore token verification errors for optional auth

packages/core/src/services/prisma-auth-service.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class PrismaAuthService {
7676
}
7777
} catch (error) {
7878
// Prisma client not available - service will operate in fallback mode
79-
console.warn('[PrismaAuthService] Prisma client not available, operating in fallback mode:', error.message);
79+
console.warn('[PrismaAuthService] Prisma client not available, operating in fallback mode:', (error as Error).message);
8080
this.fallbackMode = true;
8181
}
8282
}
@@ -203,24 +203,40 @@ export class PrismaAuthService {
203203

204204
// Generate email verification token if required
205205
let emailVerificationToken: string | undefined;
206-
if (registration.requireEmailVerification) {
207-
emailVerificationToken = await this.generateEmailVerificationToken(user.id);
208-
}
206+
// Note: requireEmailVerification would need to be added to UserRegistration type if needed
207+
// if (registration.requireEmailVerification) {
208+
// emailVerificationToken = await this.generateEmailVerificationToken(user.id);
209+
// }
209210

210211
// Generate auth tokens
211212
const tokens = await this.generateTokens(user);
212213

213214
return {
214-
user: this.mapPrismaToUser(user),
215+
user: this.convertPrismaUserToUser(user),
215216
tokens,
216-
emailVerificationToken,
217217
};
218218
} catch (error) {
219219
console.error('[PrismaAuthService] Registration failed:', error);
220220
throw new Error(`Registration failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
221221
}
222222
}
223223

224+
/**
225+
* Convert Prisma user to User type
226+
*/
227+
private convertPrismaUserToUser(prismaUser: any): User {
228+
return {
229+
id: prismaUser.id,
230+
email: prismaUser.email,
231+
name: prismaUser.name || '',
232+
avatarUrl: prismaUser.avatarUrl,
233+
isEmailVerified: prismaUser.isEmailVerified || false,
234+
createdAt: prismaUser.createdAt?.toISOString() || new Date().toISOString(),
235+
updatedAt: prismaUser.updatedAt?.toISOString() || new Date().toISOString(),
236+
lastLoginAt: prismaUser.lastLoginAt?.toISOString(),
237+
};
238+
}
239+
224240
/**
225241
* Authenticate user login
226242
*/
@@ -280,7 +296,7 @@ export class PrismaAuthService {
280296
const tokens = await this.generateTokens(user);
281297

282298
return {
283-
user: this.mapPrismaToUser(user),
299+
user: this.convertPrismaUserToUser(user),
284300
tokens,
285301
};
286302
} catch (error) {

packages/core/src/services/prisma-devlog-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class PrismaDevlogService {
6666
}
6767
} catch (error) {
6868
// Prisma client not available - service will operate in fallback mode
69-
console.warn('[PrismaDevlogService] Prisma client not available, operating in fallback mode:', error.message);
69+
console.warn('[PrismaDevlogService] Prisma client not available, operating in fallback mode:', (error as Error).message);
7070
this.fallbackMode = true;
7171
}
7272
}

0 commit comments

Comments
 (0)