diff --git a/client/src/constants.tsx b/client/src/constants.tsx index dccc503..ad5b80b 100644 --- a/client/src/constants.tsx +++ b/client/src/constants.tsx @@ -1,6 +1,6 @@ // constants.tsx const ENV_VARIABLES = { - MODE: 'production', + MODE: process.env.MODE || "production", VITE_BACKEND_URL: process.env.VITE_BACKEND_URL || "", BACKEND_URL: process.env.SITE_URL != undefined ? `${process.env.SITE_URL}${process.env.USE_PORTS ? `:${process.env.BACKEND_PORT}`:''}` : process.env.VITE_BACKEND_URL || '', FRONTEND_URL: process.env.SITE_URL != undefined ? `${process.env.SITE_URL}${process.env.USE_PORTS ? `:${process.env.PORT}`:''}` : '' diff --git a/client/src/pages/Teacher/Dashboard/Dashboard.tsx b/client/src/pages/Teacher/Dashboard/Dashboard.tsx index cbb9f68..cdae619 100644 --- a/client/src/pages/Teacher/Dashboard/Dashboard.tsx +++ b/client/src/pages/Teacher/Dashboard/Dashboard.tsx @@ -89,7 +89,9 @@ const Dashboard: React.FC = () => { useEffect(() => { const fetchData = async () => { - if (!ApiService.isLoggedIn()) { + const isLoggedIn = await ApiService.isLoggedIn(); + console.log(`Dashboard: isLoggedIn: ${isLoggedIn}`); + if (!isLoggedIn) { navigate('/teacher/login'); return; } else { @@ -247,10 +249,8 @@ const Dashboard: React.FC = () => { // Otherwise the quiz is invalid for (let i = 0; i < questions.length; i++) { try { - // questions[i] = QuestionService.ignoreImgTags(questions[i]); const parsedItem = parse(questions[i]); Template(parsedItem[0]); - // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { console.error('Error parsing question:', error); return false; diff --git a/client/src/services/ApiService.tsx b/client/src/services/ApiService.tsx index f1c4134..20b9fca 100644 --- a/client/src/services/ApiService.tsx +++ b/client/src/services/ApiService.tsx @@ -200,15 +200,12 @@ class ApiService { } } - /** - * @returns true if successful - * @returns A error string if unsuccessful, - */ - /** +/** * @returns true if successful * @returns An error string if unsuccessful */ public async login(email: string, password: string): Promise { + console.log(`login: email: ${email}, password: ${password}`); try { if (!email || !password) { throw new Error("L'email et le mot de passe sont requis."); @@ -218,11 +215,16 @@ public async login(email: string, password: string): Promise { const headers = this.constructRequestHeaders(); const body = { email, password }; + console.log(`login: POST ${url} body: ${JSON.stringify(body)}`); const result: AxiosResponse = await axios.post(url, body, { headers: headers }); + console.log(`login: result: ${result.status}, ${result.data}`); // If login is successful, redirect the user if (result.status === 200) { - window.location.href = result.request.responseURL; + //window.location.href = result.request.responseURL; + this.saveToken(result.data.token); + this.saveUsername(result.data.username); + window.location.href = '/teacher/dashboard'; return true; } else { throw new Error(`La connexion a échoué. Statut: ${result.status}`); @@ -1168,4 +1170,4 @@ public async login(email: string, password: string): Promise { } const apiService = new ApiService(); -export default apiService; \ No newline at end of file +export default apiService; diff --git a/client/src/services/AuthService.tsx b/client/src/services/AuthService.tsx index bca616d..050ac82 100644 --- a/client/src/services/AuthService.tsx +++ b/client/src/services/AuthService.tsx @@ -14,8 +14,13 @@ class AuthService { async fetchAuthData(){ try { + // console.info(`MODE: ${ENV_VARIABLES.MODE}`); + // if (ENV_VARIABLES.MODE === 'development') { + // return { authActive: true }; + // } const response = await fetch(this.constructRequestUrl('/auth/getActiveAuth')); const data = await response.json(); + console.log('Data:', JSON.stringify(data)); return data.authActive; } catch (error) { console.error('Erreur lors de la récupération des données d\'auth:', error); @@ -25,4 +30,4 @@ class AuthService { } const authService = new AuthService(); -export default authService; \ No newline at end of file +export default authService; diff --git a/server/auth/auth-manager.js b/server/auth/auth-manager.js index bdcc4d7..0233672 100644 --- a/server/auth/auth-manager.js +++ b/server/auth/auth-manager.js @@ -16,6 +16,7 @@ class AuthManager{ this.addModules() this.simpleregister = userModel; this.registerAuths() + console.log(`AuthManager: constructor: this.configs: ${JSON.stringify(this.configs)}`); } getUserModel(){ @@ -54,17 +55,22 @@ class AuthManager{ // eslint-disable-next-line no-unused-vars async login(userInfo,req,res,next){ //passport and simpleauth use next - const tokenToSave = jwt.create(userInfo.email, userInfo._id,userInfo.roles); + const tokenToSave = jwt.create(userInfo.email, userInfo._id, userInfo.roles); res.redirect(`/auth/callback?user=${tokenToSave}&username=${userInfo.name}`); console.info(`L'utilisateur '${userInfo.name}' vient de se connecter`) } // eslint-disable-next-line no-unused-vars async loginSimple(email,pswd,req,res,next){ //passport and simpleauth use next + console.log(`auth-manager: loginSimple: email: ${email}, pswd: ${pswd}`); const userInfo = await this.simpleregister.login(email, pswd); - const tokenToSave = jwt.create(userInfo.email, userInfo._id,userInfo.roles); - res.redirect(`/auth/callback?user=${tokenToSave}&username=${userInfo.name}`); - console.info(`L'utilisateur '${userInfo.name}' vient de se connecter`) + console.log(`auth-manager: loginSimple: userInfo: ${JSON.stringify(userInfo)}`); + userInfo.roles = ['teacher']; // hard coded role + const tokenToSave = jwt.create(userInfo.email, userInfo._id, userInfo.roles); + console.log(`auth-manager: loginSimple: tokenToSave: ${tokenToSave}`); + //res.redirect(`/auth/callback?user=${tokenToSave}&username=${userInfo.email}`); + res.status(200).json({token: tokenToSave}); + console.info(`L'utilisateur '${userInfo.email}' vient de se connecter`) } async register(userInfos){ diff --git a/server/auth/modules/simpleauth.js b/server/auth/modules/simpleauth.js index e2c9300..dde467b 100644 --- a/server/auth/modules/simpleauth.js +++ b/server/auth/modules/simpleauth.js @@ -44,6 +44,7 @@ class SimpleAuth { } async authenticate(self, req, res, next) { + console.log(`authenticate: ${JSON.stringify(req.body)}`); try { const { email, password } = req.body; @@ -54,6 +55,7 @@ class SimpleAuth { } await self.authmanager.loginSimple(email, password, req, res, next); + // return res.status(200).json({ message: 'Logged in' }); } catch (error) { const statusCode = error.statusCode || 500; const message = error.message || "An internal server error occurred"; @@ -120,4 +122,4 @@ class SimpleAuth { } -module.exports = SimpleAuth; \ No newline at end of file +module.exports = SimpleAuth; diff --git a/server/config/auth.js b/server/config/auth.js index 72e89ed..0bec1b1 100644 --- a/server/config/auth.js +++ b/server/config/auth.js @@ -12,10 +12,11 @@ class AuthConfig { // Méthode pour lire le fichier de configuration JSON loadConfig() { try { + console.info(`Chargement du fichier de configuration: ${configPath}`); const configData = fs.readFileSync(configPath, 'utf-8'); this.config = JSON.parse(configData); } catch (error) { - console.error("Erreur lors de la lecture du fichier de configuration. Ne pas se fier si vous n'avez pas mit de fichier de configuration."); + console.error("Erreur lors de la lecture du fichier de configuration. Ne pas se fier si vous n'avez pas mis de fichier de configuration."); this.config = {}; throw error; } @@ -139,6 +140,8 @@ class AuthConfig { // Méthode pour retourner la configuration des fournisseurs PassportJS pour le frontend getActiveAuth() { + console.log(`getActiveAuth: this.config: ${JSON.stringify(this.config)}`); + console.log(`getActiveAuth: this.config.auth: ${JSON.stringify(this.config.auth)}`); if (this.config && this.config.auth) { const passportConfig = {}; diff --git a/server/models/users.js b/server/models/users.js index d048d9f..2be1aa3 100644 --- a/server/models/users.js +++ b/server/models/users.js @@ -54,6 +54,7 @@ class Users { } async login(email, password) { + console.log(`models/users: login: email: ${email}, password: ${password}`); try { await this.db.connect(); const conn = this.db.getConnection(); @@ -74,7 +75,7 @@ class Users { error.statusCode = 401; throw error; } - + console.log(`models/users: login: FOUND user: ${JSON.stringify(user)}`); return user; } catch (error) { console.error(error);