simikkk commited on
Commit
6894d4f
·
verified ·
1 Parent(s): 09cd096

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

Browse files
Files changed (1) hide show
  1. src/app/api/auth/logout/route.ts +76 -8
src/app/api/auth/logout/route.ts CHANGED
@@ -1,31 +1,99 @@
1
  import { NextRequest, NextResponse } from 'next/server';
2
- import { getCookieToken, deleteSession, SESSION_COOKIE_NAME } from '@/lib/auth';
 
 
 
 
 
 
 
 
 
3
 
4
  export async function POST(request: NextRequest) {
5
  try {
6
- const token = getCookieToken(request);
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- if (token) {
9
- await deleteSession(token);
 
 
 
 
 
 
10
  }
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  // ── Determine if behind HTTPS proxy ───────────────────────────────
13
  const forwardedProto = request.headers.get('x-forwarded-proto');
14
  const isSecure = forwardedProto === 'https' || process.env.NODE_ENV === 'production';
15
 
16
- const response = NextResponse.json({ success: true });
 
 
 
 
 
 
 
 
 
17
 
18
- response.cookies.set(SESSION_COOKIE_NAME, '', {
19
  httpOnly: true,
20
  secure: isSecure,
21
  sameSite: 'lax',
22
  path: '/',
23
- maxAge: 0,
24
  });
25
 
 
 
26
  return response;
27
  } catch (error) {
28
- console.error('[LOGOUT_ERROR]', error);
29
  return NextResponse.json(
30
  { error: 'Internal server error' },
31
  { status: 500 }
 
1
  import { NextRequest, NextResponse } from 'next/server';
2
+ import { db } from '@/lib/db';
3
+ import { signupSchema, getClientIp } from '@/lib/validation';
4
+ import {
5
+ hashPassword,
6
+ createSession,
7
+ generateApiKey,
8
+ SESSION_COOKIE_NAME,
9
+ } from '@/lib/auth';
10
+ import { authRateLimit } from '@/lib/rate-limit';
11
+ import { logActivity } from '@/lib/activity';
12
 
13
  export async function POST(request: NextRequest) {
14
  try {
15
+ // ── Rate Limit ────────────────────────────────────────────────────
16
+ const ip = getClientIp(request);
17
+ const { allowed, retryAfterMs } = await authRateLimit(`auth:${ip}`);
18
+ if (!allowed) {
19
+ return NextResponse.json(
20
+ { error: 'Too many signup attempts. Please try again later.' },
21
+ {
22
+ status: 429,
23
+ headers: { 'Retry-After': String(Math.ceil(retryAfterMs / 1000)) },
24
+ }
25
+ );
26
+ }
27
 
28
+ // ── Parse & Validate Body ─────────────────────────────────────────
29
+ const body = await request.json();
30
+ const parsed = signupSchema.safeParse(body);
31
+ if (!parsed.success) {
32
+ return NextResponse.json(
33
+ { error: parsed.error.issues[0].message },
34
+ { status: 400 }
35
+ );
36
  }
37
 
38
+ const { name, email, password } = parsed.data;
39
+
40
+ // ── Check if Email Exists ─────────────────────────────────────────
41
+ const existing = await db.user.findUnique({ where: { email } });
42
+ if (existing) {
43
+ return NextResponse.json(
44
+ { error: 'An account with this email already exists' },
45
+ { status: 409 }
46
+ );
47
+ }
48
+
49
+ // ── Hash Password & Generate API Key ──────────────────────────────
50
+ const [hashedPassword, apiKey] = await Promise.all([
51
+ hashPassword(password),
52
+ generateApiKey(),
53
+ ]);
54
+
55
+ // ── Create User ───────────────────────────────────────────────────
56
+ const user = await db.user.create({
57
+ data: {
58
+ name,
59
+ email,
60
+ password: hashedPassword,
61
+ apiKey,
62
+ plan: 'free',
63
+ },
64
+ });
65
+
66
+ // ── Create Session ────────────────────────────────────────────────
67
+ const token = await createSession(user.id);
68
+
69
  // ── Determine if behind HTTPS proxy ───────────────────────────────
70
  const forwardedProto = request.headers.get('x-forwarded-proto');
71
  const isSecure = forwardedProto === 'https' || process.env.NODE_ENV === 'production';
72
 
73
+ // ── Set Cookie & Respond ──────────────────────────────────────────
74
+ const response = NextResponse.json({
75
+ user: {
76
+ id: user.id,
77
+ email: user.email,
78
+ name: user.name,
79
+ plan: user.plan,
80
+ createdAt: user.createdAt,
81
+ },
82
+ });
83
 
84
+ response.cookies.set(SESSION_COOKIE_NAME, token, {
85
  httpOnly: true,
86
  secure: isSecure,
87
  sameSite: 'lax',
88
  path: '/',
89
+ maxAge: 7 * 24 * 60 * 60, // 7 days
90
  });
91
 
92
+ logActivity(user.id, 'signup', 'Account created', user.email);
93
+
94
  return response;
95
  } catch (error) {
96
+ console.error('[SIGNUP_ERROR]', error);
97
  return NextResponse.json(
98
  { error: 'Internal server error' },
99
  { status: 500 }