@fusionauth/angular-sdk 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +333 -13
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,24 +1,344 @@
|
|
|
1
|
-
|
|
1
|
+
An SDK for using FusionAuth in Angular applications.
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
**This is currently in beta testing. We’re working towards a 1.0
|
|
4
|
+
release.**
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
# Table of Contents
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
> Note: Don't forget to add `--project fusionauth-angular-sdk` or else it will be added to the default project in your `angular.json` file.
|
|
8
|
+
- [Overview](#overview)
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
- [Getting Started](#getting-started)
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
- [Installation](#installation)
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
- [Server Code Requirements](#server-code-requirements)
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
- [Usage](#usage)
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
- [Pre-built buttons](#pre-built-buttons)
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
- [Service usage](#service-usage)
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
- [Known issues](#known-issues)
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
- [Example App](#example-app)
|
|
25
|
+
|
|
26
|
+
- [Documentation](#documentation)
|
|
27
|
+
|
|
28
|
+
- [Releases](#releases)
|
|
29
|
+
|
|
30
|
+
<!--
|
|
31
|
+
this tag, and the corresponding end tag, are used to delineate what is pulled into the FusionAuth docs site (the client libraries pages). Don't remove unless you also change the docs site.
|
|
32
|
+
|
|
33
|
+
Please also use ``` instead of indenting for code blocks. The backticks are translated correctly to adoc format.
|
|
34
|
+
-->
|
|
35
|
+
|
|
36
|
+
<!--
|
|
37
|
+
tag::forDocSite[]
|
|
38
|
+
-->
|
|
39
|
+
|
|
40
|
+
# Overview
|
|
41
|
+
|
|
42
|
+
This SDK allows you to add login, logout, and registration buttons to
|
|
43
|
+
your Angular application. You can do this via pre-built buttons, or with
|
|
44
|
+
the FusionAuthService in your own components.
|
|
45
|
+
|
|
46
|
+
Your users will be sent to FusionAuth’s themeable hosted login pages and
|
|
47
|
+
then log in. After that, they are sent back to your Angular application.
|
|
48
|
+
|
|
49
|
+
Once authentication succeeds, the following secure, HTTP-only cookies
|
|
50
|
+
will be set:
|
|
51
|
+
|
|
52
|
+
- `app.at` - an OAuth [Access
|
|
53
|
+
Token](https://fusionauth.io/docs/v1/tech/oauth/tokens#access-token)
|
|
54
|
+
|
|
55
|
+
- `app.rt` - a [Refresh
|
|
56
|
+
Token](https://fusionauth.io/docs/v1/tech/oauth/tokens#refresh-token)
|
|
57
|
+
used to obtain a new `app.at`. This cookie will only be set if
|
|
58
|
+
refresh tokens are enabled on your FusionAuth instance.
|
|
59
|
+
|
|
60
|
+
The access token can be presented to APIs to authorize the request and
|
|
61
|
+
the refresh token can be used to get a new access token.
|
|
62
|
+
|
|
63
|
+
Note that this SDK requires you to have a server that performs the OAuth
|
|
64
|
+
token exchange. See [Server Code
|
|
65
|
+
Requirements](#server-code-requirements) for more details.
|
|
66
|
+
|
|
67
|
+
You can use this library against any version of FusionAuth or any OIDC
|
|
68
|
+
compliant identity server.
|
|
69
|
+
|
|
70
|
+
# Getting Started
|
|
71
|
+
|
|
72
|
+
## Installation
|
|
73
|
+
|
|
74
|
+
NPM:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npm install @fusionauth/angular-sdk
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Yarn:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
yarn add @fusionauth/angular-sdk
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Configuring FusionAuthModule
|
|
87
|
+
|
|
88
|
+
To configure the SDK, wrap your app with `FusionAuthProvider`:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { NgModule } from '@angular/core';
|
|
92
|
+
import { BrowserModule } from '@angular/platform-browser';
|
|
93
|
+
|
|
94
|
+
import { AppRoutingModule } from './app-routing.module';
|
|
95
|
+
import { FusionAuthModule } from '@fusionauth/angular-sdk';
|
|
96
|
+
|
|
97
|
+
@NgModule({
|
|
98
|
+
declarations: [],
|
|
99
|
+
imports: [
|
|
100
|
+
FusionAuthModule.forRoot({
|
|
101
|
+
clientId: '', // Your FusionAuth client ID
|
|
102
|
+
serverUrl: '', // The base URL of your server for the token exchange
|
|
103
|
+
redirectUri: '', // The URI that the user is directed to after the login/register/logout action
|
|
104
|
+
}),
|
|
105
|
+
],
|
|
106
|
+
providers: [],
|
|
107
|
+
bootstrap: []
|
|
108
|
+
})
|
|
109
|
+
export class AppModule { }
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Server Code Requirements
|
|
113
|
+
|
|
114
|
+
Authenticating with FusionAuth requires you to set up a server that will
|
|
115
|
+
be used to perform the OAuth token exchange. This server must have the
|
|
116
|
+
following endpoints:
|
|
117
|
+
|
|
118
|
+
### `GET /app/login`
|
|
119
|
+
|
|
120
|
+
This endpoint must:
|
|
121
|
+
|
|
122
|
+
1. Generate PKCE code.
|
|
123
|
+
a. The code verifier should be saved in a secure HTTP-only cookie.
|
|
124
|
+
b. The code challenge is passed along
|
|
125
|
+
2. Encode and save `redirect_url` from angular app to `state`.
|
|
126
|
+
3. Redirect browser to `/oauth2/authorize` with a `redirect_uri` to `/app/token-exchange`
|
|
127
|
+
|
|
128
|
+
[Example
|
|
129
|
+
implementation](https://github.com/FusionAuth/fusionauth-example-angular-sdk/blob/main/server/routes/login.js)
|
|
130
|
+
|
|
131
|
+
### `GET /app/callback`
|
|
132
|
+
|
|
133
|
+
This endpoint must:
|
|
134
|
+
|
|
135
|
+
1. Call
|
|
136
|
+
[/oauth2/token](https://fusionauth.io/docs/v1/tech/oauth/endpoints#complete-the-authorization-code-grant-request)
|
|
137
|
+
to complete the Authorization Code Grant request. The `code` comes from the request query parameter and
|
|
138
|
+
`code_verifier` should be available in the secure HTTP-only cookie, while
|
|
139
|
+
the rest of the parameters should be set/configured on the server
|
|
140
|
+
side.
|
|
141
|
+
|
|
142
|
+
2. Once the token exchange succeeds, read the `app.at` from the
|
|
143
|
+
response body and set it as a secure, HTTP-only cookie with the same
|
|
144
|
+
name.
|
|
145
|
+
|
|
146
|
+
3. If you wish to support refresh tokens, repeat step 2 for the
|
|
147
|
+
`app.rt` cookie.
|
|
148
|
+
|
|
149
|
+
4. Save the expiration time in a readable `app.at_exp` cookie. And save the `app.idt` id token in a readable cookie.
|
|
150
|
+
|
|
151
|
+
5. Redirect browser back to encoded url saved in `state`.
|
|
152
|
+
|
|
153
|
+
4. Call
|
|
154
|
+
[/oauth2/userinfo](https://fusionauth.io/docs/v1/tech/oauth/endpoints#userinfo)
|
|
155
|
+
to retrieve the user info object and respond back to the client with
|
|
156
|
+
this object.
|
|
157
|
+
|
|
158
|
+
[Example
|
|
159
|
+
implementation](https://github.com/FusionAuth/fusionauth-example-angular-sdk/blob/main/server/routes/callback.js)
|
|
160
|
+
|
|
161
|
+
### `GET /app/register`
|
|
162
|
+
|
|
163
|
+
This endpoint is similar to `/login`. It must:
|
|
164
|
+
|
|
165
|
+
1. Generate PKCE code.
|
|
166
|
+
a. The code verifier should be saved in a secure HTTP-only cookie.
|
|
167
|
+
b. The code challenge is passed along
|
|
168
|
+
2. Encode and save `redirect_url` from angular app to `state`.
|
|
169
|
+
3. Redirect browser to `/oauth2/register` with a `redirect_uri` to `/app/callback`
|
|
170
|
+
|
|
171
|
+
[Example
|
|
172
|
+
implementation](https://github.com/FusionAuth/fusionauth-example-angular-sdk/blob/main/server/routes/register.js)
|
|
173
|
+
|
|
174
|
+
### `GET /app/me`
|
|
175
|
+
|
|
176
|
+
This endpoint must:
|
|
177
|
+
|
|
178
|
+
1. Use `app.at` from cookie and use as the Bearer token to call `/oauth2/userinfo`
|
|
179
|
+
2. Return json data
|
|
180
|
+
|
|
181
|
+
[Example
|
|
182
|
+
implementation](https://github.com/FusionAuth/fusionauth-example-angular-sdk/blob/main/server/routes/me.js)
|
|
183
|
+
|
|
184
|
+
### `GET /app/logout`
|
|
185
|
+
|
|
186
|
+
This endpoint must:
|
|
187
|
+
|
|
188
|
+
1. Clear the `app.at` and `app.rt` secure, HTTP-only
|
|
189
|
+
cookies.
|
|
190
|
+
2. Clear the `app.at_exp` and `app.idt` secure cookies.
|
|
191
|
+
3. Redirect to `/oauth2/logout`
|
|
192
|
+
|
|
193
|
+
[Example
|
|
194
|
+
implementation](https://github.com/FusionAuth/fusionauth-example-angular-sdk/blob/main/server/routes/logout.js)
|
|
195
|
+
|
|
196
|
+
### `POST /app/refresh` (optional)
|
|
197
|
+
|
|
198
|
+
This endpoint is necessary if you wish to use refresh tokens. This
|
|
199
|
+
endpoint must:
|
|
200
|
+
|
|
201
|
+
1. Call
|
|
202
|
+
[/oauth2/token](https://fusionauth.io/docs/v1/tech/oauth/endpoints#refresh-token-grant-request)
|
|
203
|
+
to get a new `app.at` and `app.rt`.
|
|
204
|
+
|
|
205
|
+
2. Update the `app.at`, `app.at_exp`, `app.idt`, and `app.rt` cookies from the
|
|
206
|
+
response.
|
|
207
|
+
|
|
208
|
+
[Example
|
|
209
|
+
implementation](https://github.com/FusionAuth/fusionauth-example-angular-sdk/blob/main/server/routes/token-refresh.js)
|
|
210
|
+
|
|
211
|
+
# Usage
|
|
212
|
+
|
|
213
|
+
## Pre-built buttons
|
|
214
|
+
|
|
215
|
+
There are three pre-styled buttons that are configured to perform
|
|
216
|
+
login/logout/registration. They can be placed anywhere in your app as
|
|
217
|
+
is.
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { Component } from '@angular/core';
|
|
221
|
+
|
|
222
|
+
@Component({
|
|
223
|
+
selector: 'app-login',
|
|
224
|
+
template: `<fa-login></fa-login>`,
|
|
225
|
+
styleUrls: []
|
|
226
|
+
})
|
|
227
|
+
export class LoginComponent {}
|
|
228
|
+
|
|
229
|
+
@Component({
|
|
230
|
+
selector: 'app-logout',
|
|
231
|
+
template: `<fa-logout></fa-logout>`,
|
|
232
|
+
styleUrls: []
|
|
233
|
+
})
|
|
234
|
+
export class LogoutComponent {}
|
|
235
|
+
|
|
236
|
+
@Component({
|
|
237
|
+
selector: 'app-register',
|
|
238
|
+
template: `<fa-register></fa-register>`,
|
|
239
|
+
styleUrls: []
|
|
240
|
+
})
|
|
241
|
+
export class RegisterComponent {}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Service usage
|
|
245
|
+
|
|
246
|
+
Alternatively, you may interact with the SDK Service by injecting the FusionAuthService into any component or service.
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
import { Component, OnInit } from '@angular/core';
|
|
250
|
+
import { FusionAuthService, UserInfo } from '@fusionauth/angular-sdk';
|
|
251
|
+
|
|
252
|
+
@Component({
|
|
253
|
+
selector: 'app-root',
|
|
254
|
+
templateUrl: './app.component.html',
|
|
255
|
+
styleUrls: ['./app.component.scss']
|
|
256
|
+
})
|
|
257
|
+
export class AppComponent implements OnInit {
|
|
258
|
+
private userInfo: UserInfo;
|
|
259
|
+
|
|
260
|
+
constructor(
|
|
261
|
+
private fusionAuth: FusionAuthService,
|
|
262
|
+
) {}
|
|
263
|
+
|
|
264
|
+
async ngOnInit(): Promise<void> {
|
|
265
|
+
this.fusionAuth.initAutoRefresh();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
login() {
|
|
269
|
+
this.fusionAuth.startLogin();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
register() {
|
|
273
|
+
this.fusionAuth.startRegistration();
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
logout() {
|
|
277
|
+
this.fusionAuth.logout();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
refreshToken() {
|
|
281
|
+
this.fusionAuth.refreshToken();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async getUserInfo() {
|
|
285
|
+
this.userInfo = await this.fusionAuth.getUserInfo();
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
isLoggedIn(): boolean {
|
|
289
|
+
return this.fusionAuth.isLoggedIn();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### State parameter
|
|
295
|
+
|
|
296
|
+
The `startLogin` and `startRegistration` functions both accept an optional string
|
|
297
|
+
parameter called `state`. The login and register components can also be passed the
|
|
298
|
+
state as an input. The state that is passed in to the function call will be echoed
|
|
299
|
+
back in the state query parameter of the callback uri specified in `redirectUri` on
|
|
300
|
+
the `FusionAuthConfig` used to initialize the `FusionAuthModule`. Though you may
|
|
301
|
+
pass any value you would like for the state parameter, it is often used to indicate
|
|
302
|
+
which page the user was on before redirecting to login or registration, so that the
|
|
303
|
+
user can be returned to that location after a successful authentication.
|
|
304
|
+
|
|
305
|
+
## Known Issues
|
|
306
|
+
|
|
307
|
+
# Example App
|
|
308
|
+
|
|
309
|
+
See the [FusionAuth Angular SDK
|
|
310
|
+
Example](https://github.com/FusionAuth/fusionauth-example-angular-sdk) for
|
|
311
|
+
functional example of a Angular client that utilizes the SDK as well as an
|
|
312
|
+
Express server that performs the token exchange.
|
|
313
|
+
|
|
314
|
+
# Documentation
|
|
315
|
+
|
|
316
|
+
[Full library
|
|
317
|
+
documentation](https://github.com/FusionAuth/fusionauth-angular-sdk/blob/main/docs/documentation.md)
|
|
318
|
+
|
|
319
|
+
<!--
|
|
320
|
+
end::forDocSite[]
|
|
321
|
+
-->
|
|
322
|
+
|
|
323
|
+
Use backticks for code in this readme. This readme gets turned into asciidoc and included on the fusionauth website, and backticks show the code in the best light there.
|
|
324
|
+
|
|
325
|
+
# Releases
|
|
326
|
+
|
|
327
|
+
To perform a release:
|
|
328
|
+
|
|
329
|
+
- Pull the code to your local machine
|
|
330
|
+
|
|
331
|
+
- Bump the version in [package.json](./projects/fusionauth-angular-sdk/package.json)
|
|
332
|
+
|
|
333
|
+
- Run `npm run build-sdk`
|
|
334
|
+
|
|
335
|
+
- Run `cd dist/fusionauth-angular-sdk`
|
|
336
|
+
|
|
337
|
+
- Run `npm publish`
|
|
338
|
+
|
|
339
|
+
You may have to set up your machine to be able to publish, which will
|
|
340
|
+
involve updating your .npmrc file.
|
|
341
|
+
|
|
342
|
+
There’s information [here that you can
|
|
343
|
+
use](https://dev.to/alexeagleson/how-to-create-and-publish-a-react-component-library-2oe)
|
|
344
|
+
to do that (look for the `.npmrc` section).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fusionauth/angular-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"@angular/common": "^13.3.0",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"access": "public"
|
|
14
14
|
},
|
|
15
15
|
"repository": "git@github.com:FusionAuth/fusionauth-angular-sdk.git",
|
|
16
|
+
"readme": "",
|
|
16
17
|
"module": "fesm2015/fusionauth-angular-sdk.mjs",
|
|
17
18
|
"es2020": "fesm2020/fusionauth-angular-sdk.mjs",
|
|
18
19
|
"esm2020": "esm2020/fusionauth-angular-sdk.mjs",
|