@nauth-toolkit/client-angular 0.1.56 → 0.1.58
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/esm2022/lib/auth.guard.mjs +83 -0
- package/esm2022/lib/auth.interceptor.mjs +158 -0
- package/esm2022/lib/social-redirect-callback.guard.mjs +81 -0
- package/esm2022/nauth-toolkit-client-angular.mjs +5 -0
- package/esm2022/ngmodule/auth.interceptor.class.mjs +109 -0
- package/esm2022/ngmodule/auth.service.mjs +777 -0
- package/esm2022/ngmodule/http-adapter.mjs +127 -0
- package/esm2022/ngmodule/nauth.module.mjs +65 -0
- package/esm2022/ngmodule/tokens.mjs +6 -0
- package/esm2022/public-api.mjs +19 -0
- package/esm2022/src/standalone/nauth-toolkit-client-angular-src-standalone.mjs +5 -0
- package/esm2022/src/standalone/public-api.mjs +12 -0
- package/esm2022/standalone/auth.guard.mjs +83 -0
- package/esm2022/standalone/auth.interceptor.mjs +158 -0
- package/esm2022/standalone/auth.service.mjs +777 -0
- package/esm2022/standalone/http-adapter.mjs +127 -0
- package/esm2022/standalone/nauth-toolkit-client-angular-standalone.mjs +5 -0
- package/esm2022/standalone/public-api.mjs +16 -0
- package/esm2022/standalone/social-redirect-callback.guard.mjs +81 -0
- package/esm2022/standalone/tokens.mjs +6 -0
- package/fesm2022/nauth-toolkit-client-angular-src-standalone.mjs +17 -0
- package/fesm2022/nauth-toolkit-client-angular-src-standalone.mjs.map +1 -0
- package/fesm2022/nauth-toolkit-client-angular-standalone.mjs +1229 -0
- package/fesm2022/nauth-toolkit-client-angular-standalone.mjs.map +1 -0
- package/fesm2022/nauth-toolkit-client-angular.mjs +1390 -0
- package/fesm2022/nauth-toolkit-client-angular.mjs.map +1 -0
- package/index.d.ts +5 -0
- package/{src/lib/auth.guard.ts → lib/auth.guard.d.ts} +15 -37
- package/lib/auth.interceptor.d.ts +15 -0
- package/lib/social-redirect-callback.guard.d.ts +25 -0
- package/ngmodule/auth.interceptor.class.d.ts +34 -0
- package/ngmodule/auth.service.d.ts +580 -0
- package/ngmodule/http-adapter.d.ts +53 -0
- package/ngmodule/nauth.module.d.ts +31 -0
- package/{src/ngmodule/tokens.ts → ngmodule/tokens.d.ts} +1 -2
- package/package.json +30 -20
- package/{src/public-api.ts → public-api.d.ts} +0 -6
- package/src/standalone/index.d.ts +5 -0
- package/src/standalone/{public-api.ts → public-api.d.ts} +0 -2
- package/standalone/{auth.guard.ts → auth.guard.d.ts} +15 -37
- package/standalone/auth.interceptor.d.ts +15 -0
- package/standalone/auth.service.d.ts +580 -0
- package/standalone/http-adapter.d.ts +53 -0
- package/standalone/index.d.ts +5 -0
- package/standalone/{public-api.ts → public-api.d.ts} +1 -6
- package/standalone/social-redirect-callback.guard.d.ts +25 -0
- package/standalone/{tokens.ts → tokens.d.ts} +1 -2
- package/ng-package.json +0 -12
- package/src/lib/auth.interceptor.ts +0 -194
- package/src/lib/social-redirect-callback.guard.ts +0 -87
- package/src/ngmodule/auth.interceptor.class.ts +0 -124
- package/src/ngmodule/auth.service.ts +0 -865
- package/src/ngmodule/http-adapter.ts +0 -79
- package/src/ngmodule/nauth.module.ts +0 -59
- package/src/package.json +0 -11
- package/src/standalone/ng-package.json +0 -7
- package/src/standalone/package.json +0 -8
- package/standalone/auth.interceptor.ts +0 -194
- package/standalone/auth.service.ts +0 -865
- package/standalone/http-adapter.ts +0 -79
- package/standalone/ng-package.json +0 -7
- package/standalone/package.json +0 -8
- package/standalone/social-redirect-callback.guard.ts +0 -87
- package/tsconfig.json +0 -10
- package/tsconfig.lib.json +0 -28
- package/tsconfig.lib.prod.json +0 -10
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@angular/core';
|
|
2
|
-
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
|
3
|
-
import { firstValueFrom } from 'rxjs';
|
|
4
|
-
import { HttpAdapter, HttpRequest, HttpResponse, NAuthClientError, NAuthErrorCode } from '@nauth-toolkit/client';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* HTTP adapter for Angular using HttpClient.
|
|
8
|
-
*
|
|
9
|
-
* This adapter:
|
|
10
|
-
* - Uses Angular's HttpClient for all requests
|
|
11
|
-
* - Works with Angular's HTTP interceptors (including authInterceptor)
|
|
12
|
-
* - Auto-provided via Angular DI (providedIn: 'root')
|
|
13
|
-
* - Converts HttpClient responses to HttpResponse format
|
|
14
|
-
* - Converts HttpErrorResponse to NAuthClientError
|
|
15
|
-
*
|
|
16
|
-
* Users don't need to configure this manually - it's automatically
|
|
17
|
-
* injected when using AuthService in Angular apps.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```typescript
|
|
21
|
-
* // Automatic usage (no manual setup needed)
|
|
22
|
-
* // AuthService automatically injects AngularHttpAdapter
|
|
23
|
-
* constructor(private auth: AuthService) {}
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
@Injectable()
|
|
27
|
-
export class AngularHttpAdapter implements HttpAdapter {
|
|
28
|
-
constructor(private readonly http: HttpClient) {}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Execute HTTP request using Angular's HttpClient.
|
|
32
|
-
*
|
|
33
|
-
* @param config - Request configuration
|
|
34
|
-
* @returns Response with parsed data
|
|
35
|
-
* @throws NAuthClientError if request fails
|
|
36
|
-
*/
|
|
37
|
-
async request<T>(config: HttpRequest): Promise<HttpResponse<T>> {
|
|
38
|
-
try {
|
|
39
|
-
// Use Angular's HttpClient - goes through ALL interceptors
|
|
40
|
-
const data = await firstValueFrom(
|
|
41
|
-
this.http.request<T>(config.method, config.url, {
|
|
42
|
-
body: config.body,
|
|
43
|
-
headers: config.headers,
|
|
44
|
-
withCredentials: config.credentials === 'include',
|
|
45
|
-
observe: 'body', // Only return body data
|
|
46
|
-
}),
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
data,
|
|
51
|
-
status: 200, // HttpClient only returns data on success
|
|
52
|
-
headers: {}, // Can extract from observe: 'response' if needed
|
|
53
|
-
};
|
|
54
|
-
} catch (error) {
|
|
55
|
-
if (error instanceof HttpErrorResponse) {
|
|
56
|
-
// Convert Angular's HttpErrorResponse to NAuthClientError
|
|
57
|
-
const errorData = error.error || {};
|
|
58
|
-
const code =
|
|
59
|
-
typeof errorData['code'] === 'string' ? (errorData.code as NAuthErrorCode) : NAuthErrorCode.INTERNAL_ERROR;
|
|
60
|
-
const message =
|
|
61
|
-
typeof errorData['message'] === 'string'
|
|
62
|
-
? (errorData.message as string)
|
|
63
|
-
: error.message || `Request failed with status ${error.status}`;
|
|
64
|
-
const timestamp = typeof errorData['timestamp'] === 'string' ? errorData.timestamp : undefined;
|
|
65
|
-
const details = errorData['details'] as Record<string, unknown> | undefined;
|
|
66
|
-
|
|
67
|
-
throw new NAuthClientError(code, message, {
|
|
68
|
-
statusCode: error.status,
|
|
69
|
-
timestamp,
|
|
70
|
-
details,
|
|
71
|
-
isNetworkError: error.status === 0, // Network error (no response from server)
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Re-throw non-HTTP errors
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
package/standalone/package.json
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { inject, PLATFORM_ID } from '@angular/core';
|
|
2
|
-
import { isPlatformBrowser } from '@angular/common';
|
|
3
|
-
import { type CanActivateFn } from '@angular/router';
|
|
4
|
-
import { AuthService } from './auth.service';
|
|
5
|
-
import { NAUTH_CLIENT_CONFIG } from './tokens';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Social redirect callback route guard.
|
|
9
|
-
*
|
|
10
|
-
* This guard supports the redirect-first social flow where the backend redirects
|
|
11
|
-
* back to the frontend with:
|
|
12
|
-
* - `appState` (always optional)
|
|
13
|
-
* - `exchangeToken` (present for json/hybrid flows, and for cookie flows that return a challenge)
|
|
14
|
-
* - `error` / `error_description` (provider errors)
|
|
15
|
-
*
|
|
16
|
-
* Behavior:
|
|
17
|
-
* - If `exchangeToken` exists: exchanges it via backend and redirects to success or challenge routes.
|
|
18
|
-
* - If no `exchangeToken`: treat as cookie-success path and redirect to success route.
|
|
19
|
-
* - If `error` exists: redirects to oauthError route.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```typescript
|
|
23
|
-
* import { socialRedirectCallbackGuard } from '@nauth-toolkit/client/angular';
|
|
24
|
-
*
|
|
25
|
-
* export const routes: Routes = [
|
|
26
|
-
* { path: 'auth/callback', canActivate: [socialRedirectCallbackGuard], component: CallbackComponent },
|
|
27
|
-
* ];
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
export const socialRedirectCallbackGuard: CanActivateFn = async (): Promise<boolean> => {
|
|
31
|
-
const auth = inject(AuthService);
|
|
32
|
-
const config = inject(NAUTH_CLIENT_CONFIG);
|
|
33
|
-
const platformId = inject(PLATFORM_ID);
|
|
34
|
-
const isBrowser = isPlatformBrowser(platformId);
|
|
35
|
-
|
|
36
|
-
if (!isBrowser) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const params = new URLSearchParams(window.location.search);
|
|
41
|
-
const error = params.get('error');
|
|
42
|
-
const exchangeToken = params.get('exchangeToken');
|
|
43
|
-
|
|
44
|
-
// Provider error: redirect to oauthError
|
|
45
|
-
if (error) {
|
|
46
|
-
const errorUrl = config.redirects?.oauthError || '/login';
|
|
47
|
-
window.location.replace(errorUrl);
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// No exchangeToken: cookie success path; redirect to success.
|
|
52
|
-
//
|
|
53
|
-
// Note: we do not "activate" the callback route to avoid consumers needing to render a page.
|
|
54
|
-
if (!exchangeToken) {
|
|
55
|
-
// ============================================================================
|
|
56
|
-
// Cookies mode: hydrate user state before redirecting
|
|
57
|
-
// ============================================================================
|
|
58
|
-
// WHY: In cookie delivery, the OAuth callback completes via browser redirects, so the frontend
|
|
59
|
-
// does not receive a JSON AuthResponse to populate the SDK's cached `currentUser`.
|
|
60
|
-
//
|
|
61
|
-
// Without this, sync guards (`authGuard`) can immediately redirect to /login because
|
|
62
|
-
// `currentUser` is still null even though cookies were set successfully.
|
|
63
|
-
try {
|
|
64
|
-
await auth.getProfile();
|
|
65
|
-
} catch {
|
|
66
|
-
const errorUrl = config.redirects?.oauthError || '/login';
|
|
67
|
-
window.location.replace(errorUrl);
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
const successUrl = config.redirects?.success || '/';
|
|
71
|
-
window.location.replace(successUrl);
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Exchange token and route accordingly
|
|
76
|
-
const response = await auth.exchangeSocialRedirect(exchangeToken);
|
|
77
|
-
if (response.challengeName) {
|
|
78
|
-
const challengeBase = config.redirects?.challengeBase || '/auth/challenge';
|
|
79
|
-
const challengeRoute = response.challengeName.toLowerCase().replace(/_/g, '-');
|
|
80
|
-
window.location.replace(`${challengeBase}/${challengeRoute}`);
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const successUrl = config.redirects?.success || '/';
|
|
85
|
-
window.location.replace(successUrl);
|
|
86
|
-
return false;
|
|
87
|
-
};
|
package/tsconfig.json
DELETED
package/tsconfig.lib.json
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "./out-tsc/lib",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"declarationMap": true,
|
|
7
|
-
"inlineSources": true,
|
|
8
|
-
"types": [],
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"module": "ES2022",
|
|
11
|
-
"moduleResolution": "bundler",
|
|
12
|
-
"lib": ["ES2022", "DOM"],
|
|
13
|
-
"paths": {
|
|
14
|
-
"@angular/common/http": ["./node_modules/@angular/common/types/http.d.ts"],
|
|
15
|
-
"@angular/common": ["./node_modules/@angular/common/types/common.d.ts"]
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
"angularCompilerOptions": {
|
|
19
|
-
"skipTemplateCodegen": true,
|
|
20
|
-
"strictMetadataEmit": true,
|
|
21
|
-
"enableResourceInlining": true
|
|
22
|
-
},
|
|
23
|
-
"exclude": [
|
|
24
|
-
"src/test.ts",
|
|
25
|
-
"**/*.spec.ts"
|
|
26
|
-
]
|
|
27
|
-
}
|
|
28
|
-
|