@hahnpro/ai-sdk 0.11.0 → 0.11.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 CHANGED
@@ -53,3 +53,101 @@ bootstrapApplication(AppComponent, {
53
53
  ],
54
54
  }).catch((err) => console.error(err));
55
55
  ```
56
+
57
+ ## User Authentication
58
+
59
+ ### Overview
60
+
61
+ The `AuthAdapter` is an interface that provides a unified API for different authentication libraries. It abstracts the specific authentication implementation and allows using various auth providers.
62
+
63
+ ### Interface Definition
64
+
65
+ ``` typescript
66
+ export interface AuthAdapter {
67
+ init(): Promise<void>;
68
+ isAuthenticated(): Promise<boolean>;
69
+ getAccessToken(): Promise<string>;
70
+ loginRedirect(options?: { redirectUri?: string }): Promise<void>;
71
+ loginPopup?(): Promise<void>;
72
+ logout?(): Promise<void>;
73
+ }
74
+ ```
75
+
76
+ ### Required Fields
77
+
78
+ `init()`: Initializes the auth provider (e.g., checks existing sessions)
79
+
80
+ `isAuthenticated()`: Checks if the user is authenticated
81
+
82
+ `getAccessToken()`: Returns the current access token
83
+
84
+ `loginRedirect()`: Starts the login flow with redirect
85
+
86
+ ### Optional Fields
87
+ `loginPopup()`: Starts the login flow in a popup window
88
+
89
+ `logout()`: Logs out the user
90
+
91
+ ### Implementation with OIDC
92
+ Here's an example implementation using angular-auth-oidc-client:
93
+ ``` typescript
94
+
95
+ import { AUTH_ADAPTER, AuthAdapter } from '@hahnpro/ai-sdk';
96
+ import { OidcSecurityService } from 'angular-auth-oidc-client';
97
+ import { lastValueFrom } from 'rxjs';
98
+
99
+ export const AUTH_PROVIDERS = [
100
+ // OIDC Provider configuration...
101
+ {
102
+ provide: AUTH_ADAPTER,
103
+ deps: [OidcSecurityService],
104
+ useFactory: (oidc: OidcSecurityService): AuthAdapter => ({
105
+ init: async () => {
106
+ // Automatically checks existing authentication
107
+ await lastValueFrom(oidc.checkAuth());
108
+ },
109
+ isAuthenticated: () => {
110
+ return lastValueFrom(oidc.isAuthenticated());
111
+ },
112
+ getAccessToken(): Promise<string> {
113
+ return lastValueFrom(oidc.getAccessToken());
114
+ },
115
+ loginRedirect: async (options) => {
116
+ // Optional: use redirect URL
117
+ oidc.authorize(options?.redirectUri);
118
+ },
119
+ loginPopup: async () => {
120
+ await lastValueFrom(oidc.authorizeWithPopUp());
121
+ },
122
+ logout: async () => {
123
+ await lastValueFrom(oidc.logoff());
124
+ },
125
+ }),
126
+ },
127
+ ];
128
+ ```
129
+
130
+ ### Injection in Angular
131
+ 1. Register Provider
132
+ ``` typescript
133
+ import { provideAiSdk } from '@hahnpro/ai-sdk';
134
+
135
+ bootstrapApplication(AppComponent, {
136
+ providers: [
137
+ provideAiSdk(),
138
+ ...AUTH_PROVIDERS, // AuthAdapter is provided here
139
+ ],
140
+ });
141
+ ```
142
+ 2. Initalize Auth Adapter
143
+ At some point before using the Auth Adapter, you need to initialize it. This can be done by calling the `init()` method of the Auth Adapter:
144
+ ``` typescript
145
+ export class AppComponent implements OnInit {
146
+ private readonly auth = inject(AUTH_ADAPTER);
147
+
148
+ ngOnInit() {
149
+ this.auth.init();
150
+ }
151
+ ...
152
+ }
153
+ ```