@ratespecial/logto-angular 1.0.0
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 +507 -0
- package/fesm2022/ratespecial-logto-angular-testing.mjs +49 -0
- package/fesm2022/ratespecial-logto-angular-testing.mjs.map +1 -0
- package/fesm2022/ratespecial-logto-angular.mjs +396 -0
- package/fesm2022/ratespecial-logto-angular.mjs.map +1 -0
- package/package.json +42 -0
- package/types/ratespecial-logto-angular-testing.d.ts +15 -0
- package/types/ratespecial-logto-angular.d.ts +270 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import LogtoClient, { AccessTokenClaims, IdTokenClaims, LogtoConfig } from '@logto/browser';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import * as i0 from '@angular/core';
|
|
4
|
+
import { InjectionToken, EnvironmentProviders, OnInit } from '@angular/core';
|
|
5
|
+
import { Routes, CanActivateFn } from '@angular/router';
|
|
6
|
+
import { HttpInterceptorFn } from '@angular/common/http';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Angular-friendly facade over the promise-based `@logto/browser` client. Owns the
|
|
10
|
+
* authenticated-state signal, sign-in/out, and resource-scoped token access.
|
|
11
|
+
*/
|
|
12
|
+
declare class AuthService {
|
|
13
|
+
private router;
|
|
14
|
+
private client;
|
|
15
|
+
private routing;
|
|
16
|
+
private logoutHooks;
|
|
17
|
+
private authenticated$;
|
|
18
|
+
/**
|
|
19
|
+
* Authenticated state as a stream. Backed by a `BehaviorSubject`, so it replays the current
|
|
20
|
+
* value on subscribe (order-independent for late subscribers) and only emits on change.
|
|
21
|
+
*/
|
|
22
|
+
readonly isAuthenticated$: Observable<boolean>;
|
|
23
|
+
/**
|
|
24
|
+
* Re-reads the client's authenticated state (checks for a stored session; no network) and
|
|
25
|
+
* pushes it to the stream. Returns the resolved value.
|
|
26
|
+
*/
|
|
27
|
+
refreshAuthState(): Promise<boolean>;
|
|
28
|
+
/** Begin a sign-in flow with Logto (full-page redirect to the hosted UI). */
|
|
29
|
+
signIn(redirectUri?: string): void;
|
|
30
|
+
/** Complete the OAuth callback, then refresh local auth state. */
|
|
31
|
+
handleCallback(callbackUri: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Fetch a resource-scoped JWT. The client refreshes/caches transparently, so this is safe to
|
|
34
|
+
* call on every request. Omit `resource` for an opaque (userinfo) token.
|
|
35
|
+
*/
|
|
36
|
+
getAccessToken(resource?: string): Promise<string>;
|
|
37
|
+
/** Decoded claims of the resource-scoped access token (e.g. to inspect `scope`). */
|
|
38
|
+
getAccessTokenClaims(resource?: string): Promise<AccessTokenClaims>;
|
|
39
|
+
/**
|
|
40
|
+
* Decoded claims of the ID token (e.g. `sub`, `name`, `email`, `picture`). Reads the token
|
|
41
|
+
* already in storage and decodes it locally — no network call.
|
|
42
|
+
*/
|
|
43
|
+
getIdTokenClaims(): Promise<IdTokenClaims>;
|
|
44
|
+
/** Sign out via Logto and reset local state. */
|
|
45
|
+
logout(): void;
|
|
46
|
+
protected fireLogoutHooks(): void;
|
|
47
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<AuthService, never>;
|
|
48
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<AuthService>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Facade for tracking the last visited route, used to restore navigation after login.
|
|
53
|
+
* Backed by sessionStorage so the value is scoped to the browser session.
|
|
54
|
+
*/
|
|
55
|
+
declare class HistoryService {
|
|
56
|
+
setLastVisitedRoute(route: string): void;
|
|
57
|
+
getLastVisitedRoute(): string | null;
|
|
58
|
+
clearLastVisitedRoute(): void;
|
|
59
|
+
consumeLastVisitedRoute(): string | null;
|
|
60
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<HistoryService, never>;
|
|
61
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<HistoryService>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Maps outgoing request URLs to the Logto API resource whose access token the HTTP
|
|
66
|
+
* interceptor should attach. This is the one piece `@logto/browser` does not model —
|
|
67
|
+
* the SDK issues per-resource tokens but has no concept of which requests need them.
|
|
68
|
+
*/
|
|
69
|
+
interface SecureRouteMapping {
|
|
70
|
+
/** Logto API resource indicator to fetch a token for. */
|
|
71
|
+
resource: string;
|
|
72
|
+
/** Request URL prefixes (matched with `startsWith`) that require this resource's token. */
|
|
73
|
+
routes: string[];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* App-level routing/behavior config that sits alongside the native `LogtoConfig`.
|
|
77
|
+
*/
|
|
78
|
+
interface LogtoRoutingConfig {
|
|
79
|
+
/** Path Logto redirects back to after sign-in. Used by `signIn()` and the route helper. */
|
|
80
|
+
callbackPath: string;
|
|
81
|
+
/** Path shown after sign-out. Used by `logout()` and the route helper. */
|
|
82
|
+
signedOutPath: string;
|
|
83
|
+
/**
|
|
84
|
+
* The primary API resource — used where a specific resource token is needed outside the
|
|
85
|
+
* interceptor (e.g. the Echo/Pusher auth header) and for the post-callback access gate.
|
|
86
|
+
* Defaults to the first `secureRoutes` resource when omitted.
|
|
87
|
+
*/
|
|
88
|
+
primaryResource?: string;
|
|
89
|
+
/** Maps request URLs to the resource token the interceptor attaches. */
|
|
90
|
+
secureRoutes: SecureRouteMapping[];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Full auth configuration: `@logto/browser`'s native `LogtoConfig` (endpoint, appId,
|
|
94
|
+
* scopes, resources, prompt, …) plus the app-level `routing` addon this library needs.
|
|
95
|
+
* Provided app-wide under the `LOGTO_AUTH_CONFIG` token.
|
|
96
|
+
*/
|
|
97
|
+
interface LogtoAuthConfig extends LogtoConfig {
|
|
98
|
+
routing: LogtoRoutingConfig;
|
|
99
|
+
/** Message shown when an authenticated user has no scopes on the primary resource. */
|
|
100
|
+
noAccessMessage?: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The resolved auth configuration (native Logto config + `routing` addon). Every part of
|
|
105
|
+
* the library reads from this token, so nothing depends on the consuming app's environment.
|
|
106
|
+
*/
|
|
107
|
+
declare const LOGTO_AUTH_CONFIG: InjectionToken<LogtoAuthConfig>;
|
|
108
|
+
/**
|
|
109
|
+
* The single, app-wide `@logto/browser` client. One client handles every resource:
|
|
110
|
+
* `getAccessToken(resource)` exchanges the shared refresh token for a resource-scoped JWT
|
|
111
|
+
* on demand and caches it.
|
|
112
|
+
*/
|
|
113
|
+
declare const LOGTO_CLIENT: InjectionToken<LogtoClient>;
|
|
114
|
+
/**
|
|
115
|
+
* The primary API resource (`routing.primaryResource`, or the first secure-route resource).
|
|
116
|
+
* Used where a specific resource token is needed outside the HTTP interceptor — e.g. the
|
|
117
|
+
* Echo/Pusher auth header and the post-callback access gate.
|
|
118
|
+
*/
|
|
119
|
+
declare const PRIMARY_RESOURCE: InjectionToken<string>;
|
|
120
|
+
/**
|
|
121
|
+
* A side-effect callback the library runs at the start of `AuthService.logout()`, before the
|
|
122
|
+
* sign-out redirect. Use for app teardown such as clearing client-side state or flushing caches.
|
|
123
|
+
*
|
|
124
|
+
* Return `void` for synchronous work or an `Observable` for async work. Async hooks are
|
|
125
|
+
* fire-and-forget — the library does not wait for them before logging the user off.
|
|
126
|
+
*/
|
|
127
|
+
type AuthLogoutHook = () => void | Observable<unknown>;
|
|
128
|
+
/**
|
|
129
|
+
* Multi-provider token that collects `AuthLogoutHook` callbacks invoked during logout.
|
|
130
|
+
*
|
|
131
|
+
* Ordering between hooks follows Angular DI registration order. Register one hook per concern
|
|
132
|
+
* (state reset, cache flush, telemetry, …) rather than bundling them.
|
|
133
|
+
*
|
|
134
|
+
* Prefer registering hooks via `provideLogtoAuth({ logoutHookFactories: [...] })` over wiring
|
|
135
|
+
* this token directly — the factory form runs inside an injection context so the hook can
|
|
136
|
+
* use `inject()`.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* provideLogtoAuth({
|
|
141
|
+
* endpoint: environment.logto.endpoint,
|
|
142
|
+
* appId: environment.logto.appId,
|
|
143
|
+
* routing: environment.logto.routing,
|
|
144
|
+
* logoutHookFactories: [
|
|
145
|
+
* () => {
|
|
146
|
+
* const store = inject(Store);
|
|
147
|
+
* return () => store.dispatch(new ClearState());
|
|
148
|
+
* },
|
|
149
|
+
* ],
|
|
150
|
+
* })
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
declare const AUTH_LOGOUT_HOOK: InjectionToken<AuthLogoutHook[]>;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Configuration passed to `provideLogtoAuth()`: the native `LogtoConfig` fields plus the
|
|
157
|
+
* `routing` addon, with optional logout hooks.
|
|
158
|
+
*/
|
|
159
|
+
interface LogtoAuthOptions extends LogtoAuthConfig {
|
|
160
|
+
/**
|
|
161
|
+
* Factories that produce `AuthLogoutHook` callbacks. Each factory runs inside an injection
|
|
162
|
+
* context, so it may use `inject()` to obtain app services (e.g. an NGXS `Store`) when
|
|
163
|
+
* building its hook. The hooks run at the start of `AuthService.logout()`.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* logoutHookFactories: [
|
|
168
|
+
* () => {
|
|
169
|
+
* const store = inject(Store);
|
|
170
|
+
* return () => store.dispatch(new ClearState());
|
|
171
|
+
* },
|
|
172
|
+
* ]
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
logoutHookFactories?: (() => AuthLogoutHook)[];
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Wires up the auth library: provides the resolved config, constructs the single
|
|
179
|
+
* `@logto/browser` client, resolves the primary resource, contributes any
|
|
180
|
+
* `logoutHookFactories` to the `AUTH_LOGOUT_HOOK` multi-provider, and hydrates the
|
|
181
|
+
* authenticated-state signal from any existing session before the first guard runs.
|
|
182
|
+
*
|
|
183
|
+
* Returned providers are environment-scoped — call this once at the root provider list.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* providers: [
|
|
188
|
+
* provideLogtoAuth({
|
|
189
|
+
* ...environment.logto,
|
|
190
|
+
* logoutHookFactories: [
|
|
191
|
+
* () => {
|
|
192
|
+
* const store = inject(Store);
|
|
193
|
+
* return () => store.dispatch(new ClearState());
|
|
194
|
+
* },
|
|
195
|
+
* ],
|
|
196
|
+
* }),
|
|
197
|
+
* ]
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
declare function provideLogtoAuth(options: LogtoAuthOptions): EnvironmentProviders;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Returns the auth routes (callback + signed-out landing) for the configured paths. Spread the
|
|
204
|
+
* result into the app's top-level `Routes`. Passing the same `routing` config used by
|
|
205
|
+
* `provideLogtoAuth()` keeps the route definitions and the redirect URIs from drifting apart.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```ts
|
|
209
|
+
* export const routes: Routes = [
|
|
210
|
+
* {path: '', pathMatch: 'full', redirectTo: '/dashboard'},
|
|
211
|
+
* ...getAuthRoutes(environment.logto.routing),
|
|
212
|
+
* // ...app routes
|
|
213
|
+
* ];
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
declare function getAuthRoutes(routing: Pick<LogtoRoutingConfig, 'callbackPath' | 'signedOutPath'>): Routes;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Replaces `autoLoginPartialRoutesGuard`. Allows activation when a Logto session
|
|
220
|
+
* exists; otherwise records the attempted route and kicks off a sign-in redirect.
|
|
221
|
+
*/
|
|
222
|
+
declare const authGuard: CanActivateFn;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Picks the configured resource whose `routes` match a request URL, so the interceptor can
|
|
226
|
+
* fetch the correct resource-scoped token. Returns `undefined` when no resource matches (the
|
|
227
|
+
* request goes out without an Authorization header).
|
|
228
|
+
*/
|
|
229
|
+
declare function resourceForUrl(url: string, secureRoutes: SecureRouteMapping[]): string | undefined;
|
|
230
|
+
/**
|
|
231
|
+
* Matches the outgoing URL against each configured resource's `routes`; on a match it fetches
|
|
232
|
+
* that resource's access token (cached/refreshed by the Logto client) and attaches it as a
|
|
233
|
+
* Bearer header. Requests that match no resource pass through unauthenticated.
|
|
234
|
+
*/
|
|
235
|
+
declare const logtoTokenInterceptor: HttpInterceptorFn;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* If any /api call returns 401, sign the user out and send them to the login page.
|
|
239
|
+
*/
|
|
240
|
+
declare const logoutOnUnauthInterceptor: HttpInterceptorFn;
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Initializer that tracks the last visited route.
|
|
244
|
+
* Excludes auth routes (/auth/*) to prevent redirect loops.
|
|
245
|
+
*/
|
|
246
|
+
declare function initializeRouteTracking(): () => void;
|
|
247
|
+
|
|
248
|
+
declare class CallbackComponent implements OnInit {
|
|
249
|
+
private authService;
|
|
250
|
+
private router;
|
|
251
|
+
private historyService;
|
|
252
|
+
private primaryResource;
|
|
253
|
+
private noAccessMessage;
|
|
254
|
+
loading: i0.WritableSignal<boolean>;
|
|
255
|
+
error: i0.WritableSignal<string | null>;
|
|
256
|
+
ngOnInit(): Promise<void>;
|
|
257
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<CallbackComponent, never>;
|
|
258
|
+
static ɵcmp: i0.ɵɵComponentDeclaration<CallbackComponent, "lib-callback", never, {}, {}, never, never, true, never>;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
declare class SignedOutComponent {
|
|
262
|
+
private authService;
|
|
263
|
+
/** Restart the Logto sign-in flow (redirects to the hosted UI). */
|
|
264
|
+
signIn(): void;
|
|
265
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<SignedOutComponent, never>;
|
|
266
|
+
static ɵcmp: i0.ɵɵComponentDeclaration<SignedOutComponent, "lib-signed-out", never, {}, {}, never, never, true, never>;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export { AUTH_LOGOUT_HOOK, AuthService, CallbackComponent, HistoryService, LOGTO_AUTH_CONFIG, LOGTO_CLIENT, PRIMARY_RESOURCE, SignedOutComponent, authGuard, getAuthRoutes, initializeRouteTracking, logoutOnUnauthInterceptor, logtoTokenInterceptor, provideLogtoAuth, resourceForUrl };
|
|
270
|
+
export type { AuthLogoutHook, LogtoAuthConfig, LogtoAuthOptions, LogtoRoutingConfig, SecureRouteMapping };
|