@@ -2,89 +2,106 @@ A simple [Passport](http://passportjs.org/) strategy for LinkedIn OAuth2 that wo
22
33## Install
44
5- npm install passport-linkedin-oauth2
5+ npm install passport-linkedin-oauth2
66
77## Usage
88
99Register the strategy
1010
11- ~~~ javascript
11+ ``` javascript
1212var LinkedInStrategy = require (' passport-linkedin-oauth2' ).Strategy ;
1313
14- passport .use (new LinkedInStrategy ({
15- clientID: LINKEDIN_KEY ,
16- clientSecret: LINKEDIN_SECRET ,
17- callbackURL: " http://127.0.0.1:3000/auth/linkedin/callback" ,
18- scope: [' r_emailaddress' , ' r_liteprofile' ],
19- }, function (accessToken , refreshToken , profile , done ) {
20- // asynchronous verification, for effect...
21- process .nextTick (function () {
22- // To keep the example simple, the user's LinkedIn profile is returned to
23- // represent the logged-in user. In a typical application, you would want
24- // to associate the LinkedIn account with a user record in your database,
25- // and return that user instead.
26- return done (null , profile);
27- });
28- }));
29- ~~~
14+ passport .use (
15+ new LinkedInStrategy (
16+ {
17+ clientID: LINKEDIN_KEY ,
18+ clientSecret: LINKEDIN_SECRET ,
19+ callbackURL: ' http://127.0.0.1:3000/auth/linkedin/callback' ,
20+ scope: [' email' , ' profile' , ' openid' ],
21+ },
22+ function (accessToken , refreshToken , profile , done ) {
23+ // asynchronous verification, for effect...
24+ process .nextTick (function () {
25+ // To keep the example simple, the user's LinkedIn profile is returned to
26+ // represent the logged-in user. In a typical application, you would want
27+ // to associate the LinkedIn account with a user record in your database,
28+ // and return that user instead.
29+ return done (null , profile);
30+ });
31+ }
32+ )
33+ );
34+ ```
3035
3136and then authenticate as:
3237
33- ~~~ javascript
34- app .get (' /auth/linkedin' ,
35- passport .authenticate (' linkedin' , { state: ' SOME STATE' }),
36- function (req , res ){
38+ ``` javascript
39+ app .get (
40+ ' /auth/linkedin' ,
41+ passport .authenticate (' linkedin' , { state: ' SOME STATE' }),
42+ function (req , res ) {
3743 // The request will be redirected to LinkedIn for authentication, so this
3844 // function will not be called.
39- });
40- ~~~
45+ }
46+ );
47+ ```
4148
4249the login callback:
4350
44- ~~~ javascript
45- app .get (' /auth/linkedin/callback' , passport .authenticate (' linkedin' , {
46- successRedirect: ' /' ,
47- failureRedirect: ' /login'
48- }));
49- ~~~
51+ ``` javascript
52+ app .get (
53+ ' /auth/linkedin/callback' ,
54+ passport .authenticate (' linkedin' , {
55+ successRedirect: ' /' ,
56+ failureRedirect: ' /login' ,
57+ })
58+ );
59+ ```
5060
51- See [ this] ( https://docs .microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin?context=linkedin/consumer/context?trk=eml_mktg_gco_dev_api_comms ) for details on LinkedIn API.
61+ See [ this] ( https://learn .microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2 ) for details on LinkedIn API.
5262
5363## Auto-handle ` state ` param
5464
5565The ` state ` param is used to prevent CSRF attacks, and is [ required by the LinkedIn API] ( https://developer.linkedin.com/documents/authentication ) . You can ask Passport to handle the sending and validating of the ` state ` parameter by passing ` state: true ` as an option to the strategy:
5666
57- ~~~ javascript
67+ ``` javascript
5868var LinkedInStrategy = require (' passport-linkedin-oauth2' ).Strategy ;
5969
60- passport .use (new LinkedInStrategy ({
61- clientID: LINKEDIN_KEY ,
62- clientSecret: LINKEDIN_SECRET ,
63- callbackURL: " http://127.0.0.1:3000/auth/linkedin/callback" ,
64- scope: [' r_emailaddress' , ' r_liteprofile' ],
65- state: true
66- }, function (accessToken , refreshToken , profile , done ) {
67- // asynchronous verification, for effect...
68- process .nextTick (function () {
69- // To keep the example simple, the user's LinkedIn profile is returned to
70- // represent the logged-in user. In a typical application, you would want
71- // to associate the LinkedIn account with a user record in your database,
72- // and return that user instead.
73- return done (null , profile);
74- });
75- }));
76- ~~~
70+ passport .use (
71+ new LinkedInStrategy (
72+ {
73+ clientID: LINKEDIN_KEY ,
74+ clientSecret: LINKEDIN_SECRET ,
75+ callbackURL: ' http://127.0.0.1:3000/auth/linkedin/callback' ,
76+ scope: [' email' , ' profile' , ' openid' ],
77+ state: true ,
78+ },
79+ function (accessToken , refreshToken , profile , done ) {
80+ // asynchronous verification, for effect...
81+ process .nextTick (function () {
82+ // To keep the example simple, the user's LinkedIn profile is returned to
83+ // represent the logged-in user. In a typical application, you would want
84+ // to associate the LinkedIn account with a user record in your database,
85+ // and return that user instead.
86+ return done (null , profile);
87+ });
88+ }
89+ )
90+ );
91+ ```
7792
7893and then authenticate as:
7994
80- ~~~ javascript
81- app .get (' /auth/linkedin' ,
95+ ``` javascript
96+ app .get (
97+ ' /auth/linkedin' ,
8298 passport .authenticate (' linkedin' ),
83- function (req , res ){
99+ function (req , res ) {
84100 // The request will be redirected to LinkedIn for authentication, so this
85101 // function will not be called.
86- });
87- ~~~
102+ }
103+ );
104+ ```
88105
89106## Issue Reporting
90107
0 commit comments