fix(backend): 409 Conflict bij het oproepen van /hello met een reeds geregistreerd account.

This commit is contained in:
Gerald Schmittinger 2025-04-29 23:06:58 +02:00
parent 04fd54e3d6
commit 0b9f765366
2 changed files with 6 additions and 34 deletions

View file

@ -2,10 +2,9 @@ import { UnauthorizedException } from '../exceptions/unauthorized-exception.js';
import { getLogger } from '../logging/initalize.js';
import { AuthenticatedRequest } from '../middleware/auth/authenticated-request.js';
import { envVars, getEnvVar } from '../util/envVars.js';
import { createOrUpdateStudent, createStudent } from '../services/students.js';
import { AuthenticationInfo } from '../middleware/auth/authentication-info.js';
import { createOrUpdateStudent } from '../services/students.js';
import { Request, Response } from 'express';
import { createOrUpdateTeacher, createTeacher } from '../services/teachers.js';
import { createOrUpdateTeacher } from '../services/teachers.js';
interface FrontendIdpConfig {
authority: string;
@ -45,31 +44,6 @@ export function handleGetFrontendAuthConfig(_req: Request, res: Response): void
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> {
const auth = req.auth;
if (!auth) {

View file

@ -1,15 +1,11 @@
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';
const router = express.Router();
// Returns auth configuration for frontend
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('/config', handleGetFrontendAuthConfig)
router.get('/testAuthenticatedOnly', authenticatedOnly, (_req, res) => {
/* #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!' });
});
// 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);
export default router;