You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -107,15 +107,42 @@ Let's say your endpoint will be on /oauth/state_token :
107
107
108
108
```JavaScript
109
109
app.get('/oauth/state_token', function (req, res) {
110
-
var token =OAuth.generateStateToken(req);
110
+
var token =OAuth.generateStateToken(req.session);
111
111
112
112
res.send(200, {
113
113
token:token
114
114
});
115
115
});
116
116
```
117
117
118
-
**Creating an authentication endpoint**
118
+
**Authentication**
119
+
120
+
The SDK gives you an `auth` method that allows you to retrieve a `request_object`. That `request_object` allows you to make API calls, and contains the access token.
121
+
122
+
The `auth` method takes :
123
+
124
+
1. a provider name
125
+
2. the session array
126
+
3. (optional) an options object
127
+
128
+
It returns a promise to let you handle the callback and error management.
129
+
130
+
```
131
+
OAuth.auth('provider', req.session, {
132
+
option_field: option_value,
133
+
//...
134
+
});
135
+
```
136
+
137
+
The options object can contain the following fields :
138
+
139
+
- code: an OAuth code, that will be used to get credentials from the provider and build a request_object
140
+
- credentials: a credentials object, that will be used to rebuild a refreshed request_object
141
+
- force_refresh: forces the credentials refresh if a refresh token is available
142
+
143
+
If nothing is given in the options object, the auth method tries to build a request_object from the session.
144
+
145
+
*Authenticating the user for the first time*
119
146
120
147
When you launch the authentication flow from the front-end (that is to say when you show a popup to the user so that he can allow your app to use his/her data), you'll be given a code (see next section, "Integrating the front-end SDK" to learn how to get the code).
121
148
@@ -125,26 +152,29 @@ To do that, you have to create an authentication endpoint on your backend. This
125
152
126
153
```JavaScript
127
154
app.post('/api/signin', function (req, res) {
128
-
OAuth.auth(JSON.parse(req.body).code, req)
129
-
.then(function (result) {
130
-
//result contains the access_token if OAuth 2.0
131
-
//or the couple oauth_token,oauth_token_secret if OAuth 1.0
155
+
var code =JSON.parse(req.body).code;
156
+
157
+
// Here the auth method takes the field 'code'
158
+
// in its options object. It will thus use that code
159
+
// to retrieve credentials from the provider.
160
+
OAuth.auth('facebook', req.session, {
161
+
code: code
162
+
})
163
+
.then(function (request_object) {
164
+
// request_object contains the access_token if OAuth 2.0
165
+
// or the couple oauth_token,oauth_token_secret if OAuth 1.0
132
166
133
-
//result also contains methods get|post|patch|put|delete|me
134
-
result.get('/me')
135
-
.then(function (info) {
136
-
var user = {
137
-
email:info.email,
138
-
firstname:info.first_name,
139
-
lastname:info.last_name
140
-
};
141
-
//login your user here.
142
-
res.send(200, 'Successfully logged in');
143
-
})
144
-
.fail(function (e) {
145
-
//handle errors here
146
-
res.send(500, 'An error occured');
147
-
});
167
+
// request_object also contains methods get|post|patch|put|delete|me
168
+
returnrequest_object.get('/me');
169
+
})
170
+
.then(function (info) {
171
+
var user = {
172
+
email:info.email,
173
+
firstname:info.first_name,
174
+
lastname:info.last_name
175
+
};
176
+
//login your user here.
177
+
res.send(200, 'Successfully logged in');
148
178
})
149
179
.fail(function (e) {
150
180
//handle errors here
@@ -153,19 +183,21 @@ app.post('/api/signin', function (req, res) {
153
183
});
154
184
```
155
185
156
-
**Use the authentication info in other endpoints**
186
+
*Authenticating a user from the session*
157
187
158
-
Once a user is authenticaded on a service, the auth result object is stored
188
+
Once a user is authenticaded on a service, the credentials are stored
159
189
in the session. You can access it very easily from any other endpoint to use it. Let's say for example that you want to post something on your user's wall on Facebook :
160
190
161
191
```JavaScript
162
192
app.post('/api/wall_message', function (req, res){
163
193
var data =JSON.parse(req.body);
164
194
//data contains field "message", containing the message to post
165
195
166
-
OAuth.create(req, 'facebook')
167
-
.post('/me/feed', {
168
-
message:data.message
196
+
OAuth.auth('facebook', req.session)
197
+
.then(function (request_object) {
198
+
returnrequest_object.post('/me/feed', {
199
+
message:data.message
200
+
});
169
201
})
170
202
.then(function (r) {
171
203
//r contains Facebook's response, normaly an id
@@ -180,6 +212,66 @@ app.post('/api/wall_message', function (req, res){
180
212
});
181
213
```
182
214
215
+
*Authenticating a user from saved credentials*
216
+
217
+
* Saving credentials
218
+
If you want to save the credentials to use them when the user is offline, (e.g. in a cron loading information), you can save the credentials in the data storage of your choice. All you need to do is to retrieve the credentials object from the request_object :
219
+
220
+
```javascript
221
+
OAuth.auth('provider', req.session, {
222
+
code: code
223
+
})
224
+
.then(function (request_object) {
225
+
var credentials = request_object->getCredentials();
226
+
227
+
// Here you can save the credentials object wherever you want
228
+
229
+
});
230
+
```
231
+
232
+
* Using saved credentials
233
+
234
+
You can then rebuild a request_object from the credentials you saved earlier :
235
+
```javascript
236
+
// Here you retrieved the credentials object from your data storage
237
+
238
+
OAuth.auth('provider', req.session, {
239
+
credentials: credentials
240
+
})
241
+
.then(function (request_object) {
242
+
// Here request_object has been rebuilt from the credentials object
243
+
// If the credentials are expired and contain a refresh token,
244
+
// the auth method automatically refresh them.
245
+
});
246
+
247
+
```
248
+
249
+
*Refreshing saved credentials*
250
+
251
+
Tokens are automatically refreshed when you use the `auth` method with the session or with saved credentials. The SDK checks that the access token is expired whenever it's called.
252
+
253
+
If it is, and if a refresh token is available in the credentials (you may have to configure the OAuth.io app in a specific way for some providers), it automatically calls the OAuth.io refresh token endpoint.
254
+
255
+
If you want to force a refresh from the `auth` method, you can pass the `force_refresh` field in the option object, like this :
256
+
257
+
```javascript
258
+
OAuth.auth('provider', req.session, {
259
+
force_refresh:true
260
+
});
261
+
```
262
+
263
+
You can also refresh a credentials object manually. To do that, call the OAuth.refreshCredentials on the request_object or on a credentials object :
0 commit comments