Skip to main content

Linkedin

Register your app in Linkedin's Developer Dashboard

To use the Linkedin Provider you will need a client id and a client secret. You will get them while following the process to setup Linkedin OAuth 2.0 for your application. Follow the instructions in the following link:

info

When asked for an origin URL, insert the origin of the url in which your app is hosted.

Examples:

  • http://localhost:3000
  • https://www.example.com

When asked for a redirect URL (or callback), insert[http or https]://[origin]/[your route for Linkedin authentication]

Examples:

  • http://localhost:8000/auth/linkedin
  • https://www.example.com/api/login/linkedin
  • https://www.example.com/godel/russell/myLinkedinLogin

Easy-to-read Medium article (Follow the guide until you get a client id and client secret and setup a callback. Don't worry about the rest, Gatekeeper will take care of that): https://medium.com/@pp411100/how-to-get-linkedin-api-access-token-98a91f77f35a

Linkedin's official guide (Follow the guide until you get a client id and client secret and setup a callback. Don't worry about the rest, Gatekeeper will take care of that): https://learn.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?tabs=HTTPS1

Linkedin Provider

Options

Option nameDescriptionDefault valueRequired?
clientIdYour Linkedin OAuth client idYes
clientSecretYour Linkedin OAuth client secretYes
callbackURLThe URL that will serve as callback for Linkedin OAuthYes
scopeThe OAuth scopes that will be granted to the access token['profile', 'email']No
danger

To the callbackURL option you must provide the URL you provided when asked for a redirect (or callback) URL when registering your Linkedin OAuth app.

Handler

The Linkedin Provider passes three parameters to the handler:

  • refresh_token: The OAuth refres token. It is usually only given the first time a user signs-in to your application
  • access_token: The OAuth access token.
  • profile: The information of the Linkedin user.

Errors

The predefined errors of this provider are:

  • UserNotFound

Examples

Example #1

Create a Linkedin Provider

import gatekeeper from 'gatekeeper-authentication'
import { LinkedinProvider } from 'gatekeeper-authentication/providers/linkedin'

const LinkedinAuth = new LinkedinProvider({
clientId: '<YOUR LINKEDIN CLIENT ID>',
clientSecret: '<YOUR LINKEDIN CLIENT SECRET>',
callbackURL: 'https://yourdomainorlocalhost.com/auth/linkedin'
}, function handler(refresh_token, access_token, profile) => {
const user = User.findOne({ linkedinId: profile.sub })

if (user == null) {
// For example, if the user does not exist, create it
User.create({
username: profile.name,
profilePicture: profile.picture
});
}

return user;
});

Login with a Linkedin Provider

warning

Important: When using the Linkedin provider on login routes, make sure you use gatekeeper.authenticateWithProvider(yourLinkedinProvider) in a GET route, as the user will be redirected to Linkedin's sign in page

router.get(
'/auth/linkedin',
gatekeeper.authenticateWithProvider(LinkedinAuth), (req, res) => {
return res.redirect('/profile');
}
);