11const bcrypt = require ( "bcrypt" ) ;
22const jwt = require ( "jsonwebtoken" ) ;
3+ const User = require ( "../models/User" ) ;
34
4- const users = [ ] ;
55const tokenBlacklist = [ ] ;
66
77const JWT_SECRET = process . env . JWT_SECRET || "your_jwt_secret" ;
88const JWT_EXPIRES_IN = "1h" ;
99
1010exports . registerUser = async ( req , res ) => {
11- console . log ( "Register body:" , req . body ) ;
12- const { email, password } = req . body ;
13- if ( users . find ( user => user . email === email ) ) {
14- return res . status ( 400 ) . json ( { message : "User already exists" } ) ;
11+ try {
12+ console . log ( "Register body:" , req . body ) ;
13+ const { email, password } = req . body ;
14+
15+ if ( ! email || ! password ) {
16+ return res . status ( 400 ) . json ( { message : "Email and password are required" } ) ;
17+ }
18+ const existingUser = await User . findOne ( { email } ) ;
19+ if ( existingUser ) {
20+ return res . status ( 400 ) . json ( { message : "User already exists" } ) ;
21+ }
22+
23+ const hash = await bcrypt . hash ( password , 10 ) ;
24+
25+ const newUser = new User ( { email, password : hash } ) ;
26+ await newUser . save ( ) ;
27+
28+ return res . status ( 201 ) . json ( { message : "User registered successfully" } ) ;
29+ } catch ( err ) {
30+ console . error ( "Register error:" , err ) ;
31+ return res . status ( 500 ) . json ( { message : "Server error" } ) ;
1532 }
16- const hash = await bcrypt . hash ( password , 10 ) ;
17- users . push ( { email, password : hash } ) ;
18- res . json ( { message : "User registered" } ) ;
19- }
33+ } ;
34+
2035exports . loginUser = async ( req , res ) => {
2136 try {
2237 console . log ( "Login body:" , req . body ) ;
23-
2438 const { email, password } = req . body ;
2539
26- // Validate input
2740 if ( ! email || ! password ) {
2841 return res . status ( 400 ) . json ( { message : "Email and password are required" } ) ;
2942 }
30-
31- // Find user
32- const user = users . find ( u => u . email === email ) ;
43+ const user = await User . findOne ( { email } ) ;
3344 if ( ! user ) {
3445 return res . status ( 400 ) . json ( { message : "Invalid credentials" } ) ;
3546 }
36-
37- // Compare password
3847 const match = await bcrypt . compare ( password , user . password ) ;
3948 if ( ! match ) {
4049 return res . status ( 400 ) . json ( { message : "Invalid credentials" } ) ;
4150 }
51+ const token = jwt . sign ( { email : user . email } , JWT_SECRET , { expiresIn : JWT_EXPIRES_IN } ) ;
4252
43- // Generate token
44- const token = jwt . sign ( { email } , JWT_SECRET , { expiresIn : JWT_EXPIRES_IN } ) ;
45-
46- // Send response
4753 return res . status ( 200 ) . json ( { token } ) ;
4854 } catch ( err ) {
4955 console . error ( "Login error:" , err ) ;
5056 return res . status ( 500 ) . json ( { message : "Server error" } ) ;
5157 }
5258} ;
53-
5459exports . logoutUser = ( req , res ) => {
5560 const { token } = req . body ;
61+ if ( ! token ) {
62+ return res . status ( 400 ) . json ( { message : "Token required for logout" } ) ;
63+ }
5664 tokenBlacklist . push ( token ) ;
57- res . json ( { message : "Logged out" } ) ;
58- }
59-
60- exports . getUsers = ( ) => users ;
61-
62- exports . getBlacklist = ( ) => tokenBlacklist ;
65+ res . json ( { message : "Logged out successfully" } ) ;
66+ } ;
67+ exports . getBlacklist = ( ) => tokenBlacklist ;
0 commit comments