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:
When asked for an origin URL, insert the origin of the url in which your app is hosted.
Examples:
http://localhost:3000https://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/linkedinhttps://www.example.com/api/login/linkedinhttps://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 name | Description | Default value | Required? |
|---|---|---|---|
clientId | Your Linkedin OAuth client id | Yes | |
clientSecret | Your Linkedin OAuth client secret | Yes | |
callbackURL | The URL that will serve as callback for Linkedin OAuth | Yes | |
scope | The OAuth scopes that will be granted to the access token | ['profile', 'email'] | No |
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 applicationaccess_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
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');
}
);