mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
simpleauth pour MODE=development
This commit is contained in:
parent
3e1e3c7f0d
commit
9aab6fa610
8 changed files with 38 additions and 19 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
// constants.tsx
|
// constants.tsx
|
||||||
const ENV_VARIABLES = {
|
const ENV_VARIABLES = {
|
||||||
MODE: 'production',
|
MODE: process.env.MODE || "production",
|
||||||
VITE_BACKEND_URL: process.env.VITE_BACKEND_URL || "",
|
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 || '',
|
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}`:''}` : ''
|
FRONTEND_URL: process.env.SITE_URL != undefined ? `${process.env.SITE_URL}${process.env.USE_PORTS ? `:${process.env.PORT}`:''}` : ''
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,9 @@ const Dashboard: React.FC = () => {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
if (!ApiService.isLoggedIn()) {
|
const isLoggedIn = await ApiService.isLoggedIn();
|
||||||
|
console.log(`Dashboard: isLoggedIn: ${isLoggedIn}`);
|
||||||
|
if (!isLoggedIn) {
|
||||||
navigate('/teacher/login');
|
navigate('/teacher/login');
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -247,10 +249,8 @@ const Dashboard: React.FC = () => {
|
||||||
// Otherwise the quiz is invalid
|
// Otherwise the quiz is invalid
|
||||||
for (let i = 0; i < questions.length; i++) {
|
for (let i = 0; i < questions.length; i++) {
|
||||||
try {
|
try {
|
||||||
// questions[i] = QuestionService.ignoreImgTags(questions[i]);
|
|
||||||
const parsedItem = parse(questions[i]);
|
const parsedItem = parse(questions[i]);
|
||||||
Template(parsedItem[0]);
|
Template(parsedItem[0]);
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error parsing question:', error);
|
console.error('Error parsing question:', error);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -200,15 +200,12 @@ class ApiService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns true if successful
|
|
||||||
* @returns A error string if unsuccessful,
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* @returns true if successful
|
* @returns true if successful
|
||||||
* @returns An error string if unsuccessful
|
* @returns An error string if unsuccessful
|
||||||
*/
|
*/
|
||||||
public async login(email: string, password: string): Promise<any> {
|
public async login(email: string, password: string): Promise<any> {
|
||||||
|
console.log(`login: email: ${email}, password: ${password}`);
|
||||||
try {
|
try {
|
||||||
if (!email || !password) {
|
if (!email || !password) {
|
||||||
throw new Error("L'email et le mot de passe sont requis.");
|
throw new Error("L'email et le mot de passe sont requis.");
|
||||||
|
|
@ -218,11 +215,16 @@ public async login(email: string, password: string): Promise<any> {
|
||||||
const headers = this.constructRequestHeaders();
|
const headers = this.constructRequestHeaders();
|
||||||
const body = { email, password };
|
const body = { email, password };
|
||||||
|
|
||||||
|
console.log(`login: POST ${url} body: ${JSON.stringify(body)}`);
|
||||||
const result: AxiosResponse = await axios.post(url, body, { headers: headers });
|
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 login is successful, redirect the user
|
||||||
if (result.status === 200) {
|
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;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`La connexion a échoué. Statut: ${result.status}`);
|
throw new Error(`La connexion a échoué. Statut: ${result.status}`);
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,13 @@ class AuthService {
|
||||||
|
|
||||||
async fetchAuthData(){
|
async fetchAuthData(){
|
||||||
try {
|
try {
|
||||||
|
// console.info(`MODE: ${ENV_VARIABLES.MODE}`);
|
||||||
|
// if (ENV_VARIABLES.MODE === 'development') {
|
||||||
|
// return { authActive: true };
|
||||||
|
// }
|
||||||
const response = await fetch(this.constructRequestUrl('/auth/getActiveAuth'));
|
const response = await fetch(this.constructRequestUrl('/auth/getActiveAuth'));
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
console.log('Data:', JSON.stringify(data));
|
||||||
return data.authActive;
|
return data.authActive;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erreur lors de la récupération des données d\'auth:', error);
|
console.error('Erreur lors de la récupération des données d\'auth:', error);
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ class AuthManager{
|
||||||
this.addModules()
|
this.addModules()
|
||||||
this.simpleregister = userModel;
|
this.simpleregister = userModel;
|
||||||
this.registerAuths()
|
this.registerAuths()
|
||||||
|
console.log(`AuthManager: constructor: this.configs: ${JSON.stringify(this.configs)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
getUserModel(){
|
getUserModel(){
|
||||||
|
|
@ -54,17 +55,22 @@ class AuthManager{
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
async login(userInfo,req,res,next){ //passport and simpleauth use next
|
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}`);
|
res.redirect(`/auth/callback?user=${tokenToSave}&username=${userInfo.name}`);
|
||||||
console.info(`L'utilisateur '${userInfo.name}' vient de se connecter`)
|
console.info(`L'utilisateur '${userInfo.name}' vient de se connecter`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
async loginSimple(email,pswd,req,res,next){ //passport and simpleauth use next
|
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 userInfo = await this.simpleregister.login(email, pswd);
|
||||||
const tokenToSave = jwt.create(userInfo.email, userInfo._id,userInfo.roles);
|
console.log(`auth-manager: loginSimple: userInfo: ${JSON.stringify(userInfo)}`);
|
||||||
res.redirect(`/auth/callback?user=${tokenToSave}&username=${userInfo.name}`);
|
userInfo.roles = ['teacher']; // hard coded role
|
||||||
console.info(`L'utilisateur '${userInfo.name}' vient de se connecter`)
|
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){
|
async register(userInfos){
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ class SimpleAuth {
|
||||||
}
|
}
|
||||||
|
|
||||||
async authenticate(self, req, res, next) {
|
async authenticate(self, req, res, next) {
|
||||||
|
console.log(`authenticate: ${JSON.stringify(req.body)}`);
|
||||||
try {
|
try {
|
||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
|
|
||||||
|
|
@ -54,6 +55,7 @@ class SimpleAuth {
|
||||||
}
|
}
|
||||||
|
|
||||||
await self.authmanager.loginSimple(email, password, req, res, next);
|
await self.authmanager.loginSimple(email, password, req, res, next);
|
||||||
|
// return res.status(200).json({ message: 'Logged in' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const statusCode = error.statusCode || 500;
|
const statusCode = error.statusCode || 500;
|
||||||
const message = error.message || "An internal server error occurred";
|
const message = error.message || "An internal server error occurred";
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,11 @@ class AuthConfig {
|
||||||
// Méthode pour lire le fichier de configuration JSON
|
// Méthode pour lire le fichier de configuration JSON
|
||||||
loadConfig() {
|
loadConfig() {
|
||||||
try {
|
try {
|
||||||
|
console.info(`Chargement du fichier de configuration: ${configPath}`);
|
||||||
const configData = fs.readFileSync(configPath, 'utf-8');
|
const configData = fs.readFileSync(configPath, 'utf-8');
|
||||||
this.config = JSON.parse(configData);
|
this.config = JSON.parse(configData);
|
||||||
} catch (error) {
|
} 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 = {};
|
this.config = {};
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
@ -139,6 +140,8 @@ class AuthConfig {
|
||||||
|
|
||||||
// Méthode pour retourner la configuration des fournisseurs PassportJS pour le frontend
|
// Méthode pour retourner la configuration des fournisseurs PassportJS pour le frontend
|
||||||
getActiveAuth() {
|
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) {
|
if (this.config && this.config.auth) {
|
||||||
const passportConfig = {};
|
const passportConfig = {};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,7 @@ class Users {
|
||||||
}
|
}
|
||||||
|
|
||||||
async login(email, password) {
|
async login(email, password) {
|
||||||
|
console.log(`models/users: login: email: ${email}, password: ${password}`);
|
||||||
try {
|
try {
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
|
@ -74,7 +75,7 @@ class Users {
|
||||||
error.statusCode = 401;
|
error.statusCode = 401;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
console.log(`models/users: login: FOUND user: ${JSON.stringify(user)}`);
|
||||||
return user;
|
return user;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue