Skip to content

Commit

Permalink
Improve login/register functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
simosathan9 committed Sep 11, 2024
1 parent 4481ed1 commit 54b70d9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 80 deletions.
71 changes: 5 additions & 66 deletions passport-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
86 changes: 72 additions & 14 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}
}
);
Expand All @@ -459,3 +513,7 @@ app.get(
res.redirect('/blockly_unix');
}
);

app.listen(3000, () => {
console.log('Server started on http://localhost:3000');
});

0 comments on commit 54b70d9

Please sign in to comment.