@@ -2,32 +2,47 @@ import { NextResponse } from 'next/server'
2
2
import { prisma } from '@/lib/prisma'
3
3
import { cookies } from 'next/headers'
4
4
5
-
6
-
7
5
export async function POST ( request : Request ) {
8
- const { username, password } = await request . json ( )
9
- console . log ( 'Received username:' , username )
10
- console . log ( 'Received password:' , password )
11
-
6
+ try {
7
+ // Parsing the request body
8
+ let username : string , password : string ;
12
9
try {
13
- const user = await prisma . user . findUnique ( { where : { username } } )
14
- if ( ! user ) {
15
- console . log ( 'User not found' )
16
- return NextResponse . json ( { error : 'Invalid credentials' } , { status : 400 } )
17
- }
10
+ const body = await request . json ( ) ;
11
+ username = body . username ;
12
+ password = body . password ;
13
+ console . log ( 'Received username:' , username ) ;
14
+ console . log ( 'Received password:' , password ) ;
15
+ } catch ( err ) {
16
+ console . error ( 'Invalid JSON in request:' , err ) ;
17
+ return NextResponse . json ( { error : 'Invalid request format' } , { status : 400 } ) ;
18
+ }
19
+
20
+ // Fetching user from database
21
+ const user = await prisma . user . findUnique ( { where : { username } } ) ;
22
+ if ( ! user ) {
23
+ console . error ( 'User not found for username:' , username ) ;
24
+ return NextResponse . json ( { error : 'Invalid credentials' } , { status : 400 } ) ;
25
+ }
18
26
27
+ // Validating password (without bcrypt)
19
28
if ( user . password !== password ) {
20
- console . log ( 'Invalid password' )
21
- return NextResponse . json ( { error : 'Invalid credentials' } , { status : 400 } )
29
+ console . error ( 'Invalid password for user:' , username ) ;
30
+ return NextResponse . json ( { error : 'Invalid credentials' } , { status : 400 } ) ;
22
31
}
23
-
24
- cookies ( ) . set ( 'userId' , user . id , { httpOnly : true , secure : process . env . NODE_ENV === 'production' } )
25
- cookies ( ) . set ( 'username' , user . username , { httpOnly : true , secure : process . env . NODE_ENV === 'production' } )
26
-
27
- return NextResponse . json ( { message : 'Login successful' } )
28
- } catch ( error ) {
29
- console . error ( 'Login error :' , error )
30
- return NextResponse . json ( { error : 'An error occurred during login ' } , { status : 500 } )
32
+
33
+ // Setting cookies with 1-week expiry
34
+ try {
35
+ cookies ( ) . set ( 'userId' , user . id , { httpOnly : true , secure : process . env . NODE_ENV === 'production' , maxAge : 60 * 60 * 24 * 7 } ) ; // 1 week expiry
36
+ cookies ( ) . set ( 'username' , user . username , { httpOnly : true , secure : process . env . NODE_ENV === 'production' , maxAge : 60 * 60 * 24 * 7 } ) ;
37
+ } catch ( cookieError ) {
38
+ console . error ( 'Error setting cookies :' , cookieError ) ;
39
+ return NextResponse . json ( { error : 'Error setting cookies ' } , { status : 500 } ) ;
31
40
}
41
+
42
+ return NextResponse . json ( { message : 'Login successful' } ) ;
43
+ } catch ( error ) {
44
+ console . error ( 'Login error:' , error ) ;
45
+ return NextResponse . json ( { error : 'An error occurred during login' } , { status : 500 } ) ;
32
46
}
33
-
47
+ }
48
+
0 commit comments