@truworth/twc-auth 1.0.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 +343 -0
- package/dist/index.esm.js +2026 -0
- package/dist/index.umd.js +70 -0
- package/dist/vite.svg +1 -0
- package/package.json +50 -0
package/README.md
ADDED
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
# TWC Auth Package
|
|
2
|
+
|
|
3
|
+
A React authentication package for The Wellness Corner that provides a simple email/password login component with callback-based API integration.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ Email/password authentication flow
|
|
8
|
+
- ✅ Two-step verification (email check → password entry)
|
|
9
|
+
- ✅ Callback-based API integration
|
|
10
|
+
- ✅ TypeScript support
|
|
11
|
+
- ✅ Lightweight and configurable
|
|
12
|
+
- ✅ No external dependencies (except React)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @truworth/twc-package-auth
|
|
18
|
+
# or
|
|
19
|
+
yarn add @truworth/twc-package-auth
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Basic Implementation
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import AuthLogin, { type EmailExistsCallback, type LoginSubmitCallback } from '@truworth/twc-package-auth';
|
|
28
|
+
|
|
29
|
+
function LoginPage() {
|
|
30
|
+
const handleEmailExists: EmailExistsCallback = async (email: string) => {
|
|
31
|
+
try {
|
|
32
|
+
const response = await fetch('/api/email-exists', {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: { 'Content-Type': 'application/json' },
|
|
35
|
+
body: JSON.stringify({ email })
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (response.ok) {
|
|
39
|
+
return { exists: true };
|
|
40
|
+
} else if (response.status === 404) {
|
|
41
|
+
return { exists: false };
|
|
42
|
+
}
|
|
43
|
+
throw new Error('Network error');
|
|
44
|
+
} catch (error) {
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const handleLogin: LoginSubmitCallback = async (email: string, password: string) => {
|
|
50
|
+
const response = await fetch('/api/login', {
|
|
51
|
+
method: 'POST',
|
|
52
|
+
headers: { 'Content-Type': 'application/json' },
|
|
53
|
+
body: JSON.stringify({ email, password })
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
throw new Error('Login failed');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const data = await response.json();
|
|
61
|
+
return { token: data.token };
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const handleSuccess = (token: string) => {
|
|
65
|
+
// Handle successful login
|
|
66
|
+
localStorage.setItem('token', token);
|
|
67
|
+
window.location.href = '/dashboard';
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const handleError = (error: any) => {
|
|
71
|
+
// Handle errors
|
|
72
|
+
if (error.type === 'USER_NOT_FOUND') {
|
|
73
|
+
// Redirect to registration
|
|
74
|
+
window.location.href = `/register?email=${error.email}`;
|
|
75
|
+
} else {
|
|
76
|
+
console.error('Login error:', error);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<AuthLogin
|
|
82
|
+
onEmailExists={handleEmailExists}
|
|
83
|
+
onLogin={handleLogin}
|
|
84
|
+
onSuccess={handleSuccess}
|
|
85
|
+
onError={handleError}
|
|
86
|
+
initialEmail=""
|
|
87
|
+
className="custom-login-styles"
|
|
88
|
+
/>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### API Integration
|
|
94
|
+
|
|
95
|
+
The package expects two API endpoints:
|
|
96
|
+
|
|
97
|
+
#### 1. Email Exists Check (`/api/email-exists`)
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
// POST /api/email-exists
|
|
101
|
+
// Request: { email: string }
|
|
102
|
+
// Response: { exists: boolean, loginType?: number }
|
|
103
|
+
// 404 status for non-existent users
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### 2. Login (`/api/login`)
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
// POST /api/login
|
|
110
|
+
// Request: { email: string, password: string }
|
|
111
|
+
// Response: { token: string }
|
|
112
|
+
// 400 status for invalid credentials
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Props
|
|
116
|
+
|
|
117
|
+
| Prop | Type | Required | Description |
|
|
118
|
+
|------|------|----------|-------------|
|
|
119
|
+
| `onEmailExists` | `EmailExistsCallback` | ✅ | Function to check if email exists |
|
|
120
|
+
| `onLogin` | `LoginSubmitCallback` | ✅ | Function to handle login |
|
|
121
|
+
| `onSuccess` | `(token: string) => void` | ✅ | Callback when login succeeds |
|
|
122
|
+
| `onError` | `(error: any) => void` | ❌ | Callback for error handling |
|
|
123
|
+
| `initialEmail` | `string` | ❌ | Pre-fill email field |
|
|
124
|
+
| `className` | `string` | ❌ | Additional CSS classes |
|
|
125
|
+
|
|
126
|
+
### Error Handling
|
|
127
|
+
|
|
128
|
+
The package provides structured error handling:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
interface LoginError {
|
|
132
|
+
type?: 'USER_NOT_FOUND' | 'INVALID_CREDENTIALS' | 'NETWORK_ERROR';
|
|
133
|
+
message: string;
|
|
134
|
+
email?: string; // Available for USER_NOT_FOUND errors
|
|
135
|
+
redirectToRegistration?: boolean;
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Development
|
|
140
|
+
|
|
141
|
+
### Building the Package
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
npm run build
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Linking for Local Development
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
# In the auth package directory
|
|
151
|
+
npm link
|
|
152
|
+
|
|
153
|
+
# In your host application
|
|
154
|
+
npm link @truworth/twc-package-auth
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Project Structure
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
src/
|
|
161
|
+
├── components/
|
|
162
|
+
│ └── AuthLogin.tsx # Main login component
|
|
163
|
+
├── types.ts # TypeScript type definitions
|
|
164
|
+
└── index.ts # Package exports
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Integration Example
|
|
168
|
+
|
|
169
|
+
See the complete integration example in the host application:
|
|
170
|
+
|
|
171
|
+
- **API Routes**: `pages/api/email-exists.ts` and `pages/api/login.ts`
|
|
172
|
+
- **Login Page**: `pages/login.tsx`
|
|
173
|
+
- **Index Redirect**: `pages/index.tsx`
|
|
174
|
+
|
|
175
|
+
## Migration from Hardcoded Token
|
|
176
|
+
|
|
177
|
+
The package replaces hardcoded token entry with proper authentication:
|
|
178
|
+
|
|
179
|
+
**Before**: Manual token entry via `_enter-token.tsx`
|
|
180
|
+
**After**: Email/password login with proper backend integration
|
|
181
|
+
|
|
182
|
+
## Styling
|
|
183
|
+
|
|
184
|
+
The component uses minimal inline styles and accepts a `className` prop for customization. Default styles include:
|
|
185
|
+
|
|
186
|
+
- Responsive design
|
|
187
|
+
- Form validation states
|
|
188
|
+
- Loading indicators
|
|
189
|
+
- Error messages
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
Private package for The Wellness Corner internal use.
|
|
194
|
+
|
|
195
|
+
# TWC Auth Package Development
|
|
196
|
+
|
|
197
|
+
## Project Overview
|
|
198
|
+
A comprehensive authentication package (`@truworth/twc-package-auth`) built for TWC partner integrations. The package provides a complete login/registration UI that can be integrated into host applications.
|
|
199
|
+
|
|
200
|
+
## 🚀 **Current Status: Phase 2 - Complete UI Implementation**
|
|
201
|
+
|
|
202
|
+
### ✅ **Phase 1 Completed:**
|
|
203
|
+
- Basic email/password login working
|
|
204
|
+
- Token management and auth context integration
|
|
205
|
+
- Host app successfully using auth package
|
|
206
|
+
- API callback system functional
|
|
207
|
+
|
|
208
|
+
### 🔄 **Phase 2 In Progress:**
|
|
209
|
+
Implementing complete production UI from `/Users/soumik.acharjee/twc-repos/twc-v2/pages/login/index.tsx`
|
|
210
|
+
|
|
211
|
+
**Features to Implement:**
|
|
212
|
+
1. **Complete LoginForm Component**
|
|
213
|
+
- Email validation step with proper UI
|
|
214
|
+
- Password entry step with validation
|
|
215
|
+
- Back navigation between steps
|
|
216
|
+
- Forgot password integration
|
|
217
|
+
|
|
218
|
+
2. **SSO Login Flow**
|
|
219
|
+
- Organization search with autocomplete
|
|
220
|
+
- SSO initiation and redirect handling
|
|
221
|
+
- Multi-step UI with transitions
|
|
222
|
+
|
|
223
|
+
3. **OTP/Mobile Sign-in**
|
|
224
|
+
- OTP request and verification
|
|
225
|
+
- Mobile number handling
|
|
226
|
+
- Modal-based UI flow
|
|
227
|
+
|
|
228
|
+
4. **Password Reset Flow**
|
|
229
|
+
- Email-based reset initiation
|
|
230
|
+
- OTP verification for reset
|
|
231
|
+
- New password setting
|
|
232
|
+
|
|
233
|
+
5. **Enhanced UI/UX**
|
|
234
|
+
- Left panel with background image and carousel
|
|
235
|
+
- Smooth transitions between states
|
|
236
|
+
- Proper loading states and error handling
|
|
237
|
+
- Responsive design matching production
|
|
238
|
+
|
|
239
|
+
**Excluded Features:**
|
|
240
|
+
- ❌ Google Sign-in (removed)
|
|
241
|
+
- ❌ Facebook Sign-in (removed)
|
|
242
|
+
|
|
243
|
+
## Architecture Principles
|
|
244
|
+
|
|
245
|
+
### **Pure UI Package with Callbacks**
|
|
246
|
+
- Auth package contains ONLY UI components and logic
|
|
247
|
+
- ALL API calls handled by host application via callbacks
|
|
248
|
+
- No direct API dependencies in the package
|
|
249
|
+
- Host app passes callback functions for all backend operations
|
|
250
|
+
|
|
251
|
+
### **Production Reference**
|
|
252
|
+
- **Source:** `/Users/soumik.acharjee/twc-repos/twc-v2/pages/login/index.tsx`
|
|
253
|
+
- **Target:** `/Users/soumik.acharjee/twc-repos/twc-web-package-host`
|
|
254
|
+
- **Design System:** `@truworth/twc-web-design` for exact UI match
|
|
255
|
+
|
|
256
|
+
## Development Tasks Breakdown
|
|
257
|
+
|
|
258
|
+
### **Task 1: Enhanced Types & Interfaces**
|
|
259
|
+
- [ ] Update `AuthLoginProps` with all callback functions
|
|
260
|
+
- [ ] Add interfaces for SSO, OTP, and Reset Password
|
|
261
|
+
- [ ] Define error types for different scenarios
|
|
262
|
+
|
|
263
|
+
### **Task 2: Complete LoginForm Component**
|
|
264
|
+
- [ ] Implement two-step email/password flow
|
|
265
|
+
- [ ] Add form validation and error states
|
|
266
|
+
- [ ] Integrate forgot password trigger
|
|
267
|
+
|
|
268
|
+
### **Task 3: SSO Login Component**
|
|
269
|
+
- [ ] Build organization search with autocomplete
|
|
270
|
+
- [ ] Create SSO selection and initiation UI
|
|
271
|
+
- [ ] Handle SSO redirect flow
|
|
272
|
+
|
|
273
|
+
### **Task 4: OTP/Mobile Sign-in Component**
|
|
274
|
+
- [ ] Create modal-based OTP interface
|
|
275
|
+
- [ ] Implement OTP request and verification
|
|
276
|
+
- [ ] Add mobile number validation
|
|
277
|
+
|
|
278
|
+
### **Task 5: Password Reset Component**
|
|
279
|
+
- [ ] Build reset password modal
|
|
280
|
+
- [ ] Create OTP verification for reset
|
|
281
|
+
- [ ] Add new password setting form
|
|
282
|
+
|
|
283
|
+
### **Task 6: Main Auth Component Integration**
|
|
284
|
+
- [ ] Create state management for different flows
|
|
285
|
+
- [ ] Implement smooth transitions between states
|
|
286
|
+
- [ ] Add comprehensive error handling
|
|
287
|
+
|
|
288
|
+
### **Task 7: Host App Integration**
|
|
289
|
+
- [ ] Update login.tsx with all callback handlers
|
|
290
|
+
- [ ] Create API route handlers for new endpoints
|
|
291
|
+
- [ ] Test complete integration flow
|
|
292
|
+
|
|
293
|
+
## Current Implementation Status
|
|
294
|
+
|
|
295
|
+
```typescript
|
|
296
|
+
// Current working callbacks
|
|
297
|
+
interface AuthLoginProps {
|
|
298
|
+
onEmailExists: (email: string) => void;
|
|
299
|
+
onLogin: (email: string, password: string) => void;
|
|
300
|
+
onSuccess: (token: string) => void;
|
|
301
|
+
onError?: (error: AuthError | any) => void;
|
|
302
|
+
makeRequest: (params: MakeRequestParams) => void;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Next: Expand to include all production features
|
|
306
|
+
interface CompleteAuthLoginProps extends AuthLoginProps {
|
|
307
|
+
onSSOSearch: (query: string, callback: (results: any[]) => void) => void;
|
|
308
|
+
onSSOLogin: (clientId: string, callback: (authUrl: string) => void) => void;
|
|
309
|
+
onOTPRequest: (email: string, callback: (success: boolean) => void) => void;
|
|
310
|
+
onOTPVerify: (email: string, otp: string, callback: (token: string) => void) => void;
|
|
311
|
+
onPasswordReset: (email: string, callback: (success: boolean) => void) => void;
|
|
312
|
+
// ... additional callbacks
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Integration Pattern
|
|
317
|
+
|
|
318
|
+
The host app handles all API calls and passes results back to the auth package via callbacks:
|
|
319
|
+
|
|
320
|
+
```typescript
|
|
321
|
+
// Host app implementation
|
|
322
|
+
const handleSSOSearch = (query: string, callback: (results: any[]) => void) => {
|
|
323
|
+
makeRequest({
|
|
324
|
+
url: '/auth/login-sso/search-clients',
|
|
325
|
+
method: 'POST',
|
|
326
|
+
data: { query },
|
|
327
|
+
onSuccess: (data) => callback(data),
|
|
328
|
+
onError: (err) => callback([])
|
|
329
|
+
});
|
|
330
|
+
};
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
## Build Commands
|
|
334
|
+
|
|
335
|
+
```bash
|
|
336
|
+
# Development
|
|
337
|
+
npm run dev # Start dev server
|
|
338
|
+
npm run build # Build package
|
|
339
|
+
|
|
340
|
+
# Integration Testing
|
|
341
|
+
cd ../twc-web-package-host
|
|
342
|
+
npm run dev # Test complete integration
|
|
343
|
+
```
|