idenplane-angular 1.0.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 +141 -0
- package/package.json +61 -0
package/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# idenplane-angular
|
|
2
|
+
|
|
3
|
+
Angular SDK for [Idenplane](https://github.com/idenplane/idenplane) — an injectable `AuthService`, a route guard, an HTTP interceptor, and an `NgModule` entry point.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install idenplane-angular idenplane-sdk rxjs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### 1. Import `IdenplaneModule.forRoot()`
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// app.module.ts
|
|
17
|
+
import { NgModule } from '@angular/core';
|
|
18
|
+
import { HttpClientModule } from '@angular/common/http';
|
|
19
|
+
import { IdenplaneModule } from 'idenplane-angular';
|
|
20
|
+
|
|
21
|
+
@NgModule({
|
|
22
|
+
imports: [
|
|
23
|
+
HttpClientModule,
|
|
24
|
+
IdenplaneModule.forRoot({
|
|
25
|
+
url: 'http://localhost:3000',
|
|
26
|
+
realm: 'my-realm',
|
|
27
|
+
clientId: 'my-app',
|
|
28
|
+
redirectUri: 'http://localhost:4200/callback',
|
|
29
|
+
}),
|
|
30
|
+
],
|
|
31
|
+
bootstrap: [AppComponent],
|
|
32
|
+
})
|
|
33
|
+
export class AppModule {}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 2. Inject `AuthService`
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Component } from '@angular/core';
|
|
40
|
+
import { AuthService } from 'idenplane-angular';
|
|
41
|
+
|
|
42
|
+
@Component({
|
|
43
|
+
selector: 'app-nav',
|
|
44
|
+
template: `
|
|
45
|
+
<ng-container *ngIf="auth.isLoading$ | async; else content">
|
|
46
|
+
<span>Loading...</span>
|
|
47
|
+
</ng-container>
|
|
48
|
+
<ng-template #content>
|
|
49
|
+
<button *ngIf="!(auth.isAuthenticated$ | async)" (click)="auth.login()">
|
|
50
|
+
Sign In
|
|
51
|
+
</button>
|
|
52
|
+
<div *ngIf="auth.isAuthenticated$ | async">
|
|
53
|
+
<span>{{ (auth.user$ | async)?.name }}</span>
|
|
54
|
+
<button (click)="auth.logout()">Sign Out</button>
|
|
55
|
+
</div>
|
|
56
|
+
</ng-template>
|
|
57
|
+
`,
|
|
58
|
+
})
|
|
59
|
+
export class NavComponent {
|
|
60
|
+
constructor(public auth: AuthService) {}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 3. Protect routes with `AuthGuard`
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// app.routes.ts
|
|
68
|
+
import { Routes } from '@angular/router';
|
|
69
|
+
import { AuthGuard } from 'idenplane-angular';
|
|
70
|
+
|
|
71
|
+
export const routes: Routes = [
|
|
72
|
+
{ path: '', component: HomeComponent },
|
|
73
|
+
{
|
|
74
|
+
path: 'dashboard',
|
|
75
|
+
component: DashboardComponent,
|
|
76
|
+
canActivate: [AuthGuard],
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
path: 'admin',
|
|
80
|
+
component: AdminComponent,
|
|
81
|
+
canActivate: [AuthGuard],
|
|
82
|
+
data: { roles: ['admin'] },
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 4. Automatically attach Bearer tokens (HTTP interceptor)
|
|
88
|
+
|
|
89
|
+
The HTTP interceptor is registered automatically when you use `IdenplaneModule.forRoot()`.
|
|
90
|
+
|
|
91
|
+
For standalone / functional interceptor style (Angular 15+):
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
// app.config.ts
|
|
95
|
+
import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
|
96
|
+
import { authInterceptor } from 'idenplane-angular';
|
|
97
|
+
|
|
98
|
+
export const appConfig = {
|
|
99
|
+
providers: [
|
|
100
|
+
provideHttpClient(withInterceptors([authInterceptor])),
|
|
101
|
+
],
|
|
102
|
+
};
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## API Reference
|
|
106
|
+
|
|
107
|
+
### `IdenplaneModule`
|
|
108
|
+
|
|
109
|
+
- `IdenplaneModule.forRoot(config: IdenplaneConfig)` — registers all providers
|
|
110
|
+
|
|
111
|
+
### `AuthService`
|
|
112
|
+
|
|
113
|
+
| Member | Type | Description |
|
|
114
|
+
|--------|------|-------------|
|
|
115
|
+
| `isAuthenticated$` | `Observable<boolean>` | Reactive auth state |
|
|
116
|
+
| `isLoading$` | `Observable<boolean>` | True while initializing |
|
|
117
|
+
| `user$` | `Observable<UserInfo \| null>` | Current user |
|
|
118
|
+
| `isAuthenticated` | `boolean` | Synchronous getter |
|
|
119
|
+
| `user` | `UserInfo \| null` | Synchronous getter |
|
|
120
|
+
| `login(opts?)` | `Promise<void>` | Redirect to login |
|
|
121
|
+
| `logout()` | `Promise<void>` | Log out |
|
|
122
|
+
| `getToken()` | `string \| null` | Current access token |
|
|
123
|
+
| `hasRole(role)` | `boolean` | Realm role check |
|
|
124
|
+
| `hasPermission(perm)` | `boolean` | Permission check |
|
|
125
|
+
| `getRoles()` | `string[]` | All realm roles |
|
|
126
|
+
|
|
127
|
+
### `AuthGuard`
|
|
128
|
+
|
|
129
|
+
Route guard implementing `CanActivate`. Reads `route.data.roles` and `route.data.loginPath`.
|
|
130
|
+
|
|
131
|
+
### `AuthInterceptor`
|
|
132
|
+
|
|
133
|
+
Class-based HTTP interceptor. Adds `Authorization: Bearer <token>` to requests.
|
|
134
|
+
|
|
135
|
+
### `authInterceptor`
|
|
136
|
+
|
|
137
|
+
Functional interceptor for use with `withInterceptors([authInterceptor])`.
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "idenplane-angular",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Angular SDK for Idenplane Identity and Access Management Server",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@angular/common": ">=17.0.0",
|
|
18
|
+
"@angular/core": ">=17.0.0",
|
|
19
|
+
"@angular/router": ">=17.0.0",
|
|
20
|
+
"idenplane-sdk": ">=1.0.0",
|
|
21
|
+
"rxjs": ">=7.0.0"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.json",
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"test": "vitest run",
|
|
27
|
+
"test:watch": "vitest"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@angular/common": "^22.0.1",
|
|
31
|
+
"@angular/core": "^22.0.1",
|
|
32
|
+
"@angular/router": "^22.0.1",
|
|
33
|
+
"idenplane-sdk": "file:../idenplane-js",
|
|
34
|
+
"rxjs": "^7.8.0",
|
|
35
|
+
"typescript": "^5.7.0",
|
|
36
|
+
"vitest": "^4.0.18",
|
|
37
|
+
"zone.js": "^0.15.0"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"README.md"
|
|
42
|
+
],
|
|
43
|
+
"keywords": [
|
|
44
|
+
"idenplane",
|
|
45
|
+
"oauth2",
|
|
46
|
+
"oidc",
|
|
47
|
+
"angular",
|
|
48
|
+
"authentication",
|
|
49
|
+
"interceptor",
|
|
50
|
+
"guard"
|
|
51
|
+
],
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "https://github.com/idenplane/idenplane.git",
|
|
56
|
+
"directory": "packages/idenplane-angular"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
}
|
|
61
|
+
}
|