diff --git a/server/routes/auth.js b/server/routes/auth.js index b26c98a..f900e94 100644 --- a/server/routes/auth.js +++ b/server/routes/auth.js @@ -152,6 +152,16 @@ router.get('/linkedin/callback', async (req, res) => { [githubUsername, linkedinId, githubAccessToken, accessToken] ); + // Once it's done, there's no need to keep the session data, destroy them. + req.session.destroy((err) => { + if (err) { + console.error('Error destroying session data:', err); + } else { + console.log('Session data destroyed successfully.'); + } + }); + + // Redirect to the registration page with the GitHub username and LinkedIn id res.redirect(`/users/register?githubUsername=${githubUsername}&linkedinId=${linkedinId}`); }); diff --git a/server/routes/index.js b/server/routes/index.js index 816719a..b3eb011 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -16,7 +16,16 @@ const masto = createRestAPIClient({ /* GET home page. */ router.get('/', function(req, res, next) { - res.render('index', { title: 'Express' }); + // Try to fetch the user's login info, if not eixsting, redirect to the login page + const userInfo = req.session.user; + + if (!userInfo) { + return res.redirect('/users/login'); + } + // Actually, I'd like to see what's inside userInfo + console.log('userInfo contents fetched from session store:', userInfo); + + res.render('index', { title: 'Express', userInfo: userInfo}); }); // Function to expand commit message and generate a full post using ChatGPT API diff --git a/server/routes/users.js b/server/routes/users.js index 60e444a..372947b 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -9,9 +9,45 @@ router.get('/', function(req, res, next) { }); router.get('/login', (req, res) => { - res.render('login', { title: 'Login' }); + res.render('login', { error, title: 'Login' }); }); +// Once finished the authorizatio process during registration, the user can directly login via GitHub username, +// instead of going through the normal login workflow and try to gain authorization from GitHub or LinkedIn again. +router.get('/login/now', async (req, res) => { + try { + const { githubUsername } = req.query; + await pool.query('SELECT * FROM users WHERE github_username = ?', [githubUsername], (error, results) => { + if (error) { + console.error('Database query error:', error); + return res.redirect('/users/login?error=databaseError'); + } + + if (results.length > 0) { + // Save the user info in the session store + req.session.user = results[0]; + req.session.save((err) => { + if (err) { + console.error('Session save error:', err); + return res.redirect('/users/login?error=sessionError'); + } + // Once successfully saved the user info, redirect the user to the main page + res.redirect('/'); + }); + + } else { + res.redirect('/users/register?error=userNotFound'); + } + + }); + + } catch (error) { + console.error('Error during login with GitHub username directly!', error); + res.redirect('/users/login?error=githubLoginFailed'); + } +}); + + router.get('/register', (req, res) => { const { githubUsername, linkedinId, error } = req.query; res.render('register', { githubUsername, linkedinId, error }); diff --git a/server/views/index.pug b/server/views/index.pug index 49da536..38c2148 100644 --- a/server/views/index.pug +++ b/server/views/index.pug @@ -3,22 +3,6 @@ extends layout block content h1= title p Welcome to #{title} - h1 GitHub Actions Deployment Pipeline - p Testing GitHub Actions pipeline! - p Push again to see if changes made can be seen here! - p Continue to make changes, push, and the results will be automatically deployed to the server! - p Updated project settings so that changes in the existing repository can be reflected right away. - p Updated local repo on remote server to keep synced with the existing repository - p Rebooted remote server and push again to test. - h1 ChatGPT Integration Progress Check - p Test GitHub Webhook - p Added chatgpt api key - p Implemented a function to expand the commit message using OpenAI API - p Removed excessive app in pm2 - h1 Autoposting Functionality Test - p Added url and access token in order to make the post - p Updated ChatGPT prompt to limit the total length of the post - p Push again to test the posting functionality. - p Merged auto-posting functionality into the main repository - p Updated application access token - p Pushed into the main repository to test the auto-posting workflow \ No newline at end of file + + if userInfo + p Welcome our GitHub guest: #{userInfo.github_username} diff --git a/server/views/login.pug b/server/views/login.pug index f4d2309..abf0bca 100644 --- a/server/views/login.pug +++ b/server/views/login.pug @@ -8,6 +8,15 @@ block content p.text-white-50.mb-5 Please log in using one of the following options! .d-flex.flex-column.align-items-center + if error == 'githubLoginFailed' + p.text-danger.mb-1 GitHub auto-login failed! + p.text-danger.mb-3 Please try the following options again! + else if error == 'databaseError' + p.text-danger.mb-1 Database query error! + p.text-danger.mb-3 Please try the following options again! + else if error == 'sessionError' + p.text-danger.mb-1 Session error! + p.text-danger.mb-3 Please try the following options again! button.btn.btn-outline-light.btn-lg.mb-3.w-100(type='button' onclick="window.location.href='/auth/login/github'") i.fab.fa-github.fa-lg(style='margin-right: 8px') | Login with Github diff --git a/server/views/register.pug b/server/views/register.pug index 82d757a..dd0df55 100644 --- a/server/views/register.pug +++ b/server/views/register.pug @@ -17,6 +17,9 @@ block content else if error == 'openidTokenVerificationFailed' p.text-danger.mb-1 OpenId Token verification failed! p.text-danger.mb-3 Please try again! + else if error == 'userNotFound' + p.text-danger.mb-1 User info can't be found using GitHub username! + p.text-danger.mb-3 Please try registering an account first! if githubUsername button.btn.btn-success.btn-lg.mb-3.w-100(type='button' disabled) @@ -27,7 +30,7 @@ block content button.btn.btn-success.btn-lg.mb-3.w-100(type='button' disabled) i.fab.fa-github.fa-lg(style='margin-right: 8px') | LinkedIn Authorized - button.btn.btn-primary.btn-lg.w-100(type='button' onclick="window.location.href='/users/login'") + button.btn.btn-primary.btn-lg.w-100(type='button' onclick=`window.location.href='/users/login/now?githubUsername=${githubUsername}'`) i.fas.fa-sign-in-alt.fa-lg(style='margin-right: 8px') | Login Now else