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
* Isolates the OAuth experience to a few simple methods.
32
32
* Atomatically stores the tokens for later retrieval
33
-
* Works with many providers and relatively simple to add a provider
33
+
* Works with many providers and relatively simple to add a new provider
34
34
35
35
## Installation
36
36
@@ -49,55 +49,62 @@ As we are integrating with react-native, we have a little more setup to integrat
49
49
To automatically link our `react-native-oauth` client to our application, use the `rnpm` tool. [rnpm](https://github.com/rnpm/rnpm) is a React Native package manager which can help to automate the process of linking package environments.
50
50
51
51
```bash
52
-
rnpm link
52
+
react-native link react-native-oauth
53
53
```
54
54
55
-
#### Manually
55
+
Note: due to some restrictions on iOS, this module requires you to install cocoapods. The process has been semi-automated through using the above `react-native link` command. Once you have linked this library, run: `pod install` and then open the created `.xcworkspace` when it's complete.
56
56
57
-
If you prefer not to use `rnpm`, we can manually link the package together with the following steps, after `npm install`:
57
+
### Android setup
58
58
59
-
1. In XCode, right click on `Libraries` and find the `Add Files to [project name]`.
59
+
Coming soon (looking for contributors).
60
60
61
-

61
+
## Handle deep linking loading
62
62
63
-
2. Add the `node_modules/react-native-oauth/ios/OAuthManager.xcodeproj`
63
+
**Required step**
64
64
65
-

65
+
We'll need to handle app loading from a url with our app in order to handle authentication from other providers. That is, we'll need to make sure our app knows about the credentials we're authenticating our users against when the app loads _after_ a provider is authenticated against.
66
66
67
-
3. In the project's "Build Settings" tab in your app's target, add `libOAuthManager.a` to the list of `Link Binary with Libraries`
67
+
### iOS setup
68
68
69
-

69
+
We need to add a callback method in our `ios/AppDelegate.m` file and then call our OAuthManager helper method. Let's load the `ios/AppDelegate.m` file and add the following all the way at the bottom (but before the `@end`):
70
70
71
-
4. Ensure that the `Build Settings` of the `OAuthManager.xcodeproj` project is ticked to _All_ and it's `Header Search Paths` include both of the following paths _and_ are set to _recursive_:
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.
75
86
76
-

87
+
### Adding URL schemes
77
88
78
-
### Android setup
89
+
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).
79
90
80
-
Coming soon (looking for contributors).
91
+
These URL schemes can be added by navigating to to the `info` panel of our app in Xcode (see screenshot).
81
92
82
-
## Handle deep linking loading
93
+

83
94
84
-
**Required step**
95
+
Let's add the appropriate one for our provider. For instance, to set up twitter, add the app name as a URL scheme in the URL scheme box.
85
96
86
-
We'll need to handle app loading from a url with our app in order to handle authentication from other providers. That is, we'll need to make sure our app knows about the credentials we're authenticating our users against when the app loads _after_ a provider is authenticated against.
97
+

87
98
88
-
### iOS setup
99
+
## Creating the manager
89
100
90
-
We need to add a callback method in our `ios/AppDelegate.m` file and then call our OAuthManager helper method. Let's load the `ios/AppDelegate.m` file and add the following all the way at the bottom (but before the `@end`):
101
+
In our JS, we can create the manager by instantiating a new instance of it using the `new` method and passing it the name of our app:
const manager = new OAuthManager('firestackexample')
98
105
```
99
106
100
-
When our app loads up with a request that is coming back from OAuthManager _and_ matches the pattern of `[app-name]://oauth-callback/{providerName}`, the OAuthManager will take over and handle the rest and storing the credentials for later use.
107
+
We need to pass the name of our app as the oauth manager uses this to create callback keys. This _must_ match the URL route created in your iOS app. For instance, above we created a URL scheme for Twitter. Pass this as the string in the `OAuthManager` constructor.
The `consumer_key` and `consumer_secret` values are _generally_ provided by the provider development program. In the case of [twitter](https://apps.twitter.com), we can create an app and generate these values through their [development dashboard](https://apps.twitter.com).
@@ -126,32 +140,127 @@ The `consumer_key` and `consumer_secret` values are _generally_ provided by the
126
140
127
141
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:
128
142
129
-
* Twitter
130
-
* consumer_key
131
-
* consumer_secret
132
-
* Facebook (not fully implemented)
133
-
* consumer_key
134
-
* consumer_secret
143
+
#### Twitter
144
+
145
+
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)).
146
+
147
+

148
+
149
+
Once you have created one, navigate to the application and find the `Keys and Access Tokens`. Take note of the consumer key and secret:
150
+
151
+

152
+
153
+
Twitter's URL scheme needs to be the app name (that we pass into the constructor method). Make sure we have one registered in Xcode as the same name:
154
+
155
+

156
+
157
+
Add these values to the authorization configuration to pass to the `configure()` method as:
158
+
159
+
```javascript
160
+
constconfig= {
161
+
twitter: {
162
+
consumer_key:'SOME_CONSUMER_KEY',
163
+
consumer_secret:'SOME_CONSUMER_SECRET'
164
+
}
165
+
}
166
+
```
167
+
168
+
#### Facebook
169
+
170
+
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/).
171
+
172
+

173
+
174
+
Find or create an application and find the app id. Take note of this app id. Next, navigate to the `Settings` panel and find your client_secret.
175
+
176
+

177
+
178
+
Before we leave the Facebook settings, we need to tell Facebook we have a new redirect url to register. Navigate to the bottom of the page and add the following into the `bundle ID` field:
179
+
180
+
`fb{YOUR_APP_ID}`
181
+
182
+
For instance, my app ID in this example is: `1745641015707619`. In the `Bundle ID` field, I have added `fb1745641015707619`.
183
+
184
+

185
+
186
+
We'll need to create a new URL scheme for Facebook and (this is a weird bug on the Facebook side) the facebook redirect URL scheme _must be the first one_ in the list. The URL scheme needs to be the same id as the `Bundle ID` copied from above:
187
+
188
+

189
+
190
+
Back in our application, add the App ID and the secret as:
191
+
192
+
```javascript
193
+
constconfig= {
194
+
facebook: {
195
+
client_id:'YOUR_APP_ID',
196
+
client_secret:'YOUR_APP_SECRET'
197
+
}
198
+
}
199
+
```
200
+
201
+
#### Google
202
+
203
+
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)).
204
+
205
+

206
+
207
+
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.
208
+
209
+
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:
210
+
211
+

212
+
213
+
Take note of the `iOS URL Scheme`. We'll need to add this as a URL scheme in our app. In the `Info` panel of our app target (in Xcode), add the URL scheme:
214
+
215
+

216
+
217
+
Finally, add the `client_id` credential as the id from the url page as well as the ios scheme (with any path) in our app configuration:
218
+
219
+
```javascript
220
+
constconfig= {
221
+
google: {
222
+
callback_url:`[IOS SCHEME]:/google`,
223
+
client_id:'YOUR_CLIENT_ID'
224
+
}
225
+
}
226
+
```
135
227
136
228
## Authenticating against our providers
137
229
138
-
In order to make any authenticated calls against a provider, we need to authenticate against it. The `react-native-oauth` library passes through an easy method for dealing with authentication with the `authorizeWithCallbackURL()` method.
139
230
140
-
Using the app uri we previous setup, we can call the `authorizeWithCallbackURL()` method to ask iOS to redirect our user to a browser where they can log in to our app in the usual flow. When the user authorizes the login request, the promise returned by the `authorizeWithCallbackURL()` is resolved. If they reject the login request for any reason, the promise is rejected along with an error, if there are any.
231
+
We can use the manager in our app using the `authorize()` method on the manager.
232
+
233
+
The `authorize` method takes two arguments (the first one is required):
234
+
235
+
* The provider we wish to authenticate against (i.e. twitter, facebook)
236
+
* The list of options on a per-provider basis (optional)
// the oauthResponse object is the response returned by the request
146
-
// which is later stored by react-native-oauth using AsyncStorage
147
-
})
148
-
.catch((err) => {
149
-
// err is an error object that contains the reason the user
150
-
// error rejected authentication.
151
-
})
241
+
manager.authorize('twitter')
242
+
.then(resp=>console.log(resp))
243
+
.catch(err=>console.log(err));
152
244
```
153
245
154
-
When the response is returned, `react-native-oauth` will store the resulting credentials using the `AsyncStorage` object provided natively by React Native. All of this happens behinds the scenes _automatically_. When the credentials are successfully rerequested, `AsyncStorage` is updated behind the scenes automatically. All you have to do is take care of authenticating the user using the `authorizeWithCallbackURL()` method.
246
+
This method returns a promise that is resolved once the authentication has been completed. You'll get access to the authentication keys in the `resp` object.
0 commit comments