feat(backend): '/auth/hello' endpoint toegevoegd
This commit is contained in:
		
							parent
							
								
									c2f3a6169a
								
							
						
					
					
						commit
						2b3f6b5e7a
					
				
					 6 changed files with 40 additions and 7 deletions
				
			
		
							
								
								
									
										0
									
								
								backend/dwengo
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								backend/dwengo
									
										
									
									
									
										Normal file
									
								
							|  | @ -1,4 +1,9 @@ | |||
| import { UnauthorizedException } from '../exceptions/unauthorized-exception.js'; | ||||
| import { AuthenticatedRequest } from '../middleware/auth/authenticated-request.js'; | ||||
| import { createOrUpdateStudent } from '../services/students.js'; | ||||
| import { createOrUpdateTeacher } from '../services/teachers.js'; | ||||
| import { envVars, getEnvVar } from '../util/envVars.js'; | ||||
| import { Response } from "express"; | ||||
| 
 | ||||
| interface FrontendIdpConfig { | ||||
|     authority: string; | ||||
|  | @ -31,3 +36,21 @@ export function getFrontendAuthConfig(): FrontendAuthConfig { | |||
|         }, | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| export async function postHelloHandler(req: AuthenticatedRequest, _res: Response): Promise<void> { | ||||
|     const auth = req.auth; | ||||
|     if (!auth) { | ||||
|         throw new UnauthorizedException("Cannot say hello when not authenticated."); | ||||
|     } | ||||
|     const userData = { | ||||
|         id: auth.username, | ||||
|         username: auth.username, | ||||
|         firstName: auth.firstName ?? '', | ||||
|         lastName: auth.lastName ?? '' | ||||
|     }; | ||||
|     if (auth.accountType === "student") { | ||||
|         await createOrUpdateStudent({ ...userData }, { preventOverwrite: false }); | ||||
|     } else { | ||||
|         await createOrUpdateTeacher({ ...userData }, { preventOverwrite: false }); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -1,7 +1,7 @@ | |||
| import { Request, Response } from 'express'; | ||||
| import { | ||||
|     createClassJoinRequest, | ||||
|     createStudent, | ||||
|     createOrUpdateStudent, | ||||
|     deleteClassJoinRequest, | ||||
|     deleteStudent, | ||||
|     getAllStudents, | ||||
|  | @ -42,7 +42,7 @@ export async function createStudentHandler(req: Request, res: Response): Promise | |||
| 
 | ||||
|     const userData = req.body as StudentDTO; | ||||
| 
 | ||||
|     const student = await createStudent(userData); | ||||
|     const student = await createOrUpdateStudent(userData); | ||||
|     res.json({ student }); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,5 +1,5 @@ | |||
| import express from 'express'; | ||||
| import { getFrontendAuthConfig } from '../controllers/auth.js'; | ||||
| import { getFrontendAuthConfig, postHelloHandler } from '../controllers/auth.js'; | ||||
| import { authenticatedOnly, studentsOnly, teachersOnly } from '../middleware/auth/auth.js'; | ||||
| const router = express.Router(); | ||||
| 
 | ||||
|  | @ -23,4 +23,6 @@ router.get('/testTeachersOnly', teachersOnly, (_req, res) => { | |||
|     res.json({ message: 'If you see this, you should be a teacher!' }); | ||||
| }); | ||||
| 
 | ||||
| router.post('/hello', authenticatedOnly, postHelloHandler); | ||||
| 
 | ||||
| export default router; | ||||
|  |  | |||
|  | @ -53,14 +53,18 @@ export async function getStudent(username: string): Promise<StudentDTO> { | |||
|     return mapToStudentDTO(user); | ||||
| } | ||||
| 
 | ||||
| export async function createStudent(userData: StudentDTO): Promise<StudentDTO> { | ||||
| export async function createOrUpdateStudent(userData: StudentDTO, options?: { preventOverwrite: boolean}): Promise<StudentDTO> { | ||||
|     const studentRepository = getStudentRepository(); | ||||
| 
 | ||||
|     const newStudent = mapToStudent(userData); | ||||
|     await studentRepository.save(newStudent, { preventOverwrite: true }); | ||||
|     await studentRepository.save(newStudent, { preventOverwrite: options?.preventOverwrite ?? true }); | ||||
|     return userData; | ||||
| } | ||||
| 
 | ||||
| export async function createStudent(userData: StudentDTO): Promise<StudentDTO> { | ||||
|     return createOrUpdateStudent(userData, { preventOverwrite: true }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteStudent(username: string): Promise<StudentDTO> { | ||||
|     const studentRepository = getStudentRepository(); | ||||
| 
 | ||||
|  |  | |||
|  | @ -57,14 +57,18 @@ export async function getTeacher(username: string): Promise<TeacherDTO> { | |||
|     return mapToTeacherDTO(user); | ||||
| } | ||||
| 
 | ||||
| export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO> { | ||||
| export async function createOrUpdateTeacher(userData: TeacherDTO, options?: { preventOverwrite?: boolean }): Promise<TeacherDTO> { | ||||
|     const teacherRepository: TeacherRepository = getTeacherRepository(); | ||||
| 
 | ||||
|     const newTeacher = mapToTeacher(userData); | ||||
|     await teacherRepository.save(newTeacher, { preventOverwrite: true }); | ||||
|     await teacherRepository.save(newTeacher, { preventOverwrite: options?.preventOverwrite ?? true }); | ||||
|     return mapToTeacherDTO(newTeacher); | ||||
| } | ||||
| 
 | ||||
| export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO> { | ||||
|     return await createOrUpdateTeacher(userData, { preventOverwrite: true }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteTeacher(username: string): Promise<TeacherDTO> { | ||||
|     const teacherRepository: TeacherRepository = getTeacherRepository(); | ||||
| 
 | ||||
|  |  | |||
		Reference in a new issue
	
	 Gerald Schmittinger
						Gerald Schmittinger