Skip to content

Commit 1088f3b

Browse files
committed
Merge branch 'develop'
2 parents 2175f8d + e01e0f0 commit 1088f3b

File tree

3 files changed

+273
-14
lines changed

3 files changed

+273
-14
lines changed

README.md

Lines changed: 255 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
1+
OAuth.io JavaScript SDK
2+
=======================
3+
4+
This is the JavaScript SDK for [OAuth.io](https://oauth.io). OAuth.io allows you to integrate **100+ providers** really easily in your web app, without worrying about each provider's OAuth specific implementation.
5+
16
Installation
27
============
38

9+
Getting the SDK
10+
---------------
11+
12+
To get the SDK, you can :
13+
14+
- download the zip file from this repository
15+
- get it via Bower
16+
17+
**Zip file**
18+
19+
Just copy the dist/oauth.js or dist/oauth.min.js to your project.
20+
21+
**Bower**
22+
23+
```sh
24+
$ bower install oauth-js
25+
```
26+
27+
Integrating in your project
28+
---------------------------
29+
430
In the `<head>` of your HTML, include OAuth.js
531

6-
`<script src="/path/to/OAuth.js"></script>`
32+
`<script src="/path/to/oauth.js"></script>`
733

834
In your Javascript, add this line to initialize OAuth.js
935

10-
`OAuth.initialize('Public key');`
36+
`OAuth.initialize('your_app_public_key');`
1137

1238
Usage
1339
=====
@@ -19,9 +45,13 @@ Mode popup
1945

2046
```javascript
2147
//Using popup (option 1)
22-
OAuth.popup('facebook', function(err, result) {
48+
OAuth.popup('facebook')
49+
.done(function(result) {
50+
//use result.access_token in your API request
51+
//or use result.get|post|put|del|patch|me methods (see below)
52+
})
53+
.fail(function (err) {
2354
//handle error with err
24-
//use result.access_token in your API request
2555
});
2656
```
2757

@@ -36,10 +66,228 @@ OAuth.redirect('facebook', "callback/url");
3666
In callback url :
3767

3868
```javascript
39-
OAuth.callback('facebook', function(err, result) {
40-
//handle error with err
41-
//use result.access_token in your API request
69+
OAuth.callback('facebook')
70+
.done(function(result) {
71+
//use result.access_token in your API request
72+
//or use result.get|post|put|del|patch|me methods (see below)
73+
})
74+
.fail(function (err) {
75+
//handle error with err
4276
});
4377
```
4478

79+
Making requests
80+
---------------
81+
82+
You can make requests to the provider's API manually with the access token you got from the `popup` or `callback` methods, or use the request methods stored in the `result` object.
83+
84+
**GET Request**
85+
86+
To make a GET request, you have to call the `result.get` method like this :
87+
88+
```javascript
89+
//Let's say the /me endpoint on the provider API returns a JSON object
90+
//with the field "name" containing the name "John Doe"
91+
OAuth.popup(provider)
92+
.done(function(result) {
93+
result.get('/me')
94+
.done(function (response) {
95+
//this will display "John Doe" in the console
96+
console.log(response.name);
97+
})
98+
.fail(function (err) {
99+
//handle error with err
100+
});
101+
})
102+
.fail(function (err) {
103+
//handle error with err
104+
});
105+
```
106+
107+
**POST Request**
108+
109+
To make a POST request, you have to call the `result.post` method like this :
110+
111+
```javascript
112+
//Let's say the /message endpoint on the provider waits for
113+
//a POST request containing the fields "user_id" and "content"
114+
//and returns the field "id" containing the id of the sent message
115+
OAuth.popup(provider)
116+
.done(function(result) {
117+
result.post('/message', {
118+
data: {
119+
user_id: 93,
120+
content: 'Hello Mr. 93 !'
121+
}
122+
})
123+
.done(function (response) {
124+
//this will display the id of the message in the console
125+
console.log(response.id);
126+
})
127+
.fail(function (err) {
128+
//handle error with err
129+
});
130+
})
131+
.fail(function (err) {
132+
//handle error with err
133+
});
134+
```
135+
136+
**PUT Request**
137+
138+
To make a PUT request, you have to call the `result.post` method like this :
139+
140+
```javascript
141+
//Let's say the /profile endpoint on the provider waits for
142+
//a PUT request to update the authenticated user's profile
143+
//containing the field "name" and returns the field "name"
144+
//containing the new name
145+
OAuth.popup(provider)
146+
.done(function(result) {
147+
result.put('/message', {
148+
data: {
149+
name: "John Williams Doe III"
150+
}
151+
})
152+
.done(function (response) {
153+
//this will display the new name in the console
154+
console.log(response.name);
155+
})
156+
.fail(function (err) {
157+
//handle error with err
158+
});
159+
})
160+
.fail(function (err) {
161+
//handle error with err
162+
});
163+
```
164+
165+
**PATCH Request**
166+
167+
To make a PATCH request, you have to call the `result.patch` method like this :
168+
169+
```javascript
170+
//Let's say the /profile endpoint on the provider waits for
171+
//a PATCH request to update the authenticated user's profile
172+
//containing the field "name" and returns the field "name"
173+
//containing the new name
174+
OAuth.popup(provider)
175+
.done(function(result) {
176+
result.patch('/message', {
177+
data: {
178+
name: "John Williams Doe III"
179+
}
180+
})
181+
.done(function (response) {
182+
//this will display the new name in the console
183+
console.log(response.name);
184+
})
185+
.fail(function (err) {
186+
//handle error with err
187+
});
188+
})
189+
.fail(function (err) {
190+
//handle error with err
191+
});
192+
```
193+
194+
**DELETE Request**
195+
196+
To make a DELETE request, you have to call the `result.del` method like this :
197+
198+
```javascript
199+
//Let's say the /picture?id=picture_id endpoint on the provider waits for
200+
//a DELETE request to delete a picture with the id "84"
201+
//and returns true or false depending on the user's rights on the picture
202+
OAuth.popup(provider)
203+
.done(function(result) {
204+
result.del('/picture?id=84')
205+
.done(function (response) {
206+
//this will display true if the user was authorized to delete
207+
//the picture
208+
console.log(response);
209+
})
210+
.fail(function (err) {
211+
//handle error with err
212+
});
213+
})
214+
.fail(function (err) {
215+
//handle error with err
216+
});
217+
```
218+
219+
**Me() Request**
220+
221+
The `me()` request is an OAuth.io feature that allows you, when the provider is supported, to retrieve a unified object describing the authenticated user. That can be very useful when you need to login a user via several providers, but don't want to handle a different response each time.
222+
223+
To use the `me()` feature, do like the following (the example works for Facebook, Github, Twitter and many other providers in this case) :
224+
225+
```javascript
226+
//provider can be 'facebook', 'twitter', 'github', or any supported
227+
//provider that contain the fields 'firstname' and 'lastname'
228+
//or an equivalent (e.g. "FirstName" or "first-name")
229+
var provider = 'facebook';
230+
231+
OAuth.popup(provider)
232+
.done(function(result) {
233+
result.me()
234+
.done(function (response) {
235+
console.log('Firstname: ', response.firstname);
236+
console.log('Lastname: ', response.lastname);
237+
})
238+
.fail(function (err) {
239+
//handle error with err
240+
});
241+
})
242+
.fail(function (err) {
243+
//handle error with err
244+
});
245+
```
246+
247+
*Filtering the results*
248+
249+
You can filter the results of the `me()` method by passing an array of fields you need :
250+
251+
```javascript
252+
//...
253+
result.me(['firstname', 'lastname', 'email'/*, ...*/])
254+
//...
255+
```
256+
257+
258+
Contributing
259+
============
260+
261+
You are welcome to fork and make pull requests. We will be happy to review them and include them in the code if they bring nice improvements :)
262+
263+
Testing the SDK
264+
===============
265+
266+
To test the SDK, you first need to install the npm modules `jasmine-node` and `istanbul` (to get the tests coverage) :
267+
268+
```sh
269+
$ sudo npm install -g [email protected] istanbul
270+
```
271+
272+
Then you can run the testsuite from the SDK root directory :
273+
274+
```sh
275+
$ jasmine-node --verbose tests/unit/spec
276+
```
277+
278+
Once you've isntalled `istanbul`, you can run the following command to get coverage information :
279+
280+
```sh
281+
$ npm test
282+
```
283+
284+
The coverage report is generated in the `coverage` folder. You can have a nice HTML render of the report in `coverage/lcof-report/index.html`
285+
286+
License
287+
=======
288+
289+
This SDK is published under the Apache2 License.
290+
291+
292+
45293
More information in [oauth.io documentation](http://oauth.io/#/docs)

bower.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "oauth-js",
3+
"version": "0.2.0",
4+
"main": "dist/oauth.min.js",
5+
"description": "OAuth that just works",
6+
"license": "apache2",
7+
"ignore": [
8+
"coffee",
9+
"coverage",
10+
"js",
11+
"node_modules",
12+
"tests",
13+
"templates",
14+
"Gruntfile.js",
15+
"package.json",
16+
".gitignore"
17+
]
18+
}

dist/bower.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)