@leancodepl/login-manager 8.5.0 → 8.6.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 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
+ ```
@@ -0,0 +1 @@
1
+ exports._default = require('./index.cjs.js').default;