mirror of
https://github.com/Alee14/bnbso-auth.git
synced 2025-01-22 10:41:57 -05:00
No longer using email, using discord usernames as email, check if discord id exists in db
This commit is contained in:
parent
70f071180c
commit
12669097d8
8 changed files with 43 additions and 35 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -173,3 +173,5 @@ dist
|
|||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
|
||||
*.db
|
45
index.js
45
index.js
|
@ -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(
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
/** @type {import('tailwindcss').Config} */
|
||||
export default {
|
||||
content: ["./views/**/*.{html,js,ejs}"],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
<% } %>
|
||||
|
|
|
@ -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') { %>
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue