diff options
| author | Andrew Lee <andrew@alee14.me> | 2024-12-24 17:03:01 -0500 |
|---|---|---|
| committer | Andrew Lee <andrew@alee14.me> | 2024-12-24 17:03:01 -0500 |
| commit | 12669097d86af780fdf4bec629aa81ba15b7c116 (patch) | |
| tree | 0818ee433f1dc6fc1b7ee954c6565c1d74d483a1 | |
| parent | 70f071180c932e9cf5029b47ec5e0d1e2623b4b4 (diff) | |
| download | freeso-discord-auth-12669097d86af780fdf4bec629aa81ba15b7c116.tar.gz freeso-discord-auth-12669097d86af780fdf4bec629aa81ba15b7c116.tar.bz2 freeso-discord-auth-12669097d86af780fdf4bec629aa81ba15b7c116.zip | |
No longer using email, using discord usernames as email, check if discord id exists in db
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | index.js | 45 | ||||
| -rw-r--r-- | tailwind.config.js | 9 | ||||
| -rw-r--r-- | views/dashboard.ejs | 4 | ||||
| -rw-r--r-- | views/index.ejs | 2 | ||||
| -rw-r--r-- | views/password.ejs | 4 | ||||
| -rw-r--r-- | views/register.ejs | 11 | ||||
| -rw-r--r-- | views/success.ejs | 1 |
8 files changed, 43 insertions, 35 deletions
@@ -173,3 +173,5 @@ dist # Finder (MacOS) folder config .DS_Store + +*.db
\ No newline at end of file @@ -3,6 +3,7 @@ import session from "express-session"; import multer from "multer"; import passport from "passport"; import { Strategy as DiscordStrategy } from "passport-discord"; +import sqlite3 from 'sqlite3'; import path from "path"; import { fileURLToPath } from 'url'; import dotenv from "dotenv"; @@ -17,6 +18,7 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const statusMessages = JSON.parse(fs.readFileSync(path.join(__dirname, 'status.json'), 'utf8')); +const db = new sqlite3.Database('./database.db'); const upload = multer(); const app = express(); @@ -28,6 +30,11 @@ app.set('view engine', 'ejs'); app.use(express.static(path.join(__dirname, 'public'))); app.set('views', path.join(__dirname, 'views')); +db.run(`CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + discord_id TEXT NOT NULL, + fso_username TEXT NOT NULL +)`); // Passport session setup passport.serializeUser((user, done) => done(null, user)); @@ -40,7 +47,7 @@ passport.use( clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, callbackURL: process.env.REDIRECT_URI, - scope: ["identify", "email", "guilds"], + scope: ["identify", "guilds"], }, (accessToken, refreshToken, profile, done) => { return done(null, profile); @@ -66,14 +73,20 @@ app.get("/", async (req, res) => { const isInGuild = guilds.some((guild) => guild.id === process.env.GUILD_ID); if (isInGuild) { - let userExists = false; - if (userExists) { - return res.render('dashboard', req.user); + db.get(`SELECT * FROM users WHERE discord_id = ?`, [id], (err, row) => { + if (err) { + console.error("Error querying the database:", err); + return res.render('error', { error: 'An error occurred while checking user data.' }); + } + + if (row) { + return res.render('dashboard', { ...req.user, fso_username: row.fso_username }); } else { return res.render('register', req.user); } + }); } else { - return res.render('error', { error: 'You must be a member of the bits & Bytes server to access this page.' }); + return res.render('error', { error: 'You must be a member of the bits & Bytes server to access this page.' }); } } else { res.render('index'); @@ -82,18 +95,19 @@ app.get("/", async (req, res) => { app.post("/register", upload.none(), async (req, res) => { if (req.isAuthenticated()) { - const { id } = req.user; - const { username, email, password, password2 } = req.body; + const { username: discordUsername, id } = req.user; + const { username, password, passwordconfirm } = req.body; - if (password !== password2) { + if (password !== passwordconfirm) { return res.render('register', { ...req.user, error: "Passwords do not match" }); } try { const form = new FormData(); form.append('username', username); - form.append('email', email); + form.append('email', discordUsername + '@discord.com'); form.append('password', password); + form.append('key', process.env.REG_KEY); const response = await axios.post(`${process.env.API_URL}/userapi/registration`, form, { headers: form.getHeaders() @@ -104,12 +118,17 @@ app.post("/register", upload.none(), async (req, res) => { const errorMessage = statusMessages.registration_errors[errorKey] || "Something went wrong"; return res.render('register', { ...req.user, error: errorMessage }); } else { - console.log(`Discord ID: ${id}`) - return res.render('success'); + db.run(`INSERT INTO users (discord_id, fso_username) VALUES (?, ?)`, [id, username], function(err) { + if (err) { + console.error("Error inserting user data into database:", err); + return res.render('register', { ...req.user, error: "An error occurred during registration, contact server operator." }); + } + return res.render('success'); + }); } } catch (error) { console.error("Error during registration:", error); - return res.render('register', { ...req.user, error: "An error occurred during registration" }); + return res.render('register', { ...req.user, error: "An error occurred during registration, contact server operator." }); } } else { res.redirect("/"); @@ -118,7 +137,7 @@ app.post("/register", upload.none(), async (req, res) => { app.get( "/auth/discord", - passport.authenticate("discord", { scope: ["identify", "email", "guilds"] }) + passport.authenticate("discord", { scope: ["identify", "guilds"] }) ); app.get( diff --git a/tailwind.config.js b/tailwind.config.js deleted file mode 100644 index ddd5cd3..0000000 --- a/tailwind.config.js +++ /dev/null @@ -1,9 +0,0 @@ -/** @type {import('tailwindcss').Config} */ -export default { - content: ["./views/**/*.{html,js,ejs}"], - theme: { - extend: {}, - }, - plugins: [], -} - diff --git a/views/dashboard.ejs b/views/dashboard.ejs index b50f0e0..1c7996b 100644 --- a/views/dashboard.ejs +++ b/views/dashboard.ejs @@ -10,8 +10,8 @@ <div class="background"></div> <div class="container"> <img src="img/logo.png" alt="logo" width="200"> - <p>Welcome, <%= username %>!</p> - <p>FreeSO Username:</p> + <h1>Welcome, <%= username %>!</h1> + <h2>FreeSO Username: <%= fso_username %></h2> <a href="#" class="button">Change Password</a> <a href="#" class="button">Download bnbSO Client</a> <a href="/logout" class="button logout">Logout</a> diff --git a/views/index.ejs b/views/index.ejs index dc893d4..e786bd3 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -11,7 +11,7 @@ <div class="container"> <img src="img/logo.png" alt="logo" width="200"> <p>Log into your Discord account to get access to bnbSO.</p> - <p><i>You must be a bits & Bytes member to join.</i></p> + <p><i>You must be a bits & Bytes member.</i></p> <a class="button discord" href="/auth/discord">Login with Discord</a> </div> </body> diff --git a/views/password.ejs b/views/password.ejs index 2e24399..f66b60b 100644 --- a/views/password.ejs +++ b/views/password.ejs @@ -10,7 +10,8 @@ <div class="background"></div> <div class="container"> <img src="img/logo.png" alt="logo" width="200"> - <h1>Login</h1> + <h1>Change Password</h1> + <p>If you have issues changing your password, ask the server operator to change your password.</p> <form method="post" action="/login"> <label for="username">Username:</label> <input type="text" id="username" name="username"> @@ -18,7 +19,6 @@ <input type="password" id="password" name="password"> <button type="submit">Login</button> </form> - <a href="#">Forgot Password</a> <% if (typeof error !== 'undefined') { %> <div class="error"><%= error %></div> <% } %> diff --git a/views/register.ejs b/views/register.ejs index 66058c3..f417f82 100644 --- a/views/register.ejs +++ b/views/register.ejs @@ -12,17 +12,14 @@ <img src="img/logo.png" alt="logo" width="200"> <h1>Welcome to bnbSO!</h1> <p>You will be sending the following information to register your bnbSO account</p> - <p>Please verify that the following information is correct. You can only change this <b>once</b>.</p> - <p>Note: You can change your username and email if you want to change it.<br>It is there as your Discord account are using these credentials.</p> + <p>Please verify that the following information is correct. You can only change your username <b>once</b>.</p> <form method="post" action="/register"> <label for="username">Username:</label> - <input type="text" id="username" name="username" value="<%= username %>"> - <label for="email">Email:</label> - <input type="email" id="email" name="email" value="<%= email %>"> + <input type="text" id="username" name="username"> <label for="password">Password:</label> <input type="password" id="password" name="password"> - <label for="password2">Confirm Password:</label> - <input type="password" id="password2" name="password2"> + <label for="passwordconfirm">Confirm Password:</label> + <input type="password" id="passwordconfirm" name="passwordconfirm"> <button type="submit">Register</button> </form> <% if (typeof error !== 'undefined') { %> diff --git a/views/success.ejs b/views/success.ejs index 224b260..2ec9771 100644 --- a/views/success.ejs +++ b/views/success.ejs @@ -11,7 +11,6 @@ <div class="container"> <img src="img/logo.png" alt="logo" width="200"> <p>Created account successfully!</p> - <p>Check on your inbox for the confirmation code.</p> <a href="/">Dashboard</a> </div> </body> |
