Initial Commit
This commit is contained in:
parent
3eb657bf6f
commit
d2d0977774
6 changed files with 2562 additions and 0 deletions
66
app.js
Normal file
66
app.js
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
const DISCORD_CLIENT_ID = "your_client_id";
|
||||
const DISCORD_CLIENT_SECRET = "your_client_secret";
|
||||
const DISCORD_CALLBACK_URL = "http://localhost:3000/auth/discord/callback";
|
||||
const SESSION_SECRET = "your_session_secret";
|
||||
|
||||
// ...existing code...
|
||||
|
||||
const express = require('express');
|
||||
const session = require('express-session');
|
||||
const passport = require('passport');
|
||||
const DiscordStrategy = require('passport-discord').Strategy;
|
||||
const SQLite = require('sqlite3').verbose();
|
||||
const path = require('path');
|
||||
const routes = require('./routes');
|
||||
|
||||
const app = express();
|
||||
|
||||
// DB SQLite
|
||||
const db = new SQLite.Database('./database.sqlite', (err) => {
|
||||
if (err) console.error('Erreur SQLite:', err);
|
||||
else db.run('CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY, username TEXT, discriminator TEXT, avatar TEXT)');
|
||||
});
|
||||
|
||||
// Sessions
|
||||
app.use(session({
|
||||
secret: SESSION_SECRET,
|
||||
resave: false,
|
||||
saveUninitialized: false
|
||||
}));
|
||||
|
||||
// Passport config
|
||||
passport.serializeUser((user, done) => done(null, user.id));
|
||||
passport.deserializeUser((id, done) => {
|
||||
db.get('SELECT * FROM users WHERE id = ?', [id], (err, row) => {
|
||||
if (err) return done(err);
|
||||
done(null, row);
|
||||
});
|
||||
});
|
||||
|
||||
passport.use(new DiscordStrategy({
|
||||
clientID: DISCORD_CLIENT_ID,
|
||||
clientSecret: DISCORD_CLIENT_SECRET,
|
||||
callbackURL: DISCORD_CALLBACK_URL,
|
||||
scope: ['identify']
|
||||
}, (accessToken, refreshToken, profile, done) => {
|
||||
db.run('INSERT OR REPLACE INTO users (id, username, discriminator, avatar) VALUES (?, ?, ?, ?)',
|
||||
[profile.id, profile.username, profile.discriminator, profile.avatar],
|
||||
(err) => {
|
||||
if (err) return done(err);
|
||||
done(null, profile);
|
||||
}
|
||||
);
|
||||
}));
|
||||
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
// EJS
|
||||
app.set('view engine', 'ejs');
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
|
||||
// Routes
|
||||
app.use('/', routes);
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => console.log('Serveur lancé sur http://localhost:' + PORT));
|
||||
BIN
database.sqlite
Normal file
BIN
database.sqlite
Normal file
Binary file not shown.
2428
package-lock.json
generated
Normal file
2428
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
26
package.json
Normal file
26
package.json
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"name": "resto-epi",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "ssh://git@git.lehub.tf:2222/SchoolTask/resto-epi.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "commonjs",
|
||||
"dependencies": {
|
||||
"dotenv": "^17.2.1",
|
||||
"ejs": "^3.1.10",
|
||||
"express": "^5.1.0",
|
||||
"express-session": "^1.18.2",
|
||||
"passport": "^0.7.0",
|
||||
"passport-discord": "^0.1.4",
|
||||
"sqlite3": "^5.1.7"
|
||||
}
|
||||
}
|
||||
27
routes.js
Normal file
27
routes.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
const express = require('express');
|
||||
const passport = require('passport');
|
||||
const router = express.Router();
|
||||
|
||||
// Page d'accueil
|
||||
router.get('/', (req, res) => {
|
||||
res.render('index', { user: req.user });
|
||||
});
|
||||
|
||||
// Auth Discord
|
||||
router.get('/auth/discord', passport.authenticate('discord'));
|
||||
|
||||
router.get('/auth/discord/callback',
|
||||
passport.authenticate('discord', { failureRedirect: '/' }),
|
||||
(req, res) => {
|
||||
res.redirect('/');
|
||||
}
|
||||
);
|
||||
|
||||
// Déconnexion
|
||||
router.get('/logout', (req, res) => {
|
||||
req.logout(() => {
|
||||
res.redirect('/');
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
15
views/index.ejs
Normal file
15
views/index.ejs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Accueil</title>
|
||||
</head>
|
||||
<body>
|
||||
<% if (user) { %>
|
||||
<h1>Bienvenue, <%= user.username %>!</h1>
|
||||
<a href="/logout">Déconnexion</a>
|
||||
<% } else { %>
|
||||
<h1>Bienvenue sur Resto-EPI</h1>
|
||||
<a href="/auth/discord">Connexion/Inscription via Discord</a>
|
||||
<% } %>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Reference in a new issue