linkedin-oauth
v0.0.5
Published
Linkedin Oauth implementation
Downloads
1,286
Maintainers
Readme
LinkedIn OAuth Implementation
Guides:
Register two endpoints
- First for hitting the Linkedin API which enforces the user to login in the Linkedin and get the temporary access code.
- Second for the callback url, which should uses the temporary code to get the access token and the user profile, using it either we can authenticate in the application server.
Example with Express
Getting the access code from the LinkedIn.
app.get("/linkedin", (req, res) => {
const redirectUri = linkedInOAuth.getAuthorizationUrl();
return res.redirect(redirectUri);
});Getting the access token and Linkedin Profile
app.get("/linkedin/callback", async (req: Request, res: Response) => {
const code = req.query.code as string;
if (!code) {
console.error("No linkedin code available.");
return res.redirect(`${LINKEDIN_FAILURE_CALLBACK_URI}?success=false`);
}
try {
//retrieving access token to get the user profile...
const linkedInAccessToken = await linkedInOAuth.getAccessToken(code);
// Getting user profile
const linkedInProfile = await linkedInOAuth.getUserProfile(
linkedInAccessToken.access_token,
);
// Alternatively use ID token.
const userProfileFromIdToken = linkedInOAuth.getUserProfileFromIdToken(linkedInAccessToken.id_token);
// Use the linkedin profile to either register or signup or create a seperate endpoint to handle each register/login
const applicationAccessToken =
mockApplication.loginOrSignUp(linkedInProfile);
return res.redirect(
`${LINKEDIN_SUCCESS_CALLBACK_URI}?success=true&accessToken=${applicationAccessToken}`,
);
} catch (err) {
console.error(err);
if (err instanceof LinkedInOAuthException) {
return res.redirect(`${LINKEDIN_FAILURE_CALLBACK_URI}?success=false`);
}
}
});