Skip to content

Commit 6a11c86

Browse files
authored
Merge pull request #137 from stormpath/support-client-api
Add client API support
2 parents c473254 + 939da5f commit 6a11c86

23 files changed

+649
-106
lines changed

README.md

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,33 @@ Follow these steps to add Stormpath user authentication to your React app.
2121
*Don't have an app? Use our [example app][] as a boilerplate - it has
2222
Stormpath already integrated!*
2323

24-
1. **Configure Stormpath**
25-
26-
In your application directory, create a file named `stormpath.yml` with the contents below:
27-
28-
```yaml
29-
client:
30-
apiKey:
31-
id: YOUR_API_KEY_ID
32-
secret: YOUR_API_KEY_SECRET
33-
application:
34-
href: https://api.stormpath.com/v1/applications/XXXX <-- YOUR APP HREF
35-
```
36-
37-
2. **Install React Router**
24+
1. **Install React Router**
3825

3926
The Stormpath module is only compatible with [React Router][], so ensure that your application is using it.
4027

41-
3. **Integrate Your Back-End**
42-
43-
This module requires Stormpath on your back-end to work properly. At the moment we
44-
have a fully-featured integration for **Express.js**, [express-stormpath][].
4528

46-
For a **quick setup**, use our [Stormpath SPA Development Server][].
29+
2. **Install the SDK**
4730

48-
4. **Install the SDK**
49-
50-
Download and include [stormpath-sdk-react.min.js][] in your *index.html* file.
51-
52-
```html
53-
<script src="stormpath-sdk-react.min.js"></script>
54-
```
31+
If you are using Bower or NPM, you can install this module with the respective command:
5532

56-
Or install with bower:
57-
5833
```term
59-
$ bower install react-stormpath --save
34+
npm install react-stormpath --save
6035
```
6136

62-
Or install with npm:
63-
6437
```term
65-
$ npm install react-stormpath --save
38+
bower install react-stormpath --save
6639
```
40+
41+
If you are not using a package manager, you can download the latest source from our Github CDN by using this link:
42+
43+
* [stormpath-sdk-react.min.js][]
44+
45+
Then include the script in your *index.html* file:
6746

47+
```html
48+
<script src="stormpath-sdk-react.min.js"></script>
49+
```
50+
6851
Then depending on how you load the library, access it as shown below:
6952

7053
```javascript
@@ -83,17 +66,19 @@ Stormpath already integrated!*
8366
var LoginLink = ReactStormpath.LoginLink;
8467
```
8568

86-
5. **Initialize the SDK**
87-
88-
Before calling `React.render()` be sure to call [`ReactStormpath.init()`][]. This sets up the SDK so that it's ready to be used.
69+
3. **Initialize the SDK**
70+
71+
The React SDK leverages the [Stormpath Client API][] for its authentication needs. Login to your Stormpath Tenant, and find your Client API domain (inside your application's policy section). Add your Client API domain as the `endpoints.baseUri` setting when initializing `ReactStormpath`:
8972

9073
```javascript
9174
ReactStormpath.init({
92-
// See the API docs for available configuration options.
75+
endpoints: {
76+
baseUri: 'https://{{clientApiDomainName}}'
77+
}
9378
});
9479
```
9580

96-
6. **Configure the Router**
81+
4. **Configure the Router**
9782

9883
In the file where you setup your [React Router][] routes, change your [`ReactRouter.Router`][] to [`ReactStormpath.Router`][] as shown below:
9984

@@ -109,7 +94,7 @@ Stormpath already integrated!*
10994
);
11095
```
11196

112-
7. **Setup your Routes**
97+
5. **Setup your Routes**
11398

11499
Start by adding a route that people can go to in order to sign up. This will just be a regular [`ReactRouter.Route`][]. Then once you've done this, create a view for your route called `RegistrationPage` and add the [`RegistrationForm`][] component to it. This will render a registration form and allow people to sign up.
115100

@@ -143,7 +128,7 @@ Stormpath already integrated!*
143128
</AuthenticatedRoute>
144129
```
145130

146-
8. **Add Login and Logout Links**
131+
6. **Add Login and Logout Links**
147132

148133
Use the [`LoginLink`][] component to create a link that will navigate your users to the [`LoginRoute`][] route:
149134

@@ -157,7 +142,7 @@ Stormpath already integrated!*
157142
<LogoutLink>Logout</LogoutLink>
158143
```
159144

160-
9. **Show Elements When Logged In**
145+
7. **Show Elements When Logged In**
161146

162147
Use the [`Authenticated`][] component:
163148

@@ -167,7 +152,7 @@ Stormpath already integrated!*
167152
</Authenticated>
168153
```
169154

170-
10. **Hide Elements When Logged Out**
155+
8. **Hide Elements When Logged Out**
171156

172157
Use the [`NotAuthenticated`][] component:
173158

@@ -177,7 +162,7 @@ Stormpath already integrated!*
177162
</NotAuthenticated>
178163
```
179164

180-
11. **User State in Components**
165+
9. **User State in Components**
181166

182167
Access user state in your components by requesting the [authenticated][] and [user][] context types:
183168

@@ -206,9 +191,59 @@ Stormpath already integrated!*
206191
}
207192
```
208193

209-
12. **That's It!**
194+
10. **That's It!**
195+
196+
You just added user authentication to your React app with Stormpath, you should now be able to register and login! See the [API Documentation][] for further information on how Stormpath can be used with your React app. Once you have been able to successfully log in, the next section will discuss integrating with your own server.
197+
198+
11. **Making Authenticated Requests**
199+
200+
Once you are able to successfully authenticate (log in) from your application, you will want to authorize access to API endpoints on your server. The React SDK provides methods for getting the current authenticated access token, and using it to authenticate requests.
201+
202+
Imagine you have an API on your server, such as `http://localhost:3000/api/subscription`, and you want to authorize requests to this endpoint and know who the user is.
203+
204+
If you want to manually construct a request, using the `fetch` library, you can use our access token getter to add the access token to the request:
205+
206+
```javascript
207+
ReactStormpath.getAccessToken()
208+
.then((accessToken) => {
209+
fetch('http://localhost:3000/api/subscription', {
210+
method: 'get',
211+
headers: {
212+
'Authorization': 'Bearer ' + accessToken
213+
}
214+
});
215+
}).catch(() => {
216+
// Could not get access token, user is not logged in
217+
});
218+
```
219+
220+
12. **Authorizing Requests Server-Side**
221+
222+
Once your app has made the request with the access token, your server will need to read the token and make an authorization decision. We provide SDKs for your backend server that make this easy. Please follow one of the following links for a language-specific or framework-specific guide:
223+
224+
**Java**
225+
226+
Spring Boot developers should make use of our Spring Boot plugin, and see the [Token Management Documentation](https://docs.stormpath.com/java/spring-boot-web/tutorial.html#token-management).
227+
228+
**.NET**
229+
230+
ASP.NET developers can leverage our [ASP.NET](https://docs.stormpath.com/dotnet/aspnet/latest/) and [ASP.NET Core](https://docs.stormpath.com/dotnet/aspnetcore/latest/) libraries to achieve authorization in their applications, please see the Authorization section of each guide.
231+
232+
**Node.js**
233+
234+
Express developers can use our [Express-Stormpath](https://docs.stormpath.com/nodejs/express/latest/) library to easily authenticate requests with access tokens and make authorization decisions, please see the [Token Authentication](https://docs.stormpath.com/nodejs/express/latest/authentication.html#token-authentication) documentation.
235+
236+
Node applications can generically use the [Stormpath Node SDK](https://docs.stormpath.com/nodejs/jsdoc/) to validate tokens, using the [JwtAuthenticator](https://docs.stormpath.com/nodejs/jsdoc/JwtAuthenticator.html).
237+
238+
**PHP**
239+
240+
Laravel developers can use our <a href="https://docs.stormpath.com/php/laravel/latest/index.html">Stormpath-Laravel</a> or [Stormpath-Lumen](https://docs.stormpath.com/php/lumen/latest/index.html) libraries and their respective `stormpath.auth` middleware to authenticate requests, please see the User Data section of the documentation for each library.
241+
242+
**Other**
243+
244+
Don't see your environment listed? Not a problem! Our access tokens are simple JWTs, that can be validated with most generic JWT validation libraries. Our product guide can walk you through the process, [Validating an Access Token](https://docs.stormpath.com/rest/product-guide/latest/auth_n.html#validating-an-access-token").
210245

211-
You just added user authentication to your app with Stormpath. See the [API Documentation][] for further information on how Stormpath can be used with your React app.
246+
Need more assistance? Feel free to contact our support channel, details are below.
212247

213248
## Documentation
214249

docs/api.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ ReactStormpath.init({
4141
});
4242
```
4343

44+
## Authorization
45+
46+
Once the user is logged in, you can make authenticated requests to back-end APIs by getting the access token and attaching it to your request:
47+
48+
```javascript
49+
ReactStormpath.getAccessToken()
50+
.then((accessToken) => {
51+
fetch('http://localhost:3000/api/subscription', {
52+
method: 'get',
53+
headers: {
54+
'Authorization': 'Bearer ' + accessToken
55+
}
56+
});
57+
}).catch(() => {
58+
// Could not get access token, user is not logged in
59+
});
60+
```
61+
62+
4463
## Routing
4564

4665
#### Router

docs/dispatcher.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ Dispatched when a user requests to change their password.
2525

2626
Dispatched when a user requests to verify their email.
2727

28+
#### TOKEN_SET
29+
30+
Dispatched when a token is set.
31+
32+
#### TOKEN_REFRESH
33+
34+
Dispatched when tokens are refreshed.
35+
2836
#### USER_LOGOUT
2937

3038
Dispatched when a user requests to logout.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,8 @@
7777
"user-management",
7878
"stormpath",
7979
"sdk"
80-
]
80+
],
81+
"dependencies": {
82+
"xtend": "^4.0.1"
83+
}
8184
}

src/actions/TokenActions.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import context from './../context';
2+
import TokenConstants from './../constants/TokenConstants';
3+
4+
function dispatch(event) {
5+
setTimeout(() => {
6+
context.getDispatcher().dispatch(event);
7+
}, 0);
8+
}
9+
10+
class TokenActions {
11+
set(type, token, callback) {
12+
dispatch({
13+
type: TokenConstants.TOKEN_SET,
14+
options: {
15+
type: type,
16+
token: token
17+
},
18+
callback: callback
19+
});
20+
}
21+
22+
refresh(token, callback) {
23+
dispatch({
24+
type: TokenConstants.TOKEN_REFRESH,
25+
options: {
26+
token: token
27+
},
28+
callback: callback
29+
});
30+
}
31+
}
32+
33+
export default new TokenActions()

0 commit comments

Comments
 (0)