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
|
||||
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}`:''}` : ''
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -202,13 +202,10 @@ 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<any> {
|
||||
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<any> {
|
|||
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}`);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ class AuthManager{
|
|||
this.addModules()
|
||||
this.simpleregister = userModel;
|
||||
this.registerAuths()
|
||||
console.log(`AuthManager: constructor: this.configs: ${JSON.stringify(this.configs)}`);
|
||||
}
|
||||
|
||||
getUserModel(){
|
||||
|
|
@ -61,10 +62,15 @@ class AuthManager{
|
|||
|
||||
// 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);
|
||||
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);
|
||||
res.redirect(`/auth/callback?user=${tokenToSave}&username=${userInfo.name}`);
|
||||
console.info(`L'utilisateur '${userInfo.name}' vient de se connecter`)
|
||||
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){
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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 = {};
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue