Skip to content

Commit

Permalink
Merge pull request #62 from rogershi-dev/feature/registration-auth
Browse files Browse the repository at this point in the history
Implemented LoginNow after registration.
  • Loading branch information
rogershi-dev authored Jul 11, 2024
2 parents 6e08d62 + f5a9f0b commit 8714296
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 22 deletions.
10 changes: 10 additions & 0 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
Expand Down
11 changes: 10 additions & 1 deletion server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 37 additions & 1 deletion server/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
22 changes: 3 additions & 19 deletions server/views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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

if userInfo
p Welcome our GitHub guest: #{userInfo.github_username}
9 changes: 9 additions & 0 deletions server/views/login.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion server/views/register.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 8714296

Please sign in to comment.