Skip to content

Commit

Permalink
Merge pull request #58 from rogershi-dev/feature/registration-auth
Browse files Browse the repository at this point in the history
Fixed property undifined error when decoding openid token.
  • Loading branch information
rogershi-dev authored Jul 11, 2024
2 parents 10b4d7d + 5d0da93 commit 4e009ce
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,36 @@ router.get('/linkedin/callback', async (req, res) => {
const accessToken = tokenResponse.data.access_token; // Valid within 60 days
console.log(tokenResponse.data);
const openidToken = tokenResponse.data.id_token;
console.log('Received openid token:', openidToken);

// Verify the JWT openid token and extract user data from it
var decodedUserData;
// var decodedUserData;
jwt.verify(openidToken, getKey, (err, decoded) => {
if (err) {
console.error('Error verifying openid token:', err);
res.redirect(`/users/register?githubUsername=${req.session.githubUsername}&error=openidTokenVerificationFailed`);
}

// Access verified claims
decodedUserData = decoded;
// const { sub, name, email, picture } = decoded;
// decodedUserData = decoded;
const { sub, name, email, picture } = decoded;
const linkedinId = sub; // user id
console.log('Decoded user data:', decoded);

// Retrieve the github credentials from session store and save all of them in local DB
const { githubUsername, githubAccessToken } = req.session;
pool.query(
'INSERT INTO users (github_username, linkedin_id, github_token, linkedin_token) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE github_token = VALUES(github_token), linkedin_token = VALUES(linkedin_token)',
[githubUsername, linkedinId, githubAccessToken, accessToken]
);

// Redirect to the registration page with the GitHub username and LinkedIn id
res.redirect(`/users/register?githubUsername=${githubUsername}&linkedinId=${linkedinId}`);
});

const { sub } = decodedUserData;
const linkedinId = sub; // user id
console.log(decodedUserData);
// const { sub } = decodedUserData;
// const linkedinId = sub; // user id
// console.log(decodedUserData);

// Now we can use the access token to fetch the user's profile information
// data: {
Expand All @@ -151,16 +164,6 @@ router.get('/linkedin/callback', async (req, res) => {
// });
// const linkedinId = profileResponse.data.id;

// Retrieve the github credentials from session store and save all of them in local DB
const { githubUsername, githubAccessToken } = req.session;
await pool.query(
'INSERT INTO users (github_username, linkedin_id, github_token, linkedin_token) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE github_token = VALUES(github_token), linkedin_token = VALUES(linkedin_token)',
[githubUsername, linkedinId, githubAccessToken, accessToken]
);

// Redirect to the registration page with the GitHub username and LinkedIn id
res.redirect(`/users/register?githubUsername=${githubUsername}&linkedinId=${linkedinId}`);

} catch (error) {
console.error('Error during LinkedIn access token exchanging:', error);
// If the authorization process failed, redirect the user back to the registration page with an error message
Expand Down

0 comments on commit 4e009ce

Please sign in to comment.