From 54b70d96bb5e0b04786912d2155711e3b39c1825 Mon Sep 17 00:00:00 2001 From: simosathan9 Date: Wed, 11 Sep 2024 21:33:50 +0300 Subject: [PATCH] Improve login/register functionality --- passport-config.js | 71 +++----------------------------------- server.js | 86 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 77 insertions(+), 80 deletions(-) diff --git a/passport-config.js b/passport-config.js index baec5be..ec5e177 100644 --- a/passport-config.js +++ b/passport-config.js @@ -5,57 +5,6 @@ const GoogleStrategy = require('passport-google-oauth20').Strategy; const sqlite3 = require('sqlite3').verbose(); const db = new sqlite3.Database('./blockly_unix_database.db'); -// Google OAuth2 Strategy -passport.use( - new GoogleStrategy( - { - clientID: process.env.GOOGLE_CLIENT_ID, - clientSecret: process.env.GOOGLE_CLIENT_SECRET, - callbackURL: 'http://localhost:3000/auth/google/callback' - }, - function (accessToken, refreshToken, profile, done) { - db.get( - 'SELECT * FROM users WHERE googleId = ?', - [profile.id], - (err, row) => { - if (err) { - return done(err); - } - - if (!row) { - db.run( - `INSERT INTO users (googleId, username, email) VALUES (?, ?, ?)`, - [profile.id, profile.displayName, profile.emails[0].value], - function (err) { - if (err) { - return done(err); - } - - const newUserId = this.lastID; - db.get( - 'SELECT * FROM users WHERE id = ?', - [newUserId], - (err, newRow) => { - if (err) { - return done(err); - } - if (!newRow) { - return done(new Error('Failed to retrieve new user')); - } - return done(null, newRow); - } - ); - } - ); - } else { - return done(null, row); // Return the existing user - } - } - ); - } - ) -); - // Initialize function to set up LocalStrategy and Google OAuth function initialize(passport, getUserByUsername, getUserById) { // Local Strategy for login @@ -93,21 +42,11 @@ function initialize(passport, getUserByUsername, getUserById) { done(null, user.id); // Χρησιμοποίησε το `id` από τη βάση δεδομένων }); - passport.deserializeUser((googleId, done) => { - // Find user by Google ID in the database - db.get( - 'SELECT * FROM users WHERE googleId = ?', - [googleId], - (err, user) => { - if (err) { - return done(err); - } - if (!user) { - return done(new Error('User not found')); - } - done(null, user); // Return the user - } - ); + passport.deserializeUser((id, done) => { + getUserById(id, (err, user) => { + if (err) return done(err); + return done(null, user); + }); }); } module.exports = initialize; diff --git a/server.js b/server.js index 8abe0d4..69ba9b4 100644 --- a/server.js +++ b/server.js @@ -405,10 +405,6 @@ function checkNotAuthenticated(req, res, next) { next(); } -app.listen(3000, () => { - console.log('Server started on http://localhost:3000'); -}); - const GoogleStrategy = require('passport-google-oauth20').Strategy; app.use(passport.initialize()); @@ -421,23 +417,81 @@ passport.use( clientSecret: process.env.GOOGLE_CLIENT_SECRET, callbackURL: 'http://localhost:3000/auth/google/callback' }, - function (accessToken, refreshToken, profile, done) { + (accessToken, refreshToken, profile, done) => { + // Check if user with the given Google ID exists db.get( 'SELECT * FROM users WHERE googleId = ?', [profile.id], - (err, row) => { + (err, user) => { if (err) return done(err); - if (!row) { - db.run( - `INSERT INTO users (googleId, username, email) VALUES (?, ?, ?)`, - [profile.id, profile.displayName, profile.emails[0].value], - function (err) { + + if (user) { + // User already exists, log them in + return done(null, user); + } else { + // User does not exist, create a new user + db.get( + 'SELECT * FROM users WHERE email = ?', + [profile.emails[0].value], + (err, existingUser) => { if (err) return done(err); - return done(null, profile); + + if (existingUser) { + // Update the existing user with the Google ID + db.run( + 'UPDATE users SET googleId = ? WHERE email = ?', + [profile.id, profile.emails[0].value], + (err) => { + if (err) return done(err); + + // Fetch the updated user + db.get( + 'SELECT * FROM users WHERE email = ?', + [profile.emails[0].value], + (err, updatedUser) => { + if (err) return done(err); + done(null, updatedUser); + } + ); + } + ); + } else { + // Register a new user + const newUser = { + googleId: profile.id, + username: profile.displayName, + email: profile.emails[0].value + }; + + db.run( + 'INSERT INTO users (googleId, username, email) VALUES (?, ?, ?)', + [newUser.googleId, newUser.username, newUser.email], + function (err) { + if (err) return done(err); + + // Retrieve the newly created user + db.get( + 'SELECT * FROM users WHERE id = ?', + [this.lastID], + (err, createdUser) => { + if (err) return done(err); + + // Create default workspace for the new user + db.run( + 'INSERT INTO workspaces (workspaceData, userId, workspaceName) VALUES (?, ?, ?)', + ['{}', createdUser.id, '__autosave__'], + (err) => { + if (err) return done(err); + done(null, createdUser); + } + ); + } + ); + } + ); + } } ); - } else { - return done(null, row); } } ); @@ -459,3 +513,7 @@ app.get( res.redirect('/blockly_unix'); } ); + +app.listen(3000, () => { + console.log('Server started on http://localhost:3000'); +});