@@ -5,6 +5,14 @@ import { ENV } from '../config/env';
55import { hashPassword , generateApiKey } from '../utils/auth' ;
66import { successResponse , errorResponse } from '../utils/response' ;
77import UserRepository from '../repositories/user-repository' ;
8+ import Users from '../models/user' ;
9+ import { InferAttributes } from 'sequelize' ;
10+
11+ type UserType = InferAttributes < Users > ;
12+
13+ interface AuthRequest extends Request {
14+ user ?: UserType ;
15+ }
816
917export const registerUser = async ( req : Request , res : Response ) => {
1018 try {
@@ -71,21 +79,9 @@ export const loginUser = async (req: Request, res: Response) => {
7179 }
7280} ;
7381
74- export const getUserDetails = async ( req : Request , res : Response ) => {
82+ export const getUserDetails = async ( req : AuthRequest , res : Response ) => {
7583 try {
76- const token = req . headers . authorization ?. split ( ' ' ) [ 1 ] ;
77- if ( ! token ) {
78- return errorResponse ( res , 'Unauthorized' , 401 ) ;
79- }
80-
81- const decoded : any = jwt . verify ( token , ENV . JWT_SECRET ) ;
82- const user = await UserRepository . findOne ( { id : decoded . userId } ) ;
83-
84- if ( ! user ) {
85- return errorResponse ( res , 'Unauthorized' , 401 ) ;
86- }
87-
88- return successResponse ( res , user , 'User details retrieved' ) ;
84+ return successResponse ( res , req . user , 'User details retrieved' ) ;
8985 } catch ( error ) {
9086 console . error ( error ) ;
9187 return errorResponse ( res , 'Unauthorized' , 401 ) ;
0 commit comments