fix: Fouten in implementatie van hello-mechanisme opgelost.
This commit is contained in:
		
							parent
							
								
									59569445c3
								
							
						
					
					
						commit
						57ff2daf6c
					
				
					 5 changed files with 31 additions and 12 deletions
				
			
		|  | @ -1,4 +1,5 @@ | ||||||
| import { UnauthorizedException } from '../exceptions/unauthorized-exception.js'; | import { UnauthorizedException } from '../exceptions/unauthorized-exception.js'; | ||||||
|  | import { getLogger } from '../logging/initalize.js'; | ||||||
| import { AuthenticatedRequest } from '../middleware/auth/authenticated-request.js'; | import { AuthenticatedRequest } from '../middleware/auth/authenticated-request.js'; | ||||||
| import { createOrUpdateStudent } from '../services/students.js'; | import { createOrUpdateStudent } from '../services/students.js'; | ||||||
| import { createOrUpdateTeacher } from '../services/teachers.js'; | import { createOrUpdateTeacher } from '../services/teachers.js'; | ||||||
|  | @ -20,6 +21,8 @@ interface FrontendAuthConfig { | ||||||
| const SCOPE = 'openid profile email'; | const SCOPE = 'openid profile email'; | ||||||
| const RESPONSE_TYPE = 'code'; | const RESPONSE_TYPE = 'code'; | ||||||
| 
 | 
 | ||||||
|  | const logger = getLogger(); | ||||||
|  | 
 | ||||||
| export function getFrontendAuthConfig(): FrontendAuthConfig { | export function getFrontendAuthConfig(): FrontendAuthConfig { | ||||||
|     return { |     return { | ||||||
|         student: { |         student: { | ||||||
|  | @ -37,7 +40,7 @@ export function getFrontendAuthConfig(): FrontendAuthConfig { | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 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) { | ||||||
|         throw new UnauthorizedException("Cannot say hello when not authenticated."); |         throw new UnauthorizedException("Cannot say hello when not authenticated."); | ||||||
|  | @ -49,8 +52,11 @@ export async function postHelloHandler(req: AuthenticatedRequest, _res: Response | ||||||
|         lastName: auth.lastName ?? '' |         lastName: auth.lastName ?? '' | ||||||
|     }; |     }; | ||||||
|     if (auth.accountType === "student") { |     if (auth.accountType === "student") { | ||||||
|         await createOrUpdateStudent({ ...userData }, { preventOverwrite: false }); |         await createOrUpdateStudent(userData); | ||||||
|  |         logger.debug(`Synchronized student ${userData.username} with IDP`); | ||||||
|     } else { |     } else { | ||||||
|         await createOrUpdateTeacher({ ...userData }, { preventOverwrite: false }); |         await createOrUpdateTeacher(userData); | ||||||
|  |         logger.debug(`Synchronized teacher ${userData.username} with IDP`); | ||||||
|     } |     } | ||||||
|  |     res.status(200).send({ message: "Welcome!" }); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -9,7 +9,7 @@ export function errorHandler(err: unknown, _req: Request, res: Response, _: Next | ||||||
|         logger.warn(`An error occurred while handling a request: ${err} (-> HTTP ${err.status})`); |         logger.warn(`An error occurred while handling a request: ${err} (-> HTTP ${err.status})`); | ||||||
|         res.status(err.status).json(err); |         res.status(err.status).json(err); | ||||||
|     } else { |     } else { | ||||||
|         logger.error(`Unexpected error occurred while handing a request: ${JSON.stringify(err)}`); |         logger.error(`Unexpected error occurred while handing a request: ${err}`); | ||||||
|         res.status(500).json(err); |         res.status(500).json(err); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -53,16 +53,22 @@ export async function getStudent(username: string): Promise<StudentDTO> { | ||||||
|     return mapToStudentDTO(user); |     return mapToStudentDTO(user); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function createOrUpdateStudent(userData: StudentDTO, options?: { preventOverwrite: boolean}): Promise<StudentDTO> { | export async function createStudent(userData: StudentDTO): Promise<StudentDTO> { | ||||||
|     const studentRepository = getStudentRepository(); |     const studentRepository = getStudentRepository(); | ||||||
| 
 | 
 | ||||||
|     const newStudent = mapToStudent(userData); |     const newStudent = mapToStudent(userData); | ||||||
|     await studentRepository.save(newStudent, { preventOverwrite: options?.preventOverwrite ?? true }); |     await studentRepository.save(newStudent); | ||||||
|  | 
 | ||||||
|     return userData; |     return userData; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function createStudent(userData: StudentDTO): Promise<StudentDTO> { | export async function createOrUpdateStudent(userData: StudentDTO): Promise<StudentDTO> { | ||||||
|     return createOrUpdateStudent(userData, { preventOverwrite: true }); |     await getStudentRepository().upsert({ | ||||||
|  |         username: userData.username, | ||||||
|  |         firstName: userData.firstName, | ||||||
|  |         lastName: userData.lastName | ||||||
|  |     }); | ||||||
|  |     return userData; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function deleteStudent(username: string): Promise<StudentDTO> { | export async function deleteStudent(username: string): Promise<StudentDTO> { | ||||||
|  |  | ||||||
|  | @ -57,16 +57,22 @@ export async function getTeacher(username: string): Promise<TeacherDTO> { | ||||||
|     return mapToTeacherDTO(user); |     return mapToTeacherDTO(user); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function createOrUpdateTeacher(userData: TeacherDTO, options?: { preventOverwrite?: boolean }): Promise<TeacherDTO> { | export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO> { | ||||||
|     const teacherRepository: TeacherRepository = getTeacherRepository(); |     const teacherRepository: TeacherRepository = getTeacherRepository(); | ||||||
| 
 | 
 | ||||||
|     const newTeacher = mapToTeacher(userData); |     const newTeacher = mapToTeacher(userData); | ||||||
|     await teacherRepository.save(newTeacher, { preventOverwrite: options?.preventOverwrite ?? true }); |     await teacherRepository.save(newTeacher); | ||||||
|  | 
 | ||||||
|     return mapToTeacherDTO(newTeacher); |     return mapToTeacherDTO(newTeacher); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO> { | export async function createOrUpdateTeacher(userData: TeacherDTO): Promise<TeacherDTO> { | ||||||
|     return await createOrUpdateTeacher(userData, { preventOverwrite: true }); |     await getTeacherRepository().upsert({ | ||||||
|  |         username: userData.username, | ||||||
|  |         firstName: userData.firstName, | ||||||
|  |         lastName: userData.lastName | ||||||
|  |     }); | ||||||
|  |     return userData; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function deleteTeacher(username: string): Promise<TeacherDTO> { | export async function deleteTeacher(username: string): Promise<TeacherDTO> { | ||||||
|  |  | ||||||
|  | @ -92,6 +92,7 @@ async function handleLoginCallback(): Promise<void> { | ||||||
|         throw new Error("Login callback received, but the user is not logging in!"); |         throw new Error("Login callback received, but the user is not logging in!"); | ||||||
|     } |     } | ||||||
|     authState.user = (await (await getUserManagers())[activeRole].signinCallback()) || null; |     authState.user = (await (await getUserManagers())[activeRole].signinCallback()) || null; | ||||||
|  |     await apiClient.post("/auth/hello"); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gerald Schmittinger
						Gerald Schmittinger