Skip to content

Commit

Permalink
Improve login functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
foivospro committed Sep 11, 2024
1 parent 8ab4d69 commit 8505642
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Binary file modified blockly_unix_database.db
Binary file not shown.
24 changes: 12 additions & 12 deletions passport-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,42 @@ passport.use(
callbackURL: 'http://localhost:3000/auth/google/callback'
},
function (accessToken, refreshToken, profile, done) {
console.log('Google profile:', profile); // Log the profile for debugging

// Find user by Google ID in the database
db.get(
'SELECT * FROM users WHERE googleId = ?',
[profile.id],
(err, row) => {
if (err) return done(err);
if (err) {
return done(err);
}

if (!row) {
// Insert a new user if one does not exist
db.run(
`INSERT INTO users (googleId, username, email) VALUES (?, ?, ?)`,
[profile.id, profile.displayName, profile.emails[0].value],
function (err) {
if (err) return done(err);
if (err) {
return done(err);
}

// Retrieve the newly created user
db.get(
'SELECT * FROM users WHERE googleId = ?',
[profile.id],
(err, newRow) => {
if (err) return done(err);
if (err) {
return done(err);
}
if (!newRow) {
return done(new Error('Failed to retrieve new user'));
}
console.log('New user created:', newRow); // Log the new user data
return done(null, newRow); // Return the new user with ID
}
);
}
);
} else {
console.log('Existing user found:', row); // Log the existing user
return done(null, row); // Return the existing user
}
}
Expand Down Expand Up @@ -90,7 +92,6 @@ function initialize(passport, getUserByUsername, getUserById) {

// Serialize user into the session
passport.serializeUser((user, done) => {
console.log('Serialized user:', user); // Log the user object being serialized
if (user && user.id) {
done(null, user.id);
} else {
Expand All @@ -99,13 +100,12 @@ function initialize(passport, getUserByUsername, getUserById) {
});

passport.deserializeUser((id, done) => {
console.log('Deserializing user with ID:', id); // Log the ID being deserialized

// Find user by ID in the database
db.get('SELECT * FROM users WHERE id = ?', [id], (err, user) => {
if (err) return done(err);
if (err) {
return done(err);
}
if (!user) {
console.log('User not found during deserialization'); // Log if user is not found
return done(new Error('User not found'));
}
done(null, user); // If user is found, pass it to the next middleware
Expand Down
14 changes: 9 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,11 @@ app.post(
],
function (err) {
if (err) {
console.log(err.message);
req.flash('error', 'Registration failed.');
return res.redirect('/register');
}
req.logIn(newUser, (err) => {
if (err) {
console.log(err);
req.flash('error', 'Registration failed.');
return res.redirect('/register');
}
Expand All @@ -177,14 +175,12 @@ app.post(
['{}', newUser.id, '__autosave__'],
function (err) {
if (err) {
console.log(err.message);
req.flash('error', 'Registration failed.');
return res.redirect('/register');
}
}
);
} catch (e) {
console.log(e);
req.flash('error', 'Registration failed.');
res.redirect('/register');
}
Expand All @@ -204,7 +200,6 @@ app.get('/blockly_unix', addAuthToken, (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'), {
headers: { 'X-Auth-Token': req.authToken || '' }
});
//console.log('Token:', req.authToken);
});

app.get('/auth-token', addAuthToken, (req, res) => {
Expand Down Expand Up @@ -250,8 +245,17 @@ app.get('/logout', function (req, res, next) {
if (err) {
return next(err);
}

res.clearCookie('connect.sid', {
path: '/',
httpOnly: true,
secure: false
});

return res.redirect('/');
});
} else {
res.redirect('/');
}
});

Expand Down
14 changes: 7 additions & 7 deletions table_creation_queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ CREATE TABLE IF NOT EXISTS users (
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL UNIQUE,
password TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
created_at DATETIME DEFAULT (datetime('now', 'localtime'))
);

CREATE TABLE IF NOT EXISTS workspaces (
id INTEGER PRIMARY KEY AUTOINCREMENT,
workspaceData TEXT NOT NULL,
userId TEXT NOT NULL,
workspaceName TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
userId INTEGER NOT NULL,
workspaceName TEXT NOT NULL,
created_at DATETIME DEFAULT (datetime('now', 'localtime')),
FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS guestsWorkspaces (
id INTEGER PRIMARY KEY AUTOINCREMENT,
workspaceData TEXT NOT NULL,
executionStatus BOOLEAN DEFAULT 0, -- Field to track if execution was performed (0 for no, 1 for yes)
changesAfterExecution BOOLEAN DEFAULT 0, -- Field to track if changes were made after execution (0 for no, 1 for yes)
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
executionStatus BOOLEAN DEFAULT 0,
changesAfterExecution BOOLEAN DEFAULT 0,
created_at DATETIME DEFAULT (datetime('now', 'localtime'))
);

--DELETE FROM users;
Expand Down

0 comments on commit 8505642

Please sign in to comment.