Skip to content

Commit 69b93c0

Browse files
committed
Updated docs with Android setup
1 parent b5f1574 commit 69b93c0

File tree

5 files changed

+83
-15
lines changed

5 files changed

+83
-15
lines changed

README.md

+54-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ manager.authorize('google', {scopes: 'profile email'})
3030

3131
* Isolates the OAuth experience to a few simple methods.
3232
* Atomatically stores the tokens for later retrieval
33-
* Works with many providers and relatively simple to add a new provider
33+
* Works with many providers and simple to add a new provider
34+
* Works on both Android and iOS
3435

3536
## Installation
3637

@@ -76,7 +77,7 @@ Finally, open the created `.xcworkspace` in the `ios/` directory (**NOT THE `.xp
7677

7778
### Android setup
7879

79-
Coming soon (looking for contributors).
80+
All we need to do is link `react-native-oauth` to our project and Android should just work.
8081

8182
## Handle deep linking loading
8283

@@ -124,6 +125,12 @@ In addition, we'll need to set up the handlers within the iOS app. Add the follo
124125

125126
When our app loads up with a request that is coming back from OAuthManager _and_ matches the url pattern, OAuthManager will take over and handle the rest and storing the credentials for later use.
126127

128+
### Android setup
129+
130+
After we link `react-native-oauth` to our application, we're ready to go. Android integration is much simpler, thanks to the in-app browser ability for our apps. `react-native-oauth` handles this for you.
131+
132+
One note, *all* of the callback urls follow the scheme: `http://localhost/[provider_name]`. Make sure this is set as a configuration for each provider below (documented in the provider setup sections).
133+
127134
### Adding URL schemes
128135

129136
In order for our app to load through these callbacks, we need to tell our iOS app that we want to load them. In order to do that, we'll have to create some URL schemes to register our app. Some providers require specific schemes (mentioned later).
@@ -180,7 +187,7 @@ The `consumer_key` and `consumer_secret` values are _generally_ provided by the
180187

181188
The following list are the providers we've implemented thus far in `react-native-oauth` and the _required_ keys to pass when configuring the provider:
182189

183-
#### Twitter
190+
#### Twitter (iOS/Android)
184191

185192
To authenticate against twitter, we need to register a Twitter application. Register your twitter application (or create a new one at [apps.twitter.com](https://apps.twitter.com)).
186193

@@ -205,7 +212,7 @@ const config = {
205212
}
206213
```
207214

208-
#### Facebook
215+
#### Facebook (iOS/Android)
209216

210217
To add facebook authentication, we'll need to have a Facebook app. To create one (or use an existing one), navigate to [developers.facebook.com/](https://developers.facebook.com/).
211218

@@ -238,15 +245,15 @@ const config = {
238245
}
239246
```
240247

241-
#### Google
248+
#### Google (iOS/Android)
242249

243250
To add Google auth to our application, first we'll need to create a google application. Create or use an existing one by heading to the [developers.google.com/](https://developers.google.com/) page (or the console directly at [https://console.developers.google.com](https://console.developers.google.com)).
244251

245252
![](./resources/google/auth-page.png)
246253

247254
We need to enable the `Identity Toolkit API` API. Click on `Enable API` and add this api to your app. Once it's enabled, we'll need to collect our credentials.
248255

249-
Navigate to the `Credentials` tab and create a new credential. Create a web API credential. Take note of the client id and the URL scheme. In addition, make sure to set the bundle ID as the bundle id in our application in Xcode:
256+
Navigate to the `Credentials` tab and create a new credential. Create a **web API credential**. Take note of the client id and the URL scheme. In addition, make sure to set the bundle ID as the bundle id in our application in Xcode:
250257

251258
![](./resources/google/creds.png)
252259

@@ -266,8 +273,31 @@ const config = {
266273
}
267274
```
268275

269-
## Authenticating against our providers
276+
#### Github (iOS/Android)
277+
278+
Adding Github auth to our application is pretty simple as well. We'll need to create a web application on the github apps page, which can be found at [https://github.com/settings/developers](https://github.com/settings/developers). Create one, making sure to add _two_ apps (one for iOS and one for Android) with the callback urls as:
279+
280+
* ios: [app_name]:// oauth (for example: `firestackexample://oauth`)
281+
* android: http://localhost/github
282+
283+
Take note of the `client_id` and `client_secret`
270284

285+
![](./resources/github/apps.png)
286+
287+
The `iOS URL Scheme` is the same as the twitter version, which means we'll just add the app name as a URL scheme (i.e. `firestackexample`).
288+
289+
Add the `client_id` and `client_secret` credentials to your configuration object:
290+
291+
```javascript
292+
const config = {
293+
github: {
294+
client_id: 'YOUR_CLIENT_ID',
295+
client_secret: 'YOUR_CLIENT_SECRET'
296+
}
297+
}
298+
```
299+
300+
## Authenticating against our providers
271301

272302
We can use the manager in our app using the `authorize()` method on the manager.
273303

@@ -349,6 +379,19 @@ authManager
349379
});
350380
```
351381

382+
To add more data to our requests, we can pass a third argument:
383+
384+
```javascript
385+
authManager
386+
.makeRequest('facebook', '/me', {
387+
headers: { 'Content-Type': 'application/java' },
388+
params: { email: 'me+rocks@ari.io' }
389+
})
390+
.then(resp => {
391+
console.log('Data ->', resp.data);
392+
});
393+
```
394+
352395
## Getting authorized accounts
353396

354397
Since OAuthManager handles storing user accounts, we can query it to see which accounts have already been authorized or not using `savedAccounts()`:
@@ -383,9 +426,8 @@ ___
383426

384427
## TODOS:
385428

386-
* [ ] Handle reauthenticating tokens (automatically?)
387429
* [x] Simplify method of adding providers
388-
* [x] Complete [facebook](https://developers.facebook.com/docs/facebook-login) support
389-
* [ ] Add [github](https://developer.github.com/v3/oauth/) support
390-
* [x] Add [Google]() support
391-
* [ ] Add Android support
430+
* [x] Add github(https://developer.github.com/v3/oauth/) support
431+
* [x] Add Google support
432+
* [x] Add Facebook support
433+
* [x] Add Android support

android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,38 @@ public void makeRequest(
225225
httpVerb = Verb.GET;
226226
}
227227

228-
OAuthRequest request;
228+
OAuthRequest request = new OAuthRequest(httpVerb, url.toString(), service);
229+
230+
// Params
231+
if (params.hasKey("params")) {
232+
final Map<String,String> params = (Map<String,String>) params.get("params");
233+
ReadableMapKeySetIterator iterator = params.keySetIterator();
234+
while (iterator.hasNextKey()) {
235+
String key = iterator.nextKey();
236+
ReadableType readableType = params.getType(key);
237+
switch(readableType) {
238+
case String:
239+
String val = params.getString(key);
240+
// String escapedVal = Uri.encode(val);
241+
request.addParameter(key, val);
242+
break;
243+
default:
244+
throw new IllegalArgumentException("Could not read object with key: " + key);
245+
}
246+
}
247+
}
248+
229249
if (authVersion.equals("1.0")) {
230250
final OAuth10aService service =
231251
OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, null);
232252
OAuth1AccessToken token = _credentialsStore.get(providerName, OAuth1AccessToken.class);
233253

234-
request = new OAuthRequest(httpVerb, url.toString(), service);
235254
service.signRequest(token, request);
236255
} else if (authVersion.equals("2.0")) {
237256
final OAuth20Service service =
238257
OAuthManagerProviders.getApiFor20Provider(providerName, cfg, null);
239258
OAuth2AccessToken token = _credentialsStore.get(providerName, OAuth2AccessToken.class);
240259

241-
request = new OAuthRequest(httpVerb, url.toString(), service);
242260
service.signRequest(token, request);
243261
} else {
244262
// Some kind of error here

ios/OAuthManager/OAuthManager.m

+8
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,14 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
410410
initWithRequestMethod:method
411411
URL:apiUrl
412412
items:items];
413+
414+
NSDictionary *body = [opts objectForKey:@"body"];
415+
if (body != nil) {
416+
for (NSString *key in body) {
417+
NSData *data = [[NSString stringWithFormat:@"%@", [body valueForKey:key]] dataUsingEncoding:NSUTF8StringEncoding];
418+
[request addMultiPartData:data withName:key type:@"application/json"]; // TODO: How should we handle different body types?
419+
}
420+
}
413421

414422
request.account = existingAccount;
415423

resources/github/apps.png

414 KB
Loading

resources/google/creds.png

57.4 KB
Loading

0 commit comments

Comments
 (0)