@leancodepl/login-manager 8.4.0 → 8.5.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 +214 -0
- package/index.cjs.js +123 -7
- package/index.esm.js +123 -7
- package/package.json +35 -3
- package/src/lib/asyncLoginManager.d.ts +24 -0
- package/src/lib/cannotRefreshToken.d.ts +16 -0
- package/src/lib/facebookClient.d.ts +17 -0
- package/src/lib/localTokenStorage.d.ts +15 -0
- package/src/lib/memoryTokenStorage.d.ts +12 -0
- package/src/lib/sessionTokenStorage.d.ts +15 -0
- package/src/lib/syncLoginManager.d.ts +24 -0
- package/index.esm.d.ts +0 -1
- /package/{index.cjs.d.ts → index.d.ts} +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# @leancodepl/login-manager
|
|
2
|
+
|
|
3
|
+
OAuth2 authentication management with token storage and refresh capabilities.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **OAuth2 authentication** - Standard-compliant implementation
|
|
8
|
+
- **Multiple storage options** - Local, session, or memory storage
|
|
9
|
+
- **Token refresh** - Automatic refresh handling
|
|
10
|
+
- **Social login** - Facebook, Google and LinkedIn integrations
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @leancodepl/login-manager
|
|
16
|
+
# or
|
|
17
|
+
yarn add @leancodepl/login-manager
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## API
|
|
21
|
+
|
|
22
|
+
### `AsyncLoginManager`
|
|
23
|
+
|
|
24
|
+
Manages OAuth2 authentication with asynchronous token storage.
|
|
25
|
+
|
|
26
|
+
**Parameters:**
|
|
27
|
+
|
|
28
|
+
- `storage: AsyncTokenStorage` - Token storage implementation
|
|
29
|
+
- `endpoint: string` - OAuth2 server endpoint
|
|
30
|
+
- `clientSecret: string | undefined` - Client secret for authentication
|
|
31
|
+
- `clientId: string` - OAuth2 client identifier
|
|
32
|
+
- `scopes: string` - Space-separated OAuth2 scopes
|
|
33
|
+
- `additionalParams?: Record<string, string>` - Additional OAuth2 parameters
|
|
34
|
+
|
|
35
|
+
### `SyncLoginManager`
|
|
36
|
+
|
|
37
|
+
Manages OAuth2 authentication with synchronous token storage.
|
|
38
|
+
|
|
39
|
+
**Parameters:**
|
|
40
|
+
|
|
41
|
+
- `storage: SyncTokenStorage` - Token storage implementation
|
|
42
|
+
- `endpoint: string` - OAuth2 server endpoint
|
|
43
|
+
- `clientSecret: string | undefined` - Client secret for authentication
|
|
44
|
+
- `clientId: string` - OAuth2 client identifier
|
|
45
|
+
- `scopes: string` - Space-separated OAuth2 scopes
|
|
46
|
+
- `additionalParams?: Record<string, string>` - Additional OAuth2 parameters
|
|
47
|
+
|
|
48
|
+
### `LocalTokenStorage`
|
|
49
|
+
|
|
50
|
+
Stores OAuth2 tokens in browser localStorage.
|
|
51
|
+
|
|
52
|
+
**Parameters:**
|
|
53
|
+
|
|
54
|
+
- `tokenKey?: string` - localStorage key for access token (default: "token")
|
|
55
|
+
- `refreshKey?: string` - localStorage key for refresh token (default: "refresh_token")
|
|
56
|
+
- `expiryKey?: string` - localStorage key for expiry date (default: "expiration_date")
|
|
57
|
+
|
|
58
|
+
### `SessionTokenStorage`
|
|
59
|
+
|
|
60
|
+
Stores OAuth2 tokens in browser sessionStorage.
|
|
61
|
+
|
|
62
|
+
**Parameters:**
|
|
63
|
+
|
|
64
|
+
- `tokenKey?: string` - sessionStorage key for access token (default: "token")
|
|
65
|
+
- `refreshKey?: string` - sessionStorage key for refresh token (default: "refresh_token")
|
|
66
|
+
- `expiryKey?: string` - sessionStorage key for expiry date (default: "expiration_date")
|
|
67
|
+
|
|
68
|
+
### `MemoryTokenStorage`
|
|
69
|
+
|
|
70
|
+
Stores OAuth2 tokens in memory.
|
|
71
|
+
|
|
72
|
+
### `FacebookClient`
|
|
73
|
+
|
|
74
|
+
Integrates Facebook Login SDK for web applications.
|
|
75
|
+
|
|
76
|
+
**Parameters:**
|
|
77
|
+
|
|
78
|
+
- `facebookAppId: string` - Facebook App ID
|
|
79
|
+
- `facebookPermissions: string` - Comma-separated Facebook permissions
|
|
80
|
+
|
|
81
|
+
### `CannotRefreshToken`
|
|
82
|
+
|
|
83
|
+
Error thrown when token refresh fails.
|
|
84
|
+
|
|
85
|
+
## Usage Examples
|
|
86
|
+
|
|
87
|
+
### Basic Authentication
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { SyncLoginManager, LocalTokenStorage } from "@leancodepl/login-manager"
|
|
91
|
+
|
|
92
|
+
const tokenStorage = new LocalTokenStorage()
|
|
93
|
+
const loginManager = new SyncLoginManager(
|
|
94
|
+
tokenStorage,
|
|
95
|
+
"https://api.example.com",
|
|
96
|
+
"client_secret",
|
|
97
|
+
"client_id",
|
|
98
|
+
"openid profile email",
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
const result = await loginManager.trySignIn("user@example.com", "password")
|
|
102
|
+
if (result.type === "success") {
|
|
103
|
+
console.log("Signed in successfully")
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Session-based Authentication
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { AsyncLoginManager, SessionTokenStorage } from "@leancodepl/login-manager"
|
|
111
|
+
|
|
112
|
+
const tokenStorage = new SessionTokenStorage()
|
|
113
|
+
const loginManager = new AsyncLoginManager(
|
|
114
|
+
tokenStorage,
|
|
115
|
+
"https://api.example.com",
|
|
116
|
+
undefined, // No client secret for public clients
|
|
117
|
+
"public_client_id",
|
|
118
|
+
"openid profile",
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
await loginManager.load()
|
|
122
|
+
const isSignedIn = await loginManager.isSigned()
|
|
123
|
+
console.log("User signed in:", isSignedIn)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Facebook Login Integration
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { FacebookClient, SyncLoginManager, LocalTokenStorage } from "@leancodepl/login-manager"
|
|
130
|
+
|
|
131
|
+
const facebookClient = new FacebookClient("your-facebook-app-id", "email,public_profile")
|
|
132
|
+
const loginManager = new SyncLoginManager(
|
|
133
|
+
new LocalTokenStorage(),
|
|
134
|
+
"https://api.example.com",
|
|
135
|
+
"client_secret",
|
|
136
|
+
"client_id",
|
|
137
|
+
"openid profile",
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
facebookClient.setup()
|
|
141
|
+
facebookClient.login(async accessToken => {
|
|
142
|
+
const result = await loginManager.trySignInWithFacebook(accessToken)
|
|
143
|
+
if (result.type === "success") {
|
|
144
|
+
console.log("Facebook login successful")
|
|
145
|
+
}
|
|
146
|
+
})
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Token Management
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { CannotRefreshToken, SyncLoginManager, LocalTokenStorage } from "@leancodepl/login-manager"
|
|
153
|
+
|
|
154
|
+
const tokenStorage = new LocalTokenStorage()
|
|
155
|
+
const loginManager = new SyncLoginManager(
|
|
156
|
+
tokenStorage,
|
|
157
|
+
"https://api.example.com",
|
|
158
|
+
"client_secret",
|
|
159
|
+
"client_id",
|
|
160
|
+
"openid profile",
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
const token = await loginManager.getToken()
|
|
165
|
+
console.log("Access token:", token)
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (error instanceof CannotRefreshToken) {
|
|
168
|
+
console.log("Token expired, user needs to sign in again")
|
|
169
|
+
await loginManager.signOut()
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Authentication State Monitoring
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import { SyncLoginManager, LocalTokenStorage } from "@leancodepl/login-manager"
|
|
178
|
+
|
|
179
|
+
const tokenStorage = new LocalTokenStorage()
|
|
180
|
+
const loginManager = new SyncLoginManager(
|
|
181
|
+
tokenStorage,
|
|
182
|
+
"https://api.example.com",
|
|
183
|
+
"client_secret",
|
|
184
|
+
"client_id",
|
|
185
|
+
"openid profile",
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
loginManager.onChange(isSignedIn => {
|
|
189
|
+
if (isSignedIn) {
|
|
190
|
+
console.log("User is now signed in")
|
|
191
|
+
} else {
|
|
192
|
+
console.log("User signed out")
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Multiple Authentication Providers
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
import { SyncLoginManager, LocalTokenStorage } from "@leancodepl/login-manager"
|
|
201
|
+
|
|
202
|
+
const tokenStorage = new LocalTokenStorage()
|
|
203
|
+
const loginManager = new SyncLoginManager(
|
|
204
|
+
tokenStorage,
|
|
205
|
+
"https://api.example.com",
|
|
206
|
+
"client_secret",
|
|
207
|
+
"client_id",
|
|
208
|
+
"openid profile",
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
const googleResult = await loginManager.trySignInWithGoogle("google_access_token")
|
|
212
|
+
const linkedinResult = await loginManager.trySignInWithLinkedIn("linkedin_access_token")
|
|
213
|
+
const otpResult = await loginManager.trySignInWithOneTimeToken("one_time_token")
|
|
214
|
+
```
|
package/index.cjs.js
CHANGED
|
@@ -194,14 +194,52 @@ class BaseLoginManager {
|
|
|
194
194
|
}
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
-
|
|
197
|
+
/**
|
|
198
|
+
* Error thrown when token refresh fails.
|
|
199
|
+
*
|
|
200
|
+
* Indicates that the refresh token is invalid or expired, requiring user to sign in again.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* try {
|
|
205
|
+
* const token = await loginManager.getToken();
|
|
206
|
+
* } catch (error) {
|
|
207
|
+
* if (error instanceof CannotRefreshToken) {
|
|
208
|
+
* console.log('User needs to sign in again');
|
|
209
|
+
* }
|
|
210
|
+
* }
|
|
211
|
+
* ```
|
|
212
|
+
*/ class CannotRefreshToken extends Error {
|
|
198
213
|
constructor(m){
|
|
199
214
|
super(m);
|
|
200
215
|
Object.setPrototypeOf(this, CannotRefreshToken.prototype);
|
|
201
216
|
}
|
|
202
217
|
}
|
|
203
218
|
|
|
204
|
-
|
|
219
|
+
/**
|
|
220
|
+
* Manages OAuth2 authentication with asynchronous token storage.
|
|
221
|
+
*
|
|
222
|
+
* Extends BaseLoginManager to work with async storage implementations like IndexedDB or remote storage.
|
|
223
|
+
* Handles token refresh, expiration, and authentication state management.
|
|
224
|
+
*
|
|
225
|
+
* @param storage - Token storage implementation
|
|
226
|
+
* @param endpoint - OAuth2 server endpoint
|
|
227
|
+
* @param clientSecret - Client secret for authentication
|
|
228
|
+
* @param clientId - OAuth2 client identifier
|
|
229
|
+
* @param scopes - Space-separated OAuth2 scopes
|
|
230
|
+
* @param additionalParams - Additional OAuth2 parameters
|
|
231
|
+
* @example
|
|
232
|
+
* ```typescript
|
|
233
|
+
* const tokenStorage = new CustomAsyncStorage();
|
|
234
|
+
* const loginManager = new AsyncLoginManager(
|
|
235
|
+
* tokenStorage,
|
|
236
|
+
* 'https://api.example.com',
|
|
237
|
+
* 'client_secret',
|
|
238
|
+
* 'client_id',
|
|
239
|
+
* 'openid profile'
|
|
240
|
+
* );
|
|
241
|
+
* ```
|
|
242
|
+
*/ class AsyncLoginManager extends BaseLoginManager {
|
|
205
243
|
async signOut() {
|
|
206
244
|
await this.storage.resetToken();
|
|
207
245
|
this.notify(false);
|
|
@@ -232,7 +270,23 @@ class AsyncLoginManager extends BaseLoginManager {
|
|
|
232
270
|
}
|
|
233
271
|
|
|
234
272
|
/// <reference types="facebook-js-sdk" />
|
|
235
|
-
|
|
273
|
+
/**
|
|
274
|
+
* Integrates Facebook Login SDK for web applications.
|
|
275
|
+
*
|
|
276
|
+
* Handles Facebook authentication flow and provides access tokens for OAuth2 sign-in.
|
|
277
|
+
* Automatically loads Facebook SDK and manages login state.
|
|
278
|
+
*
|
|
279
|
+
* @param facebookAppId - Facebook App ID
|
|
280
|
+
* @param facebookPermissions - Comma-separated Facebook permissions
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const facebookClient = new FacebookClient('your-app-id', 'email,public_profile');
|
|
284
|
+
* facebookClient.setup();
|
|
285
|
+
* facebookClient.login((accessToken) => {
|
|
286
|
+
* return loginManager.trySignInWithFacebook(accessToken);
|
|
287
|
+
* });
|
|
288
|
+
* ```
|
|
289
|
+
*/ class FacebookClient {
|
|
236
290
|
get accessToken() {
|
|
237
291
|
return this.token;
|
|
238
292
|
}
|
|
@@ -299,7 +353,21 @@ class FacebookClient {
|
|
|
299
353
|
}
|
|
300
354
|
}
|
|
301
355
|
|
|
302
|
-
|
|
356
|
+
/**
|
|
357
|
+
* Stores OAuth2 tokens in browser localStorage.
|
|
358
|
+
*
|
|
359
|
+
* Provides persistent token storage that survives browser sessions.
|
|
360
|
+
* Implements SyncTokenStorage interface for synchronous operations.
|
|
361
|
+
*
|
|
362
|
+
* @param tokenKey - localStorage key for access token (default: "token")
|
|
363
|
+
* @param refreshKey - localStorage key for refresh token (default: "refresh_token")
|
|
364
|
+
* @param expiryKey - localStorage key for expiry date (default: "expiration_date")
|
|
365
|
+
* @example
|
|
366
|
+
* ```typescript
|
|
367
|
+
* const storage = new LocalTokenStorage();
|
|
368
|
+
* const loginManager = new SyncLoginManager(storage, endpoint, secret, clientId, scopes);
|
|
369
|
+
* ```
|
|
370
|
+
*/ class LocalTokenStorage {
|
|
303
371
|
getToken() {
|
|
304
372
|
if (this.hasValue(this.tokenKey)) {
|
|
305
373
|
return {
|
|
@@ -340,7 +408,18 @@ class LocalTokenStorage {
|
|
|
340
408
|
}
|
|
341
409
|
}
|
|
342
410
|
|
|
343
|
-
|
|
411
|
+
/**
|
|
412
|
+
* Stores OAuth2 tokens in memory.
|
|
413
|
+
*
|
|
414
|
+
* Provides temporary token storage that clears when the page is refreshed.
|
|
415
|
+
* Implements SyncTokenStorage interface for synchronous operations.
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* ```typescript
|
|
419
|
+
* const storage = new MemoryTokenStorage();
|
|
420
|
+
* const loginManager = new SyncLoginManager(storage, endpoint, secret, clientId, scopes);
|
|
421
|
+
* ```
|
|
422
|
+
*/ class MemoryTokenStorage {
|
|
344
423
|
getToken() {
|
|
345
424
|
return this.token;
|
|
346
425
|
}
|
|
@@ -361,7 +440,21 @@ class MemoryTokenStorage {
|
|
|
361
440
|
}
|
|
362
441
|
}
|
|
363
442
|
|
|
364
|
-
|
|
443
|
+
/**
|
|
444
|
+
* Stores OAuth2 tokens in browser sessionStorage.
|
|
445
|
+
*
|
|
446
|
+
* Provides session-based token storage that clears when the browser tab is closed.
|
|
447
|
+
* Implements SyncTokenStorage interface for synchronous operations.
|
|
448
|
+
*
|
|
449
|
+
* @param tokenKey - sessionStorage key for access token (default: "token")
|
|
450
|
+
* @param refreshKey - sessionStorage key for refresh token (default: "refresh_token")
|
|
451
|
+
* @param expiryKey - sessionStorage key for expiry date (default: "expiration_date")
|
|
452
|
+
* @example
|
|
453
|
+
* ```typescript
|
|
454
|
+
* const storage = new SessionTokenStorage();
|
|
455
|
+
* const loginManager = new SyncLoginManager(storage, endpoint, secret, clientId, scopes);
|
|
456
|
+
* ```
|
|
457
|
+
*/ class SessionTokenStorage {
|
|
365
458
|
getToken() {
|
|
366
459
|
if (this.hasValue(this.tokenKey)) {
|
|
367
460
|
return {
|
|
@@ -402,7 +495,30 @@ class SessionTokenStorage {
|
|
|
402
495
|
}
|
|
403
496
|
}
|
|
404
497
|
|
|
405
|
-
|
|
498
|
+
/**
|
|
499
|
+
* Manages OAuth2 authentication with synchronous token storage.
|
|
500
|
+
*
|
|
501
|
+
* Extends BaseLoginManager to work with sync storage implementations like localStorage or sessionStorage.
|
|
502
|
+
* Handles token refresh, expiration, and authentication state management.
|
|
503
|
+
*
|
|
504
|
+
* @param storage - Token storage implementation
|
|
505
|
+
* @param endpoint - OAuth2 server endpoint
|
|
506
|
+
* @param clientSecret - Client secret for authentication
|
|
507
|
+
* @param clientId - OAuth2 client identifier
|
|
508
|
+
* @param scopes - Space-separated OAuth2 scopes
|
|
509
|
+
* @param additionalParams - Additional OAuth2 parameters
|
|
510
|
+
* @example
|
|
511
|
+
* ```typescript
|
|
512
|
+
* const tokenStorage = new LocalTokenStorage();
|
|
513
|
+
* const loginManager = new SyncLoginManager(
|
|
514
|
+
* tokenStorage,
|
|
515
|
+
* 'https://api.example.com',
|
|
516
|
+
* 'client_secret',
|
|
517
|
+
* 'client_id',
|
|
518
|
+
* 'openid profile'
|
|
519
|
+
* );
|
|
520
|
+
* ```
|
|
521
|
+
*/ class SyncLoginManager extends BaseLoginManager {
|
|
406
522
|
signOut() {
|
|
407
523
|
this.storage.resetToken();
|
|
408
524
|
this.notify(false);
|
package/index.esm.js
CHANGED
|
@@ -192,14 +192,52 @@ class BaseLoginManager {
|
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
-
|
|
195
|
+
/**
|
|
196
|
+
* Error thrown when token refresh fails.
|
|
197
|
+
*
|
|
198
|
+
* Indicates that the refresh token is invalid or expired, requiring user to sign in again.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* try {
|
|
203
|
+
* const token = await loginManager.getToken();
|
|
204
|
+
* } catch (error) {
|
|
205
|
+
* if (error instanceof CannotRefreshToken) {
|
|
206
|
+
* console.log('User needs to sign in again');
|
|
207
|
+
* }
|
|
208
|
+
* }
|
|
209
|
+
* ```
|
|
210
|
+
*/ class CannotRefreshToken extends Error {
|
|
196
211
|
constructor(m){
|
|
197
212
|
super(m);
|
|
198
213
|
Object.setPrototypeOf(this, CannotRefreshToken.prototype);
|
|
199
214
|
}
|
|
200
215
|
}
|
|
201
216
|
|
|
202
|
-
|
|
217
|
+
/**
|
|
218
|
+
* Manages OAuth2 authentication with asynchronous token storage.
|
|
219
|
+
*
|
|
220
|
+
* Extends BaseLoginManager to work with async storage implementations like IndexedDB or remote storage.
|
|
221
|
+
* Handles token refresh, expiration, and authentication state management.
|
|
222
|
+
*
|
|
223
|
+
* @param storage - Token storage implementation
|
|
224
|
+
* @param endpoint - OAuth2 server endpoint
|
|
225
|
+
* @param clientSecret - Client secret for authentication
|
|
226
|
+
* @param clientId - OAuth2 client identifier
|
|
227
|
+
* @param scopes - Space-separated OAuth2 scopes
|
|
228
|
+
* @param additionalParams - Additional OAuth2 parameters
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* const tokenStorage = new CustomAsyncStorage();
|
|
232
|
+
* const loginManager = new AsyncLoginManager(
|
|
233
|
+
* tokenStorage,
|
|
234
|
+
* 'https://api.example.com',
|
|
235
|
+
* 'client_secret',
|
|
236
|
+
* 'client_id',
|
|
237
|
+
* 'openid profile'
|
|
238
|
+
* );
|
|
239
|
+
* ```
|
|
240
|
+
*/ class AsyncLoginManager extends BaseLoginManager {
|
|
203
241
|
async signOut() {
|
|
204
242
|
await this.storage.resetToken();
|
|
205
243
|
this.notify(false);
|
|
@@ -230,7 +268,23 @@ class AsyncLoginManager extends BaseLoginManager {
|
|
|
230
268
|
}
|
|
231
269
|
|
|
232
270
|
/// <reference types="facebook-js-sdk" />
|
|
233
|
-
|
|
271
|
+
/**
|
|
272
|
+
* Integrates Facebook Login SDK for web applications.
|
|
273
|
+
*
|
|
274
|
+
* Handles Facebook authentication flow and provides access tokens for OAuth2 sign-in.
|
|
275
|
+
* Automatically loads Facebook SDK and manages login state.
|
|
276
|
+
*
|
|
277
|
+
* @param facebookAppId - Facebook App ID
|
|
278
|
+
* @param facebookPermissions - Comma-separated Facebook permissions
|
|
279
|
+
* @example
|
|
280
|
+
* ```typescript
|
|
281
|
+
* const facebookClient = new FacebookClient('your-app-id', 'email,public_profile');
|
|
282
|
+
* facebookClient.setup();
|
|
283
|
+
* facebookClient.login((accessToken) => {
|
|
284
|
+
* return loginManager.trySignInWithFacebook(accessToken);
|
|
285
|
+
* });
|
|
286
|
+
* ```
|
|
287
|
+
*/ class FacebookClient {
|
|
234
288
|
get accessToken() {
|
|
235
289
|
return this.token;
|
|
236
290
|
}
|
|
@@ -297,7 +351,21 @@ class FacebookClient {
|
|
|
297
351
|
}
|
|
298
352
|
}
|
|
299
353
|
|
|
300
|
-
|
|
354
|
+
/**
|
|
355
|
+
* Stores OAuth2 tokens in browser localStorage.
|
|
356
|
+
*
|
|
357
|
+
* Provides persistent token storage that survives browser sessions.
|
|
358
|
+
* Implements SyncTokenStorage interface for synchronous operations.
|
|
359
|
+
*
|
|
360
|
+
* @param tokenKey - localStorage key for access token (default: "token")
|
|
361
|
+
* @param refreshKey - localStorage key for refresh token (default: "refresh_token")
|
|
362
|
+
* @param expiryKey - localStorage key for expiry date (default: "expiration_date")
|
|
363
|
+
* @example
|
|
364
|
+
* ```typescript
|
|
365
|
+
* const storage = new LocalTokenStorage();
|
|
366
|
+
* const loginManager = new SyncLoginManager(storage, endpoint, secret, clientId, scopes);
|
|
367
|
+
* ```
|
|
368
|
+
*/ class LocalTokenStorage {
|
|
301
369
|
getToken() {
|
|
302
370
|
if (this.hasValue(this.tokenKey)) {
|
|
303
371
|
return {
|
|
@@ -338,7 +406,18 @@ class LocalTokenStorage {
|
|
|
338
406
|
}
|
|
339
407
|
}
|
|
340
408
|
|
|
341
|
-
|
|
409
|
+
/**
|
|
410
|
+
* Stores OAuth2 tokens in memory.
|
|
411
|
+
*
|
|
412
|
+
* Provides temporary token storage that clears when the page is refreshed.
|
|
413
|
+
* Implements SyncTokenStorage interface for synchronous operations.
|
|
414
|
+
*
|
|
415
|
+
* @example
|
|
416
|
+
* ```typescript
|
|
417
|
+
* const storage = new MemoryTokenStorage();
|
|
418
|
+
* const loginManager = new SyncLoginManager(storage, endpoint, secret, clientId, scopes);
|
|
419
|
+
* ```
|
|
420
|
+
*/ class MemoryTokenStorage {
|
|
342
421
|
getToken() {
|
|
343
422
|
return this.token;
|
|
344
423
|
}
|
|
@@ -359,7 +438,21 @@ class MemoryTokenStorage {
|
|
|
359
438
|
}
|
|
360
439
|
}
|
|
361
440
|
|
|
362
|
-
|
|
441
|
+
/**
|
|
442
|
+
* Stores OAuth2 tokens in browser sessionStorage.
|
|
443
|
+
*
|
|
444
|
+
* Provides session-based token storage that clears when the browser tab is closed.
|
|
445
|
+
* Implements SyncTokenStorage interface for synchronous operations.
|
|
446
|
+
*
|
|
447
|
+
* @param tokenKey - sessionStorage key for access token (default: "token")
|
|
448
|
+
* @param refreshKey - sessionStorage key for refresh token (default: "refresh_token")
|
|
449
|
+
* @param expiryKey - sessionStorage key for expiry date (default: "expiration_date")
|
|
450
|
+
* @example
|
|
451
|
+
* ```typescript
|
|
452
|
+
* const storage = new SessionTokenStorage();
|
|
453
|
+
* const loginManager = new SyncLoginManager(storage, endpoint, secret, clientId, scopes);
|
|
454
|
+
* ```
|
|
455
|
+
*/ class SessionTokenStorage {
|
|
363
456
|
getToken() {
|
|
364
457
|
if (this.hasValue(this.tokenKey)) {
|
|
365
458
|
return {
|
|
@@ -400,7 +493,30 @@ class SessionTokenStorage {
|
|
|
400
493
|
}
|
|
401
494
|
}
|
|
402
495
|
|
|
403
|
-
|
|
496
|
+
/**
|
|
497
|
+
* Manages OAuth2 authentication with synchronous token storage.
|
|
498
|
+
*
|
|
499
|
+
* Extends BaseLoginManager to work with sync storage implementations like localStorage or sessionStorage.
|
|
500
|
+
* Handles token refresh, expiration, and authentication state management.
|
|
501
|
+
*
|
|
502
|
+
* @param storage - Token storage implementation
|
|
503
|
+
* @param endpoint - OAuth2 server endpoint
|
|
504
|
+
* @param clientSecret - Client secret for authentication
|
|
505
|
+
* @param clientId - OAuth2 client identifier
|
|
506
|
+
* @param scopes - Space-separated OAuth2 scopes
|
|
507
|
+
* @param additionalParams - Additional OAuth2 parameters
|
|
508
|
+
* @example
|
|
509
|
+
* ```typescript
|
|
510
|
+
* const tokenStorage = new LocalTokenStorage();
|
|
511
|
+
* const loginManager = new SyncLoginManager(
|
|
512
|
+
* tokenStorage,
|
|
513
|
+
* 'https://api.example.com',
|
|
514
|
+
* 'client_secret',
|
|
515
|
+
* 'client_id',
|
|
516
|
+
* 'openid profile'
|
|
517
|
+
* );
|
|
518
|
+
* ```
|
|
519
|
+
*/ class SyncLoginManager extends BaseLoginManager {
|
|
404
520
|
signOut() {
|
|
405
521
|
this.storage.resetToken();
|
|
406
522
|
this.notify(false);
|
package/package.json
CHANGED
|
@@ -1,20 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leancodepl/login-manager",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.5.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"buffer": ">=6.0.0"
|
|
7
7
|
},
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public",
|
|
10
|
+
"registry": "https://registry.npmjs.org/"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=18.0.0"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/leancodepl/js_corelibrary.git",
|
|
18
|
+
"directory": "packages/login-manager"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/leancodepl/js_corelibrary",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/leancodepl/js_corelibrary/issues"
|
|
23
|
+
},
|
|
24
|
+
"description": "OAuth2 and token-based authentication management utilities",
|
|
25
|
+
"keywords": [
|
|
26
|
+
"oauth2",
|
|
27
|
+
"authentication",
|
|
28
|
+
"tokens",
|
|
29
|
+
"login",
|
|
30
|
+
"auth",
|
|
31
|
+
"typescript",
|
|
32
|
+
"javascript",
|
|
33
|
+
"leancode"
|
|
34
|
+
],
|
|
35
|
+
"author": {
|
|
36
|
+
"name": "LeanCode",
|
|
37
|
+
"url": "https://leancode.co"
|
|
38
|
+
},
|
|
39
|
+
"sideEffects": false,
|
|
8
40
|
"exports": {
|
|
9
41
|
"./package.json": "./package.json",
|
|
10
42
|
".": {
|
|
11
43
|
"module": "./index.esm.js",
|
|
12
|
-
"types": "./index.
|
|
44
|
+
"types": "./index.d.ts",
|
|
13
45
|
"import": "./index.cjs.mjs",
|
|
14
46
|
"default": "./index.cjs.js"
|
|
15
47
|
}
|
|
16
48
|
},
|
|
17
49
|
"module": "./index.esm.js",
|
|
18
50
|
"main": "./index.cjs.js",
|
|
19
|
-
"types": "./index.
|
|
51
|
+
"types": "./index.d.ts"
|
|
20
52
|
}
|
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
import { BaseLoginManager, LoginManager } from "./baseLoginManager";
|
|
2
2
|
import { AsyncTokenStorage } from "./tokenStorage";
|
|
3
|
+
/**
|
|
4
|
+
* Manages OAuth2 authentication with asynchronous token storage.
|
|
5
|
+
*
|
|
6
|
+
* Extends BaseLoginManager to work with async storage implementations like IndexedDB or remote storage.
|
|
7
|
+
* Handles token refresh, expiration, and authentication state management.
|
|
8
|
+
*
|
|
9
|
+
* @param storage - Token storage implementation
|
|
10
|
+
* @param endpoint - OAuth2 server endpoint
|
|
11
|
+
* @param clientSecret - Client secret for authentication
|
|
12
|
+
* @param clientId - OAuth2 client identifier
|
|
13
|
+
* @param scopes - Space-separated OAuth2 scopes
|
|
14
|
+
* @param additionalParams - Additional OAuth2 parameters
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const tokenStorage = new CustomAsyncStorage();
|
|
18
|
+
* const loginManager = new AsyncLoginManager(
|
|
19
|
+
* tokenStorage,
|
|
20
|
+
* 'https://api.example.com',
|
|
21
|
+
* 'client_secret',
|
|
22
|
+
* 'client_id',
|
|
23
|
+
* 'openid profile'
|
|
24
|
+
* );
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
3
27
|
export declare class AsyncLoginManager extends BaseLoginManager<AsyncTokenStorage> implements LoginManager {
|
|
4
28
|
signOut(): Promise<void>;
|
|
5
29
|
isSigned(): Promise<boolean>;
|
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when token refresh fails.
|
|
3
|
+
*
|
|
4
|
+
* Indicates that the refresh token is invalid or expired, requiring user to sign in again.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* try {
|
|
9
|
+
* const token = await loginManager.getToken();
|
|
10
|
+
* } catch (error) {
|
|
11
|
+
* if (error instanceof CannotRefreshToken) {
|
|
12
|
+
* console.log('User needs to sign in again');
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
1
17
|
export declare class CannotRefreshToken extends Error {
|
|
2
18
|
constructor(m: string);
|
|
3
19
|
}
|
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integrates Facebook Login SDK for web applications.
|
|
3
|
+
*
|
|
4
|
+
* Handles Facebook authentication flow and provides access tokens for OAuth2 sign-in.
|
|
5
|
+
* Automatically loads Facebook SDK and manages login state.
|
|
6
|
+
*
|
|
7
|
+
* @param facebookAppId - Facebook App ID
|
|
8
|
+
* @param facebookPermissions - Comma-separated Facebook permissions
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const facebookClient = new FacebookClient('your-app-id', 'email,public_profile');
|
|
12
|
+
* facebookClient.setup();
|
|
13
|
+
* facebookClient.login((accessToken) => {
|
|
14
|
+
* return loginManager.trySignInWithFacebook(accessToken);
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
1
18
|
export declare class FacebookClient {
|
|
2
19
|
private facebookAppId;
|
|
3
20
|
private facebookPermissions;
|
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
import { SyncTokenStorage, Token } from "./tokenStorage";
|
|
2
|
+
/**
|
|
3
|
+
* Stores OAuth2 tokens in browser localStorage.
|
|
4
|
+
*
|
|
5
|
+
* Provides persistent token storage that survives browser sessions.
|
|
6
|
+
* Implements SyncTokenStorage interface for synchronous operations.
|
|
7
|
+
*
|
|
8
|
+
* @param tokenKey - localStorage key for access token (default: "token")
|
|
9
|
+
* @param refreshKey - localStorage key for refresh token (default: "refresh_token")
|
|
10
|
+
* @param expiryKey - localStorage key for expiry date (default: "expiration_date")
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const storage = new LocalTokenStorage();
|
|
14
|
+
* const loginManager = new SyncLoginManager(storage, endpoint, secret, clientId, scopes);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
2
17
|
export declare class LocalTokenStorage implements SyncTokenStorage {
|
|
3
18
|
private tokenKey;
|
|
4
19
|
private refreshKey;
|
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
import { SyncTokenStorage, Token } from "./tokenStorage";
|
|
2
|
+
/**
|
|
3
|
+
* Stores OAuth2 tokens in memory.
|
|
4
|
+
*
|
|
5
|
+
* Provides temporary token storage that clears when the page is refreshed.
|
|
6
|
+
* Implements SyncTokenStorage interface for synchronous operations.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const storage = new MemoryTokenStorage();
|
|
11
|
+
* const loginManager = new SyncLoginManager(storage, endpoint, secret, clientId, scopes);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
2
14
|
export declare class MemoryTokenStorage implements SyncTokenStorage {
|
|
3
15
|
private token;
|
|
4
16
|
getToken(): Token | null;
|
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
import { SyncTokenStorage, Token } from "./tokenStorage";
|
|
2
|
+
/**
|
|
3
|
+
* Stores OAuth2 tokens in browser sessionStorage.
|
|
4
|
+
*
|
|
5
|
+
* Provides session-based token storage that clears when the browser tab is closed.
|
|
6
|
+
* Implements SyncTokenStorage interface for synchronous operations.
|
|
7
|
+
*
|
|
8
|
+
* @param tokenKey - sessionStorage key for access token (default: "token")
|
|
9
|
+
* @param refreshKey - sessionStorage key for refresh token (default: "refresh_token")
|
|
10
|
+
* @param expiryKey - sessionStorage key for expiry date (default: "expiration_date")
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const storage = new SessionTokenStorage();
|
|
14
|
+
* const loginManager = new SyncLoginManager(storage, endpoint, secret, clientId, scopes);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
2
17
|
export declare class SessionTokenStorage implements SyncTokenStorage {
|
|
3
18
|
private tokenKey;
|
|
4
19
|
private refreshKey;
|
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
import { BaseLoginManager, LoginManager } from "./baseLoginManager";
|
|
2
2
|
import { SyncTokenStorage } from "./tokenStorage";
|
|
3
|
+
/**
|
|
4
|
+
* Manages OAuth2 authentication with synchronous token storage.
|
|
5
|
+
*
|
|
6
|
+
* Extends BaseLoginManager to work with sync storage implementations like localStorage or sessionStorage.
|
|
7
|
+
* Handles token refresh, expiration, and authentication state management.
|
|
8
|
+
*
|
|
9
|
+
* @param storage - Token storage implementation
|
|
10
|
+
* @param endpoint - OAuth2 server endpoint
|
|
11
|
+
* @param clientSecret - Client secret for authentication
|
|
12
|
+
* @param clientId - OAuth2 client identifier
|
|
13
|
+
* @param scopes - Space-separated OAuth2 scopes
|
|
14
|
+
* @param additionalParams - Additional OAuth2 parameters
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const tokenStorage = new LocalTokenStorage();
|
|
18
|
+
* const loginManager = new SyncLoginManager(
|
|
19
|
+
* tokenStorage,
|
|
20
|
+
* 'https://api.example.com',
|
|
21
|
+
* 'client_secret',
|
|
22
|
+
* 'client_id',
|
|
23
|
+
* 'openid profile'
|
|
24
|
+
* );
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
3
27
|
export declare class SyncLoginManager extends BaseLoginManager<SyncTokenStorage> implements LoginManager {
|
|
4
28
|
signOut(): void;
|
|
5
29
|
isSigned(): boolean;
|
package/index.esm.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./src/index";
|
|
File without changes
|