@@ -36,7 +36,95 @@ const github = simpleOAuth2Github.create(options);
36
36
37
37
SimpleOAuth2Github comes with default values for most of the options.
38
38
39
+ ** Required options**
40
+
41
+ | Option | Description |
42
+ | --------------| -----------------------------------------------|
43
+ | clientId | Your App Id. |
44
+ | clientSecret | Your App Secret Id. |
45
+ | callbackURL | Callback configured when you created the app. |
46
+
47
+
48
+ ** Other options**
49
+
50
+ | Option | Default | Description |
51
+ | ------------------| ------------------------------| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
52
+ | scope | [ 'read: user ', 'read: email '] | https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps |
53
+ | state | '' | Your CSRF anti-forgery token. More at: https://auth0.com/docs/protocols/oauth2/oauth-state |
54
+ | returnError | false | When is false (default), will call the next middleware with the error object. When is true, will set req.tokenError to the error, and call the next middleware as if there were no error. |
55
+ | authorizeHost | 'https://github.com' | |
56
+ | authorizePath | '/login/oauth/authorize' | |
57
+ | tokenHost | 'https://github.com' | |
58
+ | tokenPath | '/login/oauth/access_token' | |
59
+ | authorizeOptions | {} | Pass extra parameters when requesting authorization. |
60
+ | tokenOptions | {} | Pass extra parameters when requesting access_token. |
61
+
39
62
## Example
40
63
41
64
### Original boilerplate
65
+
66
+ ``` js
67
+ const oauth2 = require (' simple-oauth2' ).create ({
68
+ client: {
69
+ id: process .env .GITHUB_CLIENT_ID ,
70
+ secret: process .env .GITHUB_CLIENT_SECRET
71
+ },
72
+ auth: {
73
+ authorizeHost: ' https://github.com' ,
74
+ authorizePath: ' /login/oauth/authorize' ,
75
+
76
+ tokenHost: ' https://github.com' ,
77
+ tokenPath: ' /login/oauth/access_token'
78
+ }
79
+ });
80
+
81
+ router .get (' /auth/github' , (req , res ) => {
82
+ const authorizationUri = oauth2 .authorizationCode .authorizeURL ({
83
+ redirect_uri: ' http://localhost:3000/auth/github/callback' ,
84
+ scope: [' read:user' , ' read:email' ]
85
+ });
86
+
87
+ res .redirect (authorizationUri);
88
+ });
89
+
90
+ router .get (' /auth/github/callback' , async (req, res) => {
91
+ const code = req .query .code ;
92
+ const options = {
93
+ code,
94
+ redirect_uri: ' http://localhost:3000/auth/github/callback'
95
+ };
96
+
97
+ try {
98
+ // The resulting token.
99
+ const result = await oauth2 .authorizationCode .getToken (options);
100
+
101
+ // Exchange for the access token.
102
+ const token = oauth2 .accessToken .create (result);
103
+
104
+ return res .status (200 ).json (token);
105
+ } catch (error) {
106
+ console .error (' Access Token Error' , error .message );
107
+ return res .status (500 ).json (' Authentication failed' );
108
+ }
109
+ });
110
+ ```
111
+
42
112
### With SimpleOAuth2Github
113
+
114
+ ``` js
115
+ const simpleOAuth2Github = require (' simple-oauth2-github' );
116
+
117
+ const github = simpleOAuth2Github .create ({
118
+ clientId: process .env .GITHUB_CLIENT_ID ,
119
+ clientSecret: process .env .GITHUB_CLIENT_SECRET ,
120
+ callbackURL: ' http://localhost:3000/auth/github/callback'
121
+ });
122
+
123
+ // Ask the user to authorize.
124
+ router .get (' /auth/github' , github .authorize );
125
+
126
+ // Exchange the token for the access token.
127
+ router .get (' /auth/github/callback' , github .accessToken , (req , res ) => {
128
+ return res .status (200 ).json (req .token );
129
+ });
130
+ ```
0 commit comments