react-auth-service 0.0.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 +252 -0
- package/dist/auth-ui.es.js +2302 -0
- package/dist/auth-ui.umd.js +11 -0
- package/dist/react-auth-ui.css +1 -0
- package/dist/vite.svg +1 -0
- package/package.json +40 -0
- package/types/App.d.ts +3 -0
- package/types/common/ProtectedRoute.d.ts +12 -0
- package/types/common/token.d.ts +2 -0
- package/types/components/Auth/index.d.ts +4 -0
- package/types/components/UserProfile/index.d.ts +4 -0
- package/types/constants/apiEndpoints.d.ts +9 -0
- package/types/constants/constants.d.ts +8 -0
- package/types/constants/messages.d.ts +6 -0
- package/types/context/AuthContext.d.ts +10 -0
- package/types/enum/auth.d.ts +5 -0
- package/types/hooks/useAuth.d.ts +5 -0
- package/types/index.d.ts +10 -0
- package/types/main.d.ts +1 -0
- package/types/services/axiosInterceptor.d.ts +2 -0
- package/types/types/auth.d.ts +18 -0
- package/types/utils/helperfunction.d.ts +6 -0
package/README.md
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# Auth UI
|
|
2
|
+
|
|
3
|
+
Reusable React **authentication UI library** with JWT handling, Axios interceptors, and routing helpers.
|
|
4
|
+
|
|
5
|
+
> ⚠️ **Routing and navigation are owned by the consumer app**. This library provides UI, auth state, and API handling — not app routes.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Why this package?
|
|
10
|
+
|
|
11
|
+
Most apps repeat the same authentication logic:
|
|
12
|
+
|
|
13
|
+
* Login & signup UI
|
|
14
|
+
* JWT storage and refresh handling
|
|
15
|
+
* Protecting routes
|
|
16
|
+
* Attaching tokens to API calls
|
|
17
|
+
|
|
18
|
+
**Auth UI** centralizes this logic into a reusable package so consumer apps can focus on business features.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Features
|
|
23
|
+
|
|
24
|
+
* 🔐 Login & Signup UI (`AuthForm`)
|
|
25
|
+
* 👤 User profile UI (`UserProfile`)
|
|
26
|
+
* 🧠 Central auth state via `AuthProvider`
|
|
27
|
+
* 🛡️ Public & Protected route guards
|
|
28
|
+
* 🔄 Axios interceptor with automatic token refresh
|
|
29
|
+
* 🚫 Global session-expired handling
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install auth-ui
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Required setup (important)
|
|
42
|
+
|
|
43
|
+
### 1️⃣ Wrap your app with `AuthProvider` and `BrowserRouter`
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
import { BrowserRouter } from 'react-router-dom';
|
|
47
|
+
import { AuthProvider } from 'auth-ui';
|
|
48
|
+
|
|
49
|
+
<BrowserRouter>
|
|
50
|
+
<AuthProvider>
|
|
51
|
+
<App />
|
|
52
|
+
</AuthProvider>
|
|
53
|
+
</BrowserRouter>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
> `BrowserRouter` **must be declared once** in the consumer app.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Routing helpers
|
|
61
|
+
|
|
62
|
+
This package does **not** define routes. Instead, it provides helpers you can compose with your routes.
|
|
63
|
+
|
|
64
|
+
### PublicRoute
|
|
65
|
+
|
|
66
|
+
Used for pages like **Login / Signup**.
|
|
67
|
+
|
|
68
|
+
* Accessible only when user is **not authenticated**
|
|
69
|
+
* Redirects authenticated users
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
<Route
|
|
73
|
+
path="/"
|
|
74
|
+
element={
|
|
75
|
+
<PublicRoute>
|
|
76
|
+
<AuthForm onLoginSuccess={login} />
|
|
77
|
+
</PublicRoute>
|
|
78
|
+
}
|
|
79
|
+
/>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### ProtectedRoute
|
|
83
|
+
|
|
84
|
+
Used for **private pages**.
|
|
85
|
+
|
|
86
|
+
* Blocks unauthenticated access
|
|
87
|
+
* Redirects to login page
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
<Route
|
|
91
|
+
path="/profile"
|
|
92
|
+
element={
|
|
93
|
+
<ProtectedRoute>
|
|
94
|
+
<UserProfile onLogout={logout} />
|
|
95
|
+
</ProtectedRoute>
|
|
96
|
+
}
|
|
97
|
+
/>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Auth flow (how it works)
|
|
103
|
+
|
|
104
|
+
### Login
|
|
105
|
+
|
|
106
|
+
1. User submits login form
|
|
107
|
+
2. Login API returns `accessToken`
|
|
108
|
+
3. Token is stored in memory
|
|
109
|
+
4. `AuthContext` updates auth state
|
|
110
|
+
5. Consumer app handles navigation
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
setAccessToken(token);
|
|
114
|
+
localStorage.setItem('isLoggedIn', 'true');
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
### Signup
|
|
120
|
+
|
|
121
|
+
1. User submits signup form
|
|
122
|
+
2. Password is validated on client
|
|
123
|
+
3. Registration API is called
|
|
124
|
+
4. User is redirected to login mode
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## Axios interceptor (core logic)
|
|
129
|
+
|
|
130
|
+
This package exports a **preconfigured `axiosInstance`**.
|
|
131
|
+
|
|
132
|
+
### What it does
|
|
133
|
+
|
|
134
|
+
* Attaches `Authorization: Bearer <token>` automatically
|
|
135
|
+
* Skips auth headers for login & signup APIs
|
|
136
|
+
* Refreshes token automatically on expiry
|
|
137
|
+
* Queues failed requests during refresh
|
|
138
|
+
* Logs out user if refresh fails
|
|
139
|
+
|
|
140
|
+
### Usage in consumer app
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import { axiosInstance } from 'auth-ui';
|
|
144
|
+
|
|
145
|
+
axiosInstance.get('/api/orders');
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
You **must use `axiosInstance`** instead of raw Axios to get auth benefits.
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## Session expiration handling
|
|
153
|
+
|
|
154
|
+
If refresh token fails:
|
|
155
|
+
|
|
156
|
+
* User is logged out
|
|
157
|
+
* Session-expired flag is stored
|
|
158
|
+
* App redirects to login page
|
|
159
|
+
|
|
160
|
+
On next load, `AuthForm` detects this and shows a session-expired message.
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Auth state management
|
|
165
|
+
|
|
166
|
+
Auth state is managed via `AuthContext`.
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
const { isAuthenticated, login, logout } = useAuth();
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
* State is persisted using `localStorage`
|
|
173
|
+
* Token is cleared on logout
|
|
174
|
+
* Context ensures route guards stay in sync
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## UserProfile component
|
|
179
|
+
|
|
180
|
+
* Fetches user details on mount
|
|
181
|
+
* Uses `axiosInstance` internally
|
|
182
|
+
* Calls logout API on logout
|
|
183
|
+
* Delegates navigation to consumer
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
<UserProfile onLogout={logout} />
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## Consumer responsibilities
|
|
192
|
+
|
|
193
|
+
The consumer app **must**:
|
|
194
|
+
|
|
195
|
+
* Own routing and navigation
|
|
196
|
+
* Provide backend APIs
|
|
197
|
+
* Configure endpoints
|
|
198
|
+
* Wrap app with `AuthProvider`
|
|
199
|
+
* Use provided `axiosInstance`
|
|
200
|
+
|
|
201
|
+
This library **does not**:
|
|
202
|
+
|
|
203
|
+
* Create routes
|
|
204
|
+
* Handle databases
|
|
205
|
+
* Manage backend authentication
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Backend expectations
|
|
210
|
+
|
|
211
|
+
Your backend should provide:
|
|
212
|
+
|
|
213
|
+
* `POST /login` → returns `accessToken`
|
|
214
|
+
* `POST /register`
|
|
215
|
+
* `POST /refresh-token` (cookie-based)
|
|
216
|
+
* `GET /user-details`
|
|
217
|
+
* `POST /logout`
|
|
218
|
+
|
|
219
|
+
JWT-based authentication is assumed.
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Common mistakes
|
|
224
|
+
|
|
225
|
+
❌ Importing Axios directly instead of `axiosInstance`
|
|
226
|
+
❌ Wrapping `BrowserRouter` multiple times
|
|
227
|
+
❌ Using `useAuth` outside `AuthProvider`
|
|
228
|
+
❌ Expecting this package to control routing
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## Summary
|
|
233
|
+
|
|
234
|
+
**Auth UI** provides:
|
|
235
|
+
|
|
236
|
+
* UI components
|
|
237
|
+
* Auth state management
|
|
238
|
+
* Secure API handling
|
|
239
|
+
|
|
240
|
+
You control:
|
|
241
|
+
|
|
242
|
+
* Routing
|
|
243
|
+
* Navigation
|
|
244
|
+
* Backend
|
|
245
|
+
|
|
246
|
+
This separation keeps your app flexible, secure, and scalable.
|
|
247
|
+
|
|
248
|
+
---
|
|
249
|
+
|
|
250
|
+
## License
|
|
251
|
+
|
|
252
|
+
MIT
|