@drawboard/authagonal-login 0.1.3 → 0.1.5
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 +277 -60
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,73 +1,290 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @drawboard/authagonal-login
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Default login UI for [Authagonal](https://github.com/DrawboardLtd/authagonal) — an OAuth 2.0 / OpenID Connect authentication server backed by Azure Table Storage.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Use as a standalone app (built into the Authagonal Docker image) or as an npm package to build a custom login experience while reusing the API client, branding, i18n, and base components.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/)
|
|
7
|
+
## Installation
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
```bash
|
|
10
|
+
npm install @drawboard/authagonal-login
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Peer dependencies: `react`, `react-dom`, `react-router-dom`.
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
Import the base components and styles, then mount the router:
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { BrowserRouter, Routes, Route } from 'react-router-dom';
|
|
21
|
+
import { AuthLayout, LoginPage, ForgotPasswordPage, ResetPasswordPage } from '@drawboard/authagonal-login';
|
|
22
|
+
import '@drawboard/authagonal-login/styles.css';
|
|
23
|
+
|
|
24
|
+
function App() {
|
|
25
|
+
return (
|
|
26
|
+
<BrowserRouter>
|
|
27
|
+
<Routes>
|
|
28
|
+
<Route element={<AuthLayout />}>
|
|
29
|
+
<Route path="/login" element={<LoginPage />} />
|
|
30
|
+
<Route path="/forgot-password" element={<ForgotPasswordPage />} />
|
|
31
|
+
<Route path="/reset-password" element={<ResetPasswordPage />} />
|
|
32
|
+
</Route>
|
|
33
|
+
</Routes>
|
|
34
|
+
</BrowserRouter>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Customizing pages
|
|
40
|
+
|
|
41
|
+
Override individual pages while keeping the rest. Your custom page has access to the same API client, branding hooks, and i18n as the built-in pages:
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
import { AuthLayout, ForgotPasswordPage, ResetPasswordPage } from '@drawboard/authagonal-login';
|
|
45
|
+
import { login, useBranding, useTranslation, ApiRequestError } from '@drawboard/authagonal-login';
|
|
46
|
+
import '@drawboard/authagonal-login/styles.css';
|
|
47
|
+
|
|
48
|
+
function MyLoginPage() {
|
|
49
|
+
const { t } = useTranslation();
|
|
50
|
+
const branding = useBranding();
|
|
51
|
+
const [agreedToTerms, setAgreedToTerms] = useState(false);
|
|
52
|
+
|
|
53
|
+
async function handleSubmit(email: string, password: string) {
|
|
54
|
+
if (!agreedToTerms) throw new Error('You must agree to the Terms of Service');
|
|
55
|
+
await login(email, password);
|
|
56
|
+
window.location.href = '/';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<form onSubmit={/* ... */}>
|
|
61
|
+
{/* Your custom UI using t(), branding, login(), etc. */}
|
|
62
|
+
</form>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function App() {
|
|
67
|
+
return (
|
|
68
|
+
<BrowserRouter>
|
|
69
|
+
<Routes>
|
|
70
|
+
<Route element={<AuthLayout />}>
|
|
71
|
+
<Route path="/login" element={<MyLoginPage />} />
|
|
72
|
+
<Route path="/forgot-password" element={<ForgotPasswordPage />} />
|
|
73
|
+
<Route path="/reset-password" element={<ResetPasswordPage />} />
|
|
74
|
+
</Route>
|
|
75
|
+
</Routes>
|
|
76
|
+
</BrowserRouter>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
See [`demos/custom-server/login-app`](https://github.com/DrawboardLtd/authagonal/tree/master/demos/custom-server/login-app) for a complete working example with a Terms of Service checkbox and branded footer.
|
|
82
|
+
|
|
83
|
+
## API client
|
|
84
|
+
|
|
85
|
+
All functions call the Authagonal auth API with cookie credentials. Set `VITE_API_URL` to point to a different origin during development.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { login, logout, forgotPassword, resetPassword, getSession, ssoCheck, getProviders, getPasswordPolicy, ApiRequestError } from '@drawboard/authagonal-login';
|
|
89
|
+
|
|
90
|
+
// Password login — sets a session cookie
|
|
91
|
+
await login('user@example.com', 'password');
|
|
92
|
+
|
|
93
|
+
// End the session
|
|
94
|
+
await logout();
|
|
95
|
+
|
|
96
|
+
// Check if the user has an active session
|
|
97
|
+
const session = await getSession();
|
|
98
|
+
// → { authenticated: true, userId, email, name }
|
|
99
|
+
|
|
100
|
+
// Check if an email domain requires SSO
|
|
101
|
+
const sso = await ssoCheck('user@corp.com');
|
|
102
|
+
// → { ssoRequired: true, redirectUrl: '/oidc/azure/login' }
|
|
103
|
+
|
|
104
|
+
// List configured external providers (Google, Azure AD, etc.)
|
|
105
|
+
const { providers } = await getProviders();
|
|
106
|
+
// → [{ connectionId: 'google', name: 'Google', loginUrl: '/oidc/google/login' }]
|
|
107
|
+
|
|
108
|
+
// Password reset flow
|
|
109
|
+
await forgotPassword('user@example.com');
|
|
110
|
+
await resetPassword(token, newPassword);
|
|
111
|
+
|
|
112
|
+
// Fetch password policy rules for frontend validation
|
|
113
|
+
const { rules } = await getPasswordPolicy();
|
|
114
|
+
// → [{ rule: 'MinLength', value: 8, label: 'At least 8 characters' }, ...]
|
|
115
|
+
|
|
116
|
+
// Error handling
|
|
117
|
+
try {
|
|
118
|
+
await login(email, password);
|
|
119
|
+
} catch (err) {
|
|
120
|
+
if (err instanceof ApiRequestError) {
|
|
121
|
+
switch (err.error) {
|
|
122
|
+
case 'invalid_credentials': /* wrong email/password */ break;
|
|
123
|
+
case 'locked_out': /* account locked, err.retryAfter has seconds */ break;
|
|
124
|
+
case 'email_not_confirmed': /* email verification pending */ break;
|
|
125
|
+
case 'sso_required': /* must use SSO, err.redirectUrl has the URL */ break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Branding
|
|
11
132
|
|
|
12
|
-
|
|
133
|
+
Place a `branding.json` in your public directory. The `AuthLayout` component loads it automatically.
|
|
13
134
|
|
|
14
|
-
|
|
135
|
+
```json
|
|
136
|
+
{
|
|
137
|
+
"appName": "My App",
|
|
138
|
+
"logoUrl": "/logo.png",
|
|
139
|
+
"primaryColor": "#2563eb",
|
|
140
|
+
"supportEmail": "help@example.com",
|
|
141
|
+
"showForgotPassword": true,
|
|
142
|
+
"customCssUrl": "/custom.css",
|
|
143
|
+
"welcomeTitle": "Welcome to My App",
|
|
144
|
+
"welcomeSubtitle": "Sign in to continue"
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### BrandingConfig fields
|
|
15
149
|
|
|
16
|
-
|
|
150
|
+
| Field | Type | Default | Description |
|
|
151
|
+
|---|---|---|---|
|
|
152
|
+
| `appName` | `string` | `"Authagonal"` | Shown in the header and page title |
|
|
153
|
+
| `logoUrl` | `string \| null` | `null` | Image URL replacing the text header |
|
|
154
|
+
| `primaryColor` | `string` | `"#2563eb"` | Buttons, links, focus rings via CSS custom properties |
|
|
155
|
+
| `supportEmail` | `string \| null` | `null` | Contact email shown in the footer |
|
|
156
|
+
| `showForgotPassword` | `boolean` | `true` | Toggle the forgot password link |
|
|
157
|
+
| `customCssUrl` | `string \| null` | `null` | URL to additional CSS for deeper styling |
|
|
158
|
+
| `welcomeTitle` | `LocalizedString` | `null` | Override the login page title |
|
|
159
|
+
| `welcomeSubtitle` | `LocalizedString` | `null` | Override the login page subtitle |
|
|
17
160
|
|
|
18
|
-
|
|
19
|
-
export default defineConfig([
|
|
20
|
-
globalIgnores(['dist']),
|
|
21
|
-
{
|
|
22
|
-
files: ['**/*.{ts,tsx}'],
|
|
23
|
-
extends: [
|
|
24
|
-
// Other configs...
|
|
161
|
+
### Localized strings
|
|
25
162
|
|
|
26
|
-
|
|
27
|
-
tseslint.configs.recommendedTypeChecked,
|
|
28
|
-
// Alternatively, use this for stricter rules
|
|
29
|
-
tseslint.configs.strictTypeChecked,
|
|
30
|
-
// Optionally, add this for stylistic rules
|
|
31
|
-
tseslint.configs.stylisticTypeChecked,
|
|
163
|
+
`welcomeTitle` and `welcomeSubtitle` accept either a plain string or an object mapping language codes to strings:
|
|
32
164
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
])
|
|
165
|
+
```json
|
|
166
|
+
{
|
|
167
|
+
"welcomeTitle": {
|
|
168
|
+
"en": "Welcome to Acme",
|
|
169
|
+
"es": "Bienvenido a Acme",
|
|
170
|
+
"de": "Willkommen bei Acme"
|
|
171
|
+
}
|
|
172
|
+
}
|
|
44
173
|
```
|
|
45
174
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
globalIgnores(['dist']),
|
|
55
|
-
{
|
|
56
|
-
files: ['**/*.{ts,tsx}'],
|
|
57
|
-
extends: [
|
|
58
|
-
// Other configs...
|
|
59
|
-
// Enable lint rules for React
|
|
60
|
-
reactX.configs['recommended-typescript'],
|
|
61
|
-
// Enable lint rules for React DOM
|
|
62
|
-
reactDom.configs.recommended,
|
|
63
|
-
],
|
|
64
|
-
languageOptions: {
|
|
65
|
-
parserOptions: {
|
|
66
|
-
project: ['./tsconfig.node.json', './tsconfig.app.json'],
|
|
67
|
-
tsconfigRootDir: import.meta.dirname,
|
|
68
|
-
},
|
|
69
|
-
// other options...
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
])
|
|
175
|
+
Use `resolveLocalized()` to resolve these in your own components:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
import { resolveLocalized, useBranding, useTranslation } from '@drawboard/authagonal-login';
|
|
179
|
+
|
|
180
|
+
const branding = useBranding();
|
|
181
|
+
const { i18n } = useTranslation();
|
|
182
|
+
const title = resolveLocalized(branding.welcomeTitle, i18n.language) ?? 'Default Title';
|
|
73
183
|
```
|
|
184
|
+
|
|
185
|
+
## i18n
|
|
186
|
+
|
|
187
|
+
Built-in support for 7 languages:
|
|
188
|
+
|
|
189
|
+
| Code | Language |
|
|
190
|
+
|---|---|
|
|
191
|
+
| `en` | English |
|
|
192
|
+
| `zh-Hans` | Chinese (Simplified) |
|
|
193
|
+
| `de` | German |
|
|
194
|
+
| `fr` | French |
|
|
195
|
+
| `es` | Spanish |
|
|
196
|
+
| `vi` | Vietnamese |
|
|
197
|
+
| `pt` | Portuguese |
|
|
198
|
+
|
|
199
|
+
Language is auto-detected from the browser and persisted to `localStorage`. Force a language via query string: `?lng=es`.
|
|
200
|
+
|
|
201
|
+
The `useTranslation` hook is re-exported from this package to avoid React context duplication. Always import it from `@drawboard/authagonal-login`, not directly from `react-i18next`:
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
// Correct
|
|
205
|
+
import { useTranslation } from '@drawboard/authagonal-login';
|
|
206
|
+
|
|
207
|
+
// Wrong — will get a different i18n instance
|
|
208
|
+
import { useTranslation } from 'react-i18next';
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Exports
|
|
212
|
+
|
|
213
|
+
### Components
|
|
214
|
+
|
|
215
|
+
| Export | Description |
|
|
216
|
+
|---|---|
|
|
217
|
+
| `AuthLayout` | Layout wrapper — loads branding, renders language selector, wraps `<Outlet />` |
|
|
218
|
+
| `LoginPage` | Login form with SSO check, external providers, session detection |
|
|
219
|
+
| `ForgotPasswordPage` | Email input → sends reset link |
|
|
220
|
+
| `ResetPasswordPage` | Token + new password form with policy validation |
|
|
221
|
+
|
|
222
|
+
### API client
|
|
223
|
+
|
|
224
|
+
| Export | Description |
|
|
225
|
+
|---|---|
|
|
226
|
+
| `login(email, password)` | Password login |
|
|
227
|
+
| `logout()` | End session |
|
|
228
|
+
| `forgotPassword(email)` | Request password reset |
|
|
229
|
+
| `resetPassword(token, password)` | Complete password reset |
|
|
230
|
+
| `getSession()` | Check current session |
|
|
231
|
+
| `ssoCheck(email)` | Check SSO requirement for email domain |
|
|
232
|
+
| `getProviders()` | List external identity providers |
|
|
233
|
+
| `getPasswordPolicy()` | Fetch password rules |
|
|
234
|
+
| `ApiRequestError` | Error class with `.error`, `.retryAfter`, `.redirectUrl` |
|
|
235
|
+
|
|
236
|
+
### Branding
|
|
237
|
+
|
|
238
|
+
| Export | Description |
|
|
239
|
+
|---|---|
|
|
240
|
+
| `loadBranding()` | Fetch and parse `/branding.json` |
|
|
241
|
+
| `BrandingContext` | React context for branding config |
|
|
242
|
+
| `useBranding()` | Hook to read branding config |
|
|
243
|
+
| `resolveLocalized(value, lang)` | Resolve a `LocalizedString` for a language |
|
|
244
|
+
|
|
245
|
+
### Types
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
type LocalizedString = string | Record<string, string> | null;
|
|
249
|
+
|
|
250
|
+
interface BrandingConfig {
|
|
251
|
+
appName: string;
|
|
252
|
+
logoUrl: string | null;
|
|
253
|
+
primaryColor: string;
|
|
254
|
+
supportEmail: string | null;
|
|
255
|
+
showForgotPassword: boolean;
|
|
256
|
+
customCssUrl: string | null;
|
|
257
|
+
welcomeTitle: LocalizedString;
|
|
258
|
+
welcomeSubtitle: LocalizedString;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
interface ExternalProvider {
|
|
262
|
+
connectionId: string;
|
|
263
|
+
name: string;
|
|
264
|
+
loginUrl: string;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
interface SessionResponse {
|
|
268
|
+
authenticated: boolean;
|
|
269
|
+
userId: string;
|
|
270
|
+
email: string;
|
|
271
|
+
name: string;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
interface SsoCheckResponse {
|
|
275
|
+
ssoRequired: boolean;
|
|
276
|
+
providerType?: string;
|
|
277
|
+
connectionId?: string;
|
|
278
|
+
redirectUrl?: string;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
interface PasswordPolicyRule {
|
|
282
|
+
rule: string;
|
|
283
|
+
value: number | null;
|
|
284
|
+
label: string;
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## License
|
|
289
|
+
|
|
290
|
+
Proprietary — Drawboard Ltd.
|