@@ -9,8 +9,7 @@ import * as passportAD from '../passport/activeDirectory';
99import { User } from '../../db/types' ;
1010import { AuthenticationElement } from '../../config/generated/config' ;
1111
12- import { toPublicUser } from './publicApi' ;
13- import { isAdminUser } from './utils' ;
12+ import { isAdminUser , toPublicUser } from './utils' ;
1413
1514const router = express . Router ( ) ;
1615const passport = getPassport ( ) ;
@@ -107,7 +106,7 @@ router.get('/openidconnect/callback', (req: Request, res: Response, next: NextFu
107106 passport . authenticate ( authStrategies [ 'openidconnect' ] . type , ( err : any , user : any , info : any ) => {
108107 if ( err ) {
109108 console . error ( 'Authentication error:' , err ) ;
110- return res . status ( 401 ) . end ( ) ;
109+ return res . status ( 500 ) . end ( ) ;
111110 }
112111 if ( ! user ) {
113112 console . error ( 'No user found:' , info ) ;
@@ -116,7 +115,7 @@ router.get('/openidconnect/callback', (req: Request, res: Response, next: NextFu
116115 req . logIn ( user , ( err ) => {
117116 if ( err ) {
118117 console . error ( 'Login error:' , err ) ;
119- return res . status ( 401 ) . end ( ) ;
118+ return res . status ( 500 ) . end ( ) ;
120119 }
121120 console . log ( 'Logged in successfully. User:' , user ) ;
122121 return res . redirect ( `${ uiHost } :${ uiPort } /dashboard/profile` ) ;
@@ -133,100 +132,124 @@ router.post('/logout', (req: Request, res: Response, next: NextFunction) => {
133132} ) ;
134133
135134router . get ( '/profile' , async ( req : Request , res : Response ) => {
136- if ( req . user ) {
137- const userVal = await db . findUser ( ( req . user as User ) . username ) ;
138- if ( ! userVal ) {
139- res . status ( 400 ) . send ( 'Error: Logged in user not found' ) . end ( ) ;
140- return ;
141- }
142- res . send ( toPublicUser ( userVal ) ) ;
143- } else {
144- res . status ( 401 ) . end ( ) ;
135+ if ( ! req . user ) {
136+ res
137+ . status ( 401 )
138+ . send ( {
139+ message : 'Not logged in' ,
140+ } )
141+ . end ( ) ;
142+ return ;
145143 }
144+
145+ const userVal = await db . findUser ( ( req . user as User ) . username ) ;
146+ if ( ! userVal ) {
147+ res . status ( 404 ) . send ( 'User not found' ) . end ( ) ;
148+ return ;
149+ }
150+
151+ res . send ( toPublicUser ( userVal ) ) ;
146152} ) ;
147153
148154router . post ( '/gitAccount' , async ( req : Request , res : Response ) => {
149- if ( req . user ) {
150- try {
151- let username =
152- req . body . username == null || req . body . username === 'undefined'
153- ? req . body . id
154- : req . body . username ;
155- username = username ?. split ( '@' ) [ 0 ] ;
156-
157- if ( ! username ) {
158- res . status ( 400 ) . send ( 'Error: Missing username. Git account not updated' ) . end ( ) ;
159- return ;
160- }
155+ if ( ! req . user ) {
156+ res
157+ . status ( 401 )
158+ . send ( {
159+ message : 'Not logged in' ,
160+ } )
161+ . end ( ) ;
162+ return ;
163+ }
161164
162- const reqUser = await db . findUser ( ( req . user as User ) . username ) ;
163- if ( username !== reqUser ?. username && ! reqUser ?. admin ) {
164- res . status ( 403 ) . send ( 'Error: You must be an admin to update a different account' ) . end ( ) ;
165- return ;
166- }
165+ try {
166+ let username =
167+ req . body . username == null || req . body . username === 'undefined'
168+ ? req . body . id
169+ : req . body . username ;
170+ username = username ?. split ( '@' ) [ 0 ] ;
167171
168- const user = await db . findUser ( username ) ;
169- if ( ! user ) {
170- res . status ( 400 ) . send ( 'Error: User not found' ) . end ( ) ;
171- return ;
172- }
172+ if ( ! username ) {
173+ res
174+ . status ( 400 )
175+ . send ( {
176+ message : 'Missing username. Git account not updated' ,
177+ } )
178+ . end ( ) ;
179+ return ;
180+ }
173181
174- console . log ( 'Adding gitAccount' + req . body . gitAccount ) ;
175- user . gitAccount = req . body . gitAccount ;
176- db . updateUser ( user ) ;
177- res . status ( 200 ) . end ( ) ;
178- } catch ( e : any ) {
182+ const reqUser = await db . findUser ( ( req . user as User ) . username ) ;
183+ if ( username !== reqUser ?. username && ! reqUser ?. admin ) {
179184 res
180- . status ( 500 )
185+ . status ( 403 )
181186 . send ( {
182- message : `Error updating git account: ${ e . message } ` ,
187+ message : 'Must be an admin to update a different account' ,
183188 } )
184189 . end ( ) ;
190+ return ;
185191 }
186- } else {
187- res . status ( 401 ) . end ( ) ;
188- }
189- } ) ;
190192
191- router . get ( '/me' , async ( req : Request , res : Response ) => {
192- if ( req . user ) {
193- const userVal = await db . findUser ( ( req . user as User ) . username ) ;
194- if ( ! userVal ) {
195- res . status ( 400 ) . send ( 'Error: Logged in user not found' ) . end ( ) ;
193+ const user = await db . findUser ( username ) ;
194+ if ( ! user ) {
195+ res
196+ . status ( 404 )
197+ . send ( {
198+ message : 'User not found' ,
199+ } )
200+ . end ( ) ;
196201 return ;
197202 }
198- res . send ( toPublicUser ( userVal ) ) ;
199- } else {
200- res . status ( 401 ) . end ( ) ;
203+
204+ user . gitAccount = req . body . gitAccount ;
205+ db . updateUser ( user ) ;
206+ res . status ( 200 ) . end ( ) ;
207+ } catch ( e : any ) {
208+ res
209+ . status ( 500 )
210+ . send ( {
211+ message : `Failed to update git account: ${ e . message } ` ,
212+ } )
213+ . end ( ) ;
201214 }
202215} ) ;
203216
204217router . post ( '/create-user' , async ( req : Request , res : Response ) => {
205218 if ( ! isAdminUser ( req . user ) ) {
206- res . status ( 401 ) . send ( {
207- message : 'You are not authorized to perform this action...' ,
208- } ) ;
219+ res
220+ . status ( 403 )
221+ . send ( {
222+ message : 'Not authorized to create users' ,
223+ } )
224+ . end ( ) ;
209225 return ;
210226 }
211227
212228 try {
213229 const { username, password, email, gitAccount, admin : isAdmin = false } = req . body ;
214230
215231 if ( ! username || ! password || ! email || ! gitAccount ) {
216- res . status ( 400 ) . send ( {
217- message : 'Missing required fields: username, password, email, and gitAccount are required' ,
218- } ) ;
232+ res
233+ . status ( 400 )
234+ . send ( {
235+ message :
236+ 'Missing required fields: username, password, email, and gitAccount are required' ,
237+ } )
238+ . end ( ) ;
219239 return ;
220240 }
221241
222242 await db . createUser ( username , password , email , gitAccount , isAdmin ) ;
223- res . status ( 201 ) . send ( {
224- message : 'User created successfully' ,
225- username,
226- } ) ;
243+ res
244+ . status ( 201 )
245+ . send ( {
246+ message : 'User created successfully' ,
247+ username,
248+ } )
249+ . end ( ) ;
227250 } catch ( error : any ) {
228251 console . error ( 'Error creating user:' , error ) ;
229- res . status ( 400 ) . send ( {
252+ res . status ( 500 ) . send ( {
230253 message : error . message || 'Failed to create user' ,
231254 } ) ;
232255 }
0 commit comments