feat(backend): '/auth/hello' endpoint toegevoegd

This commit is contained in:
Gerald Schmittinger 2025-04-19 11:27:51 +02:00
parent c2f3a6169a
commit 2b3f6b5e7a
6 changed files with 40 additions and 7 deletions

View 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 });
}
}

View file

@ -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 });
}