fix(backend): 409 Conflict bij het oproepen van /hello met een reeds geregistreerd account.
This commit is contained in:
		
							parent
							
								
									04fd54e3d6
								
							
						
					
					
						commit
						0b9f765366
					
				
					 2 changed files with 6 additions and 34 deletions
				
			
		|  | @ -2,10 +2,9 @@ import { UnauthorizedException } from '../exceptions/unauthorized-exception.js'; | ||||||
| import { getLogger } from '../logging/initalize.js'; | import { getLogger } from '../logging/initalize.js'; | ||||||
| import { AuthenticatedRequest } from '../middleware/auth/authenticated-request.js'; | import { AuthenticatedRequest } from '../middleware/auth/authenticated-request.js'; | ||||||
| import { envVars, getEnvVar } from '../util/envVars.js'; | import { envVars, getEnvVar } from '../util/envVars.js'; | ||||||
| import { createOrUpdateStudent, createStudent } from '../services/students.js'; | import { createOrUpdateStudent } from '../services/students.js'; | ||||||
| import { AuthenticationInfo } from '../middleware/auth/authentication-info.js'; |  | ||||||
| import { Request, Response } from 'express'; | import { Request, Response } from 'express'; | ||||||
| import { createOrUpdateTeacher, createTeacher } from '../services/teachers.js'; | import { createOrUpdateTeacher } from '../services/teachers.js'; | ||||||
| 
 | 
 | ||||||
| interface FrontendIdpConfig { | interface FrontendIdpConfig { | ||||||
|     authority: string; |     authority: string; | ||||||
|  | @ -45,31 +44,6 @@ export function handleGetFrontendAuthConfig(_req: Request, res: Response): void | ||||||
|     res.json(getFrontendAuthConfig()); |     res.json(getFrontendAuthConfig()); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function handleHello(req: AuthenticatedRequest): Promise<void> { |  | ||||||
|     const auth: AuthenticationInfo = req.auth!; |  | ||||||
|     if (auth.accountType === 'teacher') { |  | ||||||
|         await createTeacher( |  | ||||||
|             { |  | ||||||
|                 id: auth.username, |  | ||||||
|                 username: auth.username, |  | ||||||
|                 firstName: auth.firstName ?? '', |  | ||||||
|                 lastName: auth.lastName ?? '', |  | ||||||
|             }, |  | ||||||
|             true |  | ||||||
|         ); |  | ||||||
|     } else { |  | ||||||
|         await createStudent( |  | ||||||
|             { |  | ||||||
|                 id: auth.username, |  | ||||||
|                 username: auth.username, |  | ||||||
|                 firstName: auth.firstName ?? '', |  | ||||||
|                 lastName: auth.lastName ?? '', |  | ||||||
|             }, |  | ||||||
|             true |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| export async function postHelloHandler(req: AuthenticatedRequest, res: Response): Promise<void> { | export async function postHelloHandler(req: AuthenticatedRequest, res: Response): Promise<void> { | ||||||
|     const auth = req.auth; |     const auth = req.auth; | ||||||
|     if (!auth) { |     if (!auth) { | ||||||
|  |  | ||||||
|  | @ -1,15 +1,11 @@ | ||||||
| import express from 'express'; | import express from 'express'; | ||||||
| import { handleGetFrontendAuthConfig, handleHello, postHelloHandler } from '../controllers/auth.js'; | import { handleGetFrontendAuthConfig, postHelloHandler } from '../controllers/auth.js'; | ||||||
| import { authenticatedOnly, studentsOnly, teachersOnly } from '../middleware/auth/checks/auth-checks.js'; | import { authenticatedOnly, studentsOnly, teachersOnly } from '../middleware/auth/checks/auth-checks.js'; | ||||||
| 
 | 
 | ||||||
| const router = express.Router(); | const router = express.Router(); | ||||||
| 
 | 
 | ||||||
| // Returns auth configuration for frontend
 | // Returns auth configuration for frontend
 | ||||||
| router.get('/config', handleGetFrontendAuthConfig); | router.get('/config', handleGetFrontendAuthConfig) | ||||||
| 
 |  | ||||||
| // This endpoint is called by the client when the user has just logged in.
 |  | ||||||
| // It creates or updates the user entity based on the authentication data the endpoint was called with.
 |  | ||||||
| router.post('/hello', authenticatedOnly, handleHello); |  | ||||||
| 
 | 
 | ||||||
| router.get('/testAuthenticatedOnly', authenticatedOnly, (_req, res) => { | router.get('/testAuthenticatedOnly', authenticatedOnly, (_req, res) => { | ||||||
|     /* #swagger.security = [{ "student": [ ] }, { "teacher": [ ] }] */ |     /* #swagger.security = [{ "student": [ ] }, { "teacher": [ ] }] */ | ||||||
|  | @ -26,6 +22,8 @@ router.get('/testTeachersOnly', teachersOnly, (_req, res) => { | ||||||
|     res.json({ message: 'If you see this, you should be a teacher!' }); |     res.json({ message: 'If you see this, you should be a teacher!' }); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | // This endpoint is called by the client when the user has just logged in.
 | ||||||
|  | // It creates or updates the user entity based on the authentication data the endpoint was called with.
 | ||||||
| router.post('/hello', authenticatedOnly, postHelloHandler); | router.post('/hello', authenticatedOnly, postHelloHandler); | ||||||
| 
 | 
 | ||||||
| export default router; | export default router; | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gerald Schmittinger
						Gerald Schmittinger