@mhiliger/auth-fe 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 +165 -0
- package/dist/auth-fe.es.js +1290 -0
- package/dist/auth-fe.umd.js +30 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# @your-org/auth-fe
|
|
2
|
+
|
|
3
|
+
Shared authentication and registration library for React applications. This package provides components and hooks to implement a complete user registration and login workflow, including email verification, password setup, and role-based access control.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @your-org/auth-fe @tanstack/react-query axios jwt-decode react-hook-form @hookform/resolvers yup @mui/material @emotion/react @emotion/styled
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Ensure you also have `react` and `react-router-dom` installed.
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
### 1. Wrap your application in `AuthProvider`
|
|
16
|
+
|
|
17
|
+
In your root component (e.g., `main.jsx` or `App.jsx`), wrap your application with `AuthProvider` and `QueryClientProvider`.
|
|
18
|
+
|
|
19
|
+
```jsx
|
|
20
|
+
import React from "react";
|
|
21
|
+
import ReactDOM from "react-dom/client";
|
|
22
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
23
|
+
import { AuthProvider } from "@your-org/auth-fe";
|
|
24
|
+
import App from "./App";
|
|
25
|
+
|
|
26
|
+
const queryClient = new QueryClient();
|
|
27
|
+
|
|
28
|
+
ReactDOM.createRoot(document.getElementById("root")).render(
|
|
29
|
+
<React.StrictMode>
|
|
30
|
+
<QueryClientProvider client={queryClient}>
|
|
31
|
+
<AuthProvider>
|
|
32
|
+
<App />
|
|
33
|
+
</AuthProvider>
|
|
34
|
+
</QueryClientProvider>
|
|
35
|
+
</React.StrictMode>
|
|
36
|
+
);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 2. Configure Axios
|
|
40
|
+
|
|
41
|
+
Create an `axios` instance and use the `useSecureAxios` hook to automatically attach the access token to requests and handle refresh token logic.
|
|
42
|
+
|
|
43
|
+
```jsx
|
|
44
|
+
// src/api/axios.js
|
|
45
|
+
import axios from "axios";
|
|
46
|
+
import { useSecureAxios } from "@your-org/auth-fe";
|
|
47
|
+
|
|
48
|
+
const BASE_URL = "https://api.yourdomain.com";
|
|
49
|
+
|
|
50
|
+
export const axiosPrivate = axios.create({
|
|
51
|
+
baseURL: BASE_URL,
|
|
52
|
+
headers: { "Content-Type": "application/json" },
|
|
53
|
+
withCredentials: true,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Hook to get the secured instance
|
|
57
|
+
export const useAxiosPrivate = () => {
|
|
58
|
+
return useSecureAxios(axiosPrivate, "/refresh");
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 3. Implement Wrapper Components
|
|
63
|
+
|
|
64
|
+
The library components often require you to pass your specific API hooks or configuration. Create wrappers for the main pages.
|
|
65
|
+
|
|
66
|
+
**Login Wrapper (`src/components/Login.jsx`):**
|
|
67
|
+
|
|
68
|
+
```jsx
|
|
69
|
+
import React from "react";
|
|
70
|
+
import { Login as LibLogin, useLogin } from "@your-org/auth-fe";
|
|
71
|
+
import { axiosPrivate } from "../api/axios"; // Your axios instance
|
|
72
|
+
|
|
73
|
+
const Login = (props) => {
|
|
74
|
+
// Initialize the login mutation
|
|
75
|
+
const loginMutation = useLogin(axiosPrivate, "/auth"); // Point to your login endpoint
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<LibLogin
|
|
79
|
+
{...props}
|
|
80
|
+
loginMutation={loginMutation}
|
|
81
|
+
successRoute="/dashboard" // Redirect after login
|
|
82
|
+
registerPath="/register"
|
|
83
|
+
resetPath="/forgot-password"
|
|
84
|
+
/>
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export default Login;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**Registration Wrapper (`src/components/RegistrationRequest.jsx`):**
|
|
92
|
+
|
|
93
|
+
```jsx
|
|
94
|
+
import React from "react";
|
|
95
|
+
import { RegistrationRequest as LibRegister, useRegistrationSubmit } from "@your-org/auth-fe";
|
|
96
|
+
import { axiosPrivate } from "../api/axios";
|
|
97
|
+
|
|
98
|
+
const RegistrationRequest = () => {
|
|
99
|
+
const submitMutation = useRegistrationSubmit(axiosPrivate, "/register/submit");
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<LibRegister
|
|
103
|
+
submitMutation={submitMutation}
|
|
104
|
+
loginPath="/login"
|
|
105
|
+
/>
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export default RegistrationRequest;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 4. Define Routes
|
|
113
|
+
|
|
114
|
+
Use `StandardAuthRoutes` to set up your routing structure with protection.
|
|
115
|
+
|
|
116
|
+
```jsx
|
|
117
|
+
import React from "react";
|
|
118
|
+
import { BrowserRouter, Route } from "react-router-dom";
|
|
119
|
+
import { StandardAuthRoutes, RequireAuth } from "@your-org/auth-fe";
|
|
120
|
+
import Login from "./Login"; // Your wrapper
|
|
121
|
+
import RegistrationRequest from "./RegistrationRequest"; // Your wrapper
|
|
122
|
+
import Unauthorized from "./Unauthorized"; // Your wrapper or lib component
|
|
123
|
+
import Layout from "./Layout"; // Your layout component
|
|
124
|
+
|
|
125
|
+
const AppRoutes = () => {
|
|
126
|
+
return (
|
|
127
|
+
<BrowserRouter>
|
|
128
|
+
<StandardAuthRoutes
|
|
129
|
+
LoginComponent={Login}
|
|
130
|
+
RegisterComponent={RegistrationRequest}
|
|
131
|
+
UnauthorizedComponent={Unauthorized}
|
|
132
|
+
LayoutComponent={Layout}
|
|
133
|
+
postLoginRedirect="/dashboard"
|
|
134
|
+
publicRoutes={[
|
|
135
|
+
// Routes accessible without login
|
|
136
|
+
{ path: "/about", element: <AboutPage /> }
|
|
137
|
+
]}
|
|
138
|
+
>
|
|
139
|
+
{/* Protected Routes */}
|
|
140
|
+
<Route element={<RequireAuth allowedPerms={["AllowUsers"]} />}>
|
|
141
|
+
<Route path="/dashboard" element={<Dashboard />} />
|
|
142
|
+
</Route>
|
|
143
|
+
</StandardAuthRoutes>
|
|
144
|
+
</BrowserRouter>
|
|
145
|
+
);
|
|
146
|
+
};
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Available Components
|
|
150
|
+
|
|
151
|
+
- **`Login`**: Standard login form.
|
|
152
|
+
- **`RegistrationRequest`**: Initial sign-up form.
|
|
153
|
+
- **`EmailVerification`**: Handles email verification link (path: `/register/verify/:token`).
|
|
154
|
+
- **`PasswordSetup`**: Handles setting up password (path: `/register/setup/:token`).
|
|
155
|
+
- **`PasswordResetRequest`**: Forgot password form.
|
|
156
|
+
- **`RequireAuth`**: Route guard component.
|
|
157
|
+
- **`StandardAuthRoutes`**: Opinionated router setup.
|
|
158
|
+
|
|
159
|
+
## Available Hooks
|
|
160
|
+
|
|
161
|
+
- **`useAuth`**: Access `auth` state (user, permissions, accessToken).
|
|
162
|
+
- **`useSecureAxios`**: Adds interceptors for JWT auth.
|
|
163
|
+
- **`useLogin`**: React Query mutation for login.
|
|
164
|
+
- **`useRegistrationSubmit`**: React Query mutation for registration.
|
|
165
|
+
- **`usePasswordSetup`**: React Query hooks for password setup flow.
|