Chaos management

This commit is contained in:
MathieuSevignyLavallee 2024-09-30 23:05:00 -04:00
parent b9af549624
commit b1e26d7895
12 changed files with 120 additions and 15 deletions

View file

@ -23,13 +23,16 @@ import Header from './components/Header/Header';
import Footer from './components/Footer/Footer'; import Footer from './components/Footer/Footer';
import ApiService from './services/ApiService'; import ApiService from './services/ApiService';
import OAuthCallback from './pages/AuthSelection/AuthCallback';
const handleLogout = () => { const handleLogout = () => {
ApiService.logout(); ApiService.logout();
}; };
const isLoggedIn = () => { const isLoggedIn = () => {
return ApiService.isLoggedIn(); const test = ApiService.isLoggedIn();
console.log("App.tsx: " + test);
return test;
}; };
function App() { function App() {
@ -71,6 +74,9 @@ function App() {
{/* Pages authentification sélection */} {/* Pages authentification sélection */}
<Route path="/auth-selection" element={<AuthSelection />} /> <Route path="/auth-selection" element={<AuthSelection />} />
{/* Pages authentification sélection */}
<Route path="/oauth/callback" element={<OAuthCallback />} />
</Routes> </Routes>
</main> </main>
</div> </div>

View file

@ -1,4 +1,4 @@
import { useNavigate } from 'react-router-dom'; import { Link, useNavigate } from 'react-router-dom';
import * as React from 'react'; import * as React from 'react';
import './header.css'; import './header.css';
import { Button } from '@mui/material'; import { Button } from '@mui/material';
@ -32,6 +32,14 @@ const Header: React.FC<HeaderProps> = ({ isLoggedIn, handleLogout }) => {
Logout Logout
</Button> </Button>
)} )}
{!isLoggedIn() && (
<div className="auth-selection-btn">
<Link to="/auth-selection">
<button className="auth-btn">Connexion</button>
</Link>
</div>
)}
</div> </div>
); );
}; };

View file

@ -0,0 +1,27 @@
import { useEffect } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import apiService from '../../services/ApiService';
const OAuthCallback: React.FC = () => {
const navigate = useNavigate();
const location = useLocation();
useEffect(() => {
const searchParams = new URLSearchParams(location.search);
const user = searchParams.get('user');
if (user) {
// Save user data to localStorage or sessionStorage
apiService.saveToken(user);
// Navigate to the dashboard or another page
navigate('/');
} else {
navigate('/auth-selection');
}
}, [location, navigate]);
return <div>Loading...</div>;
};
export default OAuthCallback;

View file

@ -33,7 +33,7 @@ const SimpleLogin: React.FC = () => {
return; return;
} }
else { else {
navigate("/teacher/Dashboard") navigate("/")
} }
}; };

View file

@ -6,13 +6,6 @@ import { Link } from 'react-router-dom';
const Home: React.FC = () => { const Home: React.FC = () => {
return ( return (
<div className="page"> <div className="page">
<div className="auth-selection-btn">
<Link to="/auth-selection">
<button className="auth-btn">Connexion</button>
</Link>
</div>
<div className="btn-container"> <div className="btn-container">
<Link to="/student/join-room" className="student-btn"> <Link to="/student/join-room" className="student-btn">

View file

@ -32,7 +32,7 @@ class ApiService {
} }
// Helpers // Helpers
private saveToken(token: string): void { public saveToken(token: string): void {
const now = new Date(); const now = new Date();
const object = { const object = {

View file

@ -68,7 +68,6 @@ app.use(session({
})); }));
authManager = new AuthManager(app) authManager = new AuthManager(app)
app.use(errorHandler) app.use(errorHandler)
// Start server // Start server

View file

@ -56,6 +56,8 @@ class PassportOAuth {
(req, res) => { (req, res) => {
if (req.user) { if (req.user) {
res.json(req.user) res.json(req.user)
//const redirectUrl = `http://your-frontend-url.com/oauth/callback?user=${encodeURIComponent(req.user)}`;
//res.redirect(redirectUrl);
console.info(`L'utilisateur '${req.user.name}' vient de se connecter`) console.info(`L'utilisateur '${req.user.name}' vient de se connecter`)
} else { } else {
res.status(401).json({ error: "L'authentification a échoué" }); res.status(401).json({ error: "L'authentification a échoué" });

View file

@ -1,5 +1,6 @@
const fs = require('fs'); const fs = require('fs');
var passport = require('passport') var passport = require('passport')
var authprovider = require('../../models/authProvider')
class PassportJs{ class PassportJs{
constructor(authmanager,settings){ constructor(authmanager,settings){
@ -9,10 +10,10 @@ class PassportJs{
this.endpoint = "/api/auth" this.endpoint = "/api/auth"
} }
registerAuth(expressapp){ async registerAuth(expressapp){
expressapp.use(passport.initialize()); expressapp.use(passport.initialize());
expressapp.use(passport.session()); expressapp.use(passport.session());
for(const p of this.providers){ for(const p of this.providers){
for(const [name,provider] of Object.entries(p)){ for(const [name,provider] of Object.entries(p)){
if(!(provider.type in this.registeredProviders)){ if(!(provider.type in this.registeredProviders)){
@ -20,6 +21,9 @@ class PassportJs{
} }
try{ try{
this.registeredProviders[provider.type].register(expressapp,passport,this.endpoint,name,provider) this.registeredProviders[provider.type].register(expressapp,passport,this.endpoint,name,provider)
const auth_id = `passportjs_${provider.type}_${name}`
authprovider.create(auth_id)
} catch(error){ } catch(error){
console.error(`La connexion ${name} de type ${provider.type} n'as pu être chargé.`) console.error(`La connexion ${name} de type ${provider.type} n'as pu être chargé.`)
} }
@ -35,7 +39,7 @@ class PassportJs{
}); });
} }
registerProvider(providerType){ async registerProvider(providerType){
try{ try{
const providerPath = `${process.cwd()}/auth/modules/passport-providers/${providerType}.js` const providerPath = `${process.cwd()}/auth/modules/passport-providers/${providerType}.js`
const Provider = require(providerPath); const Provider = require(providerPath);
@ -45,6 +49,15 @@ class PassportJs{
console.error(`Le type de connexion '${providerType}' n'as pas pu être chargé dans passportjs.`) console.error(`Le type de connexion '${providerType}' n'as pas pu être chargé dans passportjs.`)
} }
} }
register(){
}
authenticate(){
}
} }

View file

@ -0,0 +1,30 @@
const db = require('../config/db.js')
const { ObjectId } = require('mongodb');
class AuthProvider {
constructor(name) {
this._id = new ObjectId();
this.name = name;
}
async create(name) {
await db.connect()
const conn = db.getConnection();
const collection = conn.collection('authprovider');
const existingauth = await collection.findOne({ name:name });
if(foldersCollection){
return existingauth._id;
}
const newProvider = {
name:name
}
const result = await foldersCollection.insertOne(newProvider);
return result.insertedId;
}
}
module.exports = new AuthProvider;

View file

@ -0,0 +1,14 @@
const db = require('../config/db.js')
const { ObjectId } = require('mongodb');
class AuthUserAssociation {
constructor(authProviderId, authId, userId) {
this._id = new ObjectId();
this.authProvider_id = authProviderId;
this.auth_id = authId;
this.user_id = userId;
}
}
module.exports = new AuthUserAssociation;

View file

@ -0,0 +1,13 @@
const db = require('../config/db.js')
const { ObjectId } = require('mongodb');
class AuthUserAssoc {
constructor(authProviderId, authId, userId) {
this._id = new ObjectId();
this.authProvider_id = authProviderId;
this.auth_id = authId;
this.user_id = userId;
}
}