From 26d5c09bb4ab0663452095d0fa1a17bdb0bb9f75 Mon Sep 17 00:00:00 2001 From: Gerald Schmittinger Date: Sun, 2 Mar 2025 21:02:17 +0100 Subject: [PATCH] chore(frontend): Refactoring Refactoring zodat de structuur van de authenticatieservice in de client duidelijker is. --- backend/.env.development.example | 4 +- backend/src/app.ts | 2 +- backend/src/middleware/cors.ts | 3 +- backend/src/routes/auth.ts | 13 +++ backend/src/util/envvars.ts | 4 +- frontend/src/App.vue | 4 +- frontend/src/config.ts | 19 +--- frontend/src/router/index.ts | 2 +- frontend/src/services/api-client.ts | 3 +- frontend/src/services/auth-service.ts | 106 ------------------ .../auth/auth-api-client-interceptors.ts | 24 ++++ .../src/services/auth/auth-config-loader.ts | 24 ++++ frontend/src/services/auth/auth-service.ts | 77 +++++++++++++ frontend/src/services/auth/auth-storage.ts | 13 +++ frontend/src/services/auth/auth-types.ts | 22 ++++ frontend/src/store/auth-store.ts | 40 ------- .../views/{discussions => }/CallbackPage.vue | 4 +- frontend/src/views/HomePage.vue | 19 +++- frontend/src/views/LoginPage.vue | 15 ++- 19 files changed, 215 insertions(+), 183 deletions(-) delete mode 100644 frontend/src/services/auth-service.ts create mode 100644 frontend/src/services/auth/auth-api-client-interceptors.ts create mode 100644 frontend/src/services/auth/auth-config-loader.ts create mode 100644 frontend/src/services/auth/auth-service.ts create mode 100644 frontend/src/services/auth/auth-storage.ts create mode 100644 frontend/src/services/auth/auth-types.ts delete mode 100644 frontend/src/store/auth-store.ts rename frontend/src/views/{discussions => }/CallbackPage.vue (80%) diff --git a/backend/.env.development.example b/backend/.env.development.example index f809129d..247ff054 100644 --- a/backend/.env.development.example +++ b/backend/.env.development.example @@ -12,5 +12,5 @@ DWENGO_AUTH_TEACHER_URL=http://localhost:7080/realms/teacher DWENGO_AUTH_TEACHER_CLIENT_ID=dwengo DWENGO_AUTH_TEACHER_JWKS_ENDPOINT=http://localhost:7080/realms/teacher/protocol/openid-connect/certs -# Allow frontend from anywhere to access the backend (for testing purposes). Don't forget to remove this in production! -DWENGO_CORS_ALLOWED_ORIGINS=* +# Allow Vite dev-server to access the backend (for testing purposes). Don't forget to remove this in production! +DWENGO_CORS_ALLOWED_ORIGINS=http://localhost:5173 diff --git a/backend/src/app.ts b/backend/src/app.ts index 250a6ebe..deb22095 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -24,8 +24,8 @@ app.get('/', (_, res: Response) => { }); }); -app.use(authenticateUser); app.use(cors); +app.use(authenticateUser); app.use('/student', studentRouter); app.use('/group', groupRouter); diff --git a/backend/src/middleware/cors.ts b/backend/src/middleware/cors.ts index e246aadf..6f59f600 100644 --- a/backend/src/middleware/cors.ts +++ b/backend/src/middleware/cors.ts @@ -2,5 +2,6 @@ import cors from "cors"; import {EnvVars, getEnvVar} from "../util/envvars"; export default cors({ - origin: getEnvVar(EnvVars.CorsAllowedOrigins).split(',') + origin: getEnvVar(EnvVars.CorsAllowedOrigins).split(','), + allowedHeaders: getEnvVar(EnvVars.CorsAllowedHeaders).split(',') }); diff --git a/backend/src/routes/auth.ts b/backend/src/routes/auth.ts index 87c4183c..14f9364b 100644 --- a/backend/src/routes/auth.ts +++ b/backend/src/routes/auth.ts @@ -1,5 +1,6 @@ import express from 'express' import {getFrontendAuthConfig} from "../controllers/auth"; +import {authenticatedOnly, studentsOnly, teachersOnly} from "../middleware/auth/auth"; const router = express.Router(); // returns auth configuration for frontend @@ -7,4 +8,16 @@ router.get('/config', (req, res) => { res.json(getFrontendAuthConfig()); }); +router.get('/testAuthenticatedOnly', authenticatedOnly, (req, res) => { + res.json({message: "If you see this, you should be authenticated!"}); +}); + +router.get('/testStudentsOnly', studentsOnly, (req, res) => { + res.json({message: "If you see this, you should be a student!"}); +}); + +router.get('/testTeachersOnly', teachersOnly, (req, res) => { + res.json({message: "If you see this, you should be a teacher!"}); +}); + export default router; diff --git a/backend/src/util/envvars.ts b/backend/src/util/envvars.ts index 60aa8025..b5142e58 100644 --- a/backend/src/util/envvars.ts +++ b/backend/src/util/envvars.ts @@ -3,6 +3,7 @@ const DB_PREFIX = PREFIX + 'DB_'; const IDP_PREFIX = PREFIX + 'AUTH_'; const STUDENT_IDP_PREFIX = IDP_PREFIX + 'STUDENT_'; const TEACHER_IDP_PREFIX = IDP_PREFIX + 'TEACHER_'; +const CORS_PREFIX = PREFIX + 'CORS_'; type EnvVar = { key: string; required?: boolean; defaultValue?: any }; @@ -21,7 +22,8 @@ export const EnvVars: { [key: string]: EnvVar } = { IdpTeacherClientId: { key: TEACHER_IDP_PREFIX + 'CLIENT_ID', required: true }, IdpTeacherJwksEndpoint: { key: TEACHER_IDP_PREFIX + 'JWKS_ENDPOINT', required: true }, IdpAudience: { key: IDP_PREFIX + 'AUDIENCE', defaultValue: 'account' }, - CorsAllowedOrigins: { key: PREFIX + 'CORS_ALLOWED_ORIGINS', defaultValue: ''} + CorsAllowedOrigins: { key: CORS_PREFIX + 'ALLOWED_ORIGINS', defaultValue: ''}, + CorsAllowedHeaders: { key: CORS_PREFIX + 'ALLOWED_HEADERS', defaultValue: 'Authorization,Content-Type'} } as const; /** diff --git a/frontend/src/App.vue b/frontend/src/App.vue index c1eb520c..20f0bd99 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,6 +1,6 @@