11import type { Request , Response , NextFunction } from "express" ;
22import bcrypt from "bcryptjs" ;
3- import User , { type IUser } from "../models/userModel.js" ;
3+ import User , { type IUser } from "../models/userModel.js" ;
44import { generateToken } from "../utils/generateToken.js" ;
55import { userSchema , loginSchema } from "../utils/validateInputs.js" ;
66import dotenv from "dotenv" ;
77
88dotenv . config ( ) ;
99const asTypedUser = ( user : any ) : IUser & { _id : string } => user as IUser & { _id : string } ;
1010
11- //signup controller
11+ // ✅ SIGNUP CONTROLLER
1212export const registerUser = async ( req : Request , res : Response , next : NextFunction ) => {
1313 try {
1414 const parseResult = userSchema . safeParse ( req . body ) ;
@@ -19,10 +19,15 @@ export const registerUser = async (req: Request, res: Response, next: NextFuncti
1919 } ) ;
2020 }
2121
22- const { name, email, password } = parseResult . data ;
22+ const { email, password } = parseResult . data ;
23+
24+ // ✅ Auto-derive name from email
25+ const name = email . split ( "@" ) [ 0 ] ;
26+
2327 const existingUser = await User . findOne ( { email } ) ;
24- if ( existingUser )
28+ if ( existingUser ) {
2529 return res . status ( 400 ) . json ( { success : false , message : "Email already registered" } ) ;
30+ }
2631
2732 const hashedPassword = await bcrypt . hash ( password , 10 ) ;
2833 const newUser = await User . create ( { name, email, password : hashedPassword } ) ;
@@ -38,7 +43,7 @@ export const registerUser = async (req: Request, res: Response, next: NextFuncti
3843 }
3944} ;
4045
41- //sign in controller
46+ // ✅ LOGIN CONTROLLER (same as before)
4247export const loginUser = async ( req : Request , res : Response , next : NextFunction ) => {
4348 try {
4449 const parseResult = loginSchema . safeParse ( req . body ) ;
@@ -53,6 +58,7 @@ export const loginUser = async (req: Request, res: Response, next: NextFunction)
5358 const foundUser = await User . findOne ( { email } ) ;
5459 if ( ! foundUser )
5560 return res . status ( 400 ) . json ( { success : false , message : "Invalid credentials" } ) ;
61+
5662 if ( ! foundUser . password || foundUser . password === "" ) {
5763 return res . status ( 400 ) . json ( {
5864 success : false ,
@@ -76,22 +82,27 @@ export const loginUser = async (req: Request, res: Response, next: NextFunction)
7682 }
7783} ;
7884
79- //OAuth callback handler
80- export const oauthCallback = ( req : Request & { user ?: any } , res : Response ) => {
85+ // ✅ GET PROFILE CONTROLLER (unchanged)
86+ export const getUserProfile = async ( req : Request , res : Response ) => {
8187 try {
82- const frontendUrl = process . env . FRONTEND_URL || "http://localhost:5173" ;
83- const user = req . user as IUser & { _id : string } | undefined ;
88+ const user = await User . findById ( req . userId ) . select ( "-password" ) ;
8489
8590 if ( ! user ) {
86- return res . redirect ( `${ frontendUrl } /signin?error=oauth_failed` ) ;
91+ return res . status ( 404 ) . json ( {
92+ success : false ,
93+ message : "User not found" ,
94+ } ) ;
8795 }
8896
89- const token = generateToken ( user . _id . toString ( ) ) ;
90- const redirectUrl = `${ frontendUrl } /oauth-success#token=${ token } ` ;
91-
92- return res . redirect ( redirectUrl ) ;
93- } catch ( err ) {
94- const frontendUrl = process . env . FRONTEND_URL || "http://localhost:5173" ;
95- return res . redirect ( `${ frontendUrl } /signin?error=server_error` ) ;
97+ res . status ( 200 ) . json ( {
98+ success : true ,
99+ user,
100+ } ) ;
101+ } catch ( error ) {
102+ console . error ( error ) ;
103+ res . status ( 500 ) . json ( {
104+ success : false ,
105+ message : "Server error" ,
106+ } ) ;
96107 }
97108} ;
0 commit comments