Skip to content

Commit

Permalink
Merge pull request #98 from rogershi-dev/feature/registration-auth
Browse files Browse the repository at this point in the history
Integrated a transaction to manage github_token refresh
  • Loading branch information
rogershi-dev authored Jul 13, 2024
2 parents 100f0be + 0517fa6 commit fca629b
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,50 @@ router.get('/github/callback', async (req, res) => {
// Save the user info in the session store
// Each time the user finishes the authentication, a new accessToken will be generated,
// thus we need to update the original github_token with this new accessToken
const [updatedRows, updatedFields] = await pool.query('UPDATE users SET github_token = ? WHERE github_username = ?', [accessToken, githubUsername]);
console.log('If the user already exists, update the github_token with the new one.');
console.log('The updated rows returned by MySQL server after executing the UPDATE clause:', updatedRows);
console.log('The updated fields returned by MySQL server after executing the UPDATE clause:', updatedFields);

req.session.user = updatedRows;
req.session.save((err) => {
if (err) {
console.error('Session save error:', err);
res.redirect('/users/login?error=sessionError');
} else {
console.log('Session saved successfully!');
res.redirect('/');
try {
// Get a connection from the pool
const connection = await pool.getConnection();
try {
// Start a transaction
await connection.beginTransaction();

// Perform the UPDATE operation to refresh github_token
await connection.query('UPDATE users SET github_token = ? WHERE github_username = ?', [accessToken, githubUsername]);

// Perform a SELECT query to retrieve the updated row data
const [updatedRows, updatedFields] = await connection.query('SELECT * FROM users WHERE github_username = ?', [githubUsername]);
console.log('If the user already exists, update the github_token with the new one.');
console.log('The updated rows returned by MySQL server after executing the UPDATE clause:', updatedRows);
console.log('The updated fields returned by MySQL server after executing the UPDATE clause:', updatedFields);

// Commit the transaction
await connection.commit();

req.session.user = updatedRows;
req.session.save((err) => {
if (err) {
console.error('Session save error:', err);
res.redirect('/users/login?error=sessionError');
} else {
console.log('Session saved successfully!');
res.redirect('/');
}
});

} catch (error) {
// Rollback the transaction in case of error
await connection.rollback();
console.error('Error during transaction:', error);
} finally {
// Release the connection back to the pool
connection.release();
}
});
} catch (poolError) {
console.error('Error getting connection from pool:', poolError);
}


} else {
// Store Github username and access token in session for later use with LinkedIn OAuth
req.session.githubUsername = githubUsername;
Expand Down

0 comments on commit fca629b

Please sign in to comment.