@ttoss/react-auth-core 0.2.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/LICENSE +21 -0
- package/README.md +177 -0
- package/dist/esm/index.js +1091 -0
- package/dist/index.d.cts +175 -0
- package/dist/index.d.ts +175 -0
- package/dist/index.js +1158 -0
- package/i18n/compiled/en.json +272 -0
- package/i18n/lang/en.json +155 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, Terezinha Tech Operations (ttoss)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# @ttoss/react-auth-core
|
|
2
|
+
|
|
3
|
+
Provider-agnostic authentication components and abstractions for React applications with screen-based flow management.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @ttoss/react-auth-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quickstart
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import {
|
|
15
|
+
AuthProvider,
|
|
16
|
+
Auth,
|
|
17
|
+
useAuth,
|
|
18
|
+
useAuthScreen,
|
|
19
|
+
} from '@ttoss/react-auth-core';
|
|
20
|
+
|
|
21
|
+
// 1. Wrap your app with AuthProvider
|
|
22
|
+
function App() {
|
|
23
|
+
return (
|
|
24
|
+
<AuthProvider signOut={async () => await myAuthService.signOut()}>
|
|
25
|
+
<AuthenticatedApp />
|
|
26
|
+
</AuthProvider>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 2. Create auth flow with handlers
|
|
31
|
+
function LoginPage() {
|
|
32
|
+
const { screen, setScreen } = useAuthScreen();
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<Auth
|
|
36
|
+
screen={screen}
|
|
37
|
+
setScreen={setScreen}
|
|
38
|
+
onSignIn={async ({ email, password }) => {
|
|
39
|
+
await myAuthService.signIn(email, password);
|
|
40
|
+
}}
|
|
41
|
+
onSignUp={async ({ email, password }) => {
|
|
42
|
+
await myAuthService.signUp(email, password);
|
|
43
|
+
setScreen({ value: 'confirmSignUpCheckEmail' });
|
|
44
|
+
}}
|
|
45
|
+
/>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 3. Use authentication state
|
|
50
|
+
function AuthenticatedApp() {
|
|
51
|
+
const { isAuthenticated, user, signOut } = useAuth();
|
|
52
|
+
|
|
53
|
+
if (!isAuthenticated) {
|
|
54
|
+
return <LoginPage />;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<div>
|
|
59
|
+
<p>Welcome, {user?.email}</p>
|
|
60
|
+
<button onClick={signOut}>Sign Out</button>
|
|
61
|
+
</div>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Features
|
|
67
|
+
|
|
68
|
+
- **Screen-based flows**: Manages authentication through distinct screens (signIn, signUp, forgotPassword, etc.)
|
|
69
|
+
- **Provider agnostic**: Works with any authentication service - you provide the handlers
|
|
70
|
+
- **Type-safe**: Full TypeScript support with comprehensive type definitions
|
|
71
|
+
- **UI components**: Pre-built forms and layouts for common authentication flows
|
|
72
|
+
- **Error handling**: Built-in error boundaries and proper error management
|
|
73
|
+
|
|
74
|
+
## Core Concepts
|
|
75
|
+
|
|
76
|
+
### Authentication Screens
|
|
77
|
+
|
|
78
|
+
```mermaid
|
|
79
|
+
flowchart TD
|
|
80
|
+
A[signIn] --> B[signUp]
|
|
81
|
+
A --> C[forgotPassword]
|
|
82
|
+
B --> D[confirmSignUpWithCode]
|
|
83
|
+
B --> E[confirmSignUpCheckEmail]
|
|
84
|
+
C --> F[confirmResetPassword]
|
|
85
|
+
D --> A
|
|
86
|
+
E --> A
|
|
87
|
+
F --> A
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Provider Pattern
|
|
91
|
+
|
|
92
|
+
The package uses React Context to manage authentication state across your application. You provide the authentication logic, and the components handle the UI and flow management.
|
|
93
|
+
|
|
94
|
+
## API Reference
|
|
95
|
+
|
|
96
|
+
### AuthProvider
|
|
97
|
+
|
|
98
|
+
Wraps your application to provide authentication context.
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
<AuthProvider
|
|
102
|
+
getAuthData={() => Promise<AuthData>} // Optional: fetch initial auth state
|
|
103
|
+
signOut={() => Promise<void>} // Required: sign out handler
|
|
104
|
+
>
|
|
105
|
+
{children}
|
|
106
|
+
</AuthProvider>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### useAuth Hook
|
|
110
|
+
|
|
111
|
+
Access authentication state and actions.
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
const {
|
|
115
|
+
isAuthenticated, // boolean
|
|
116
|
+
user, // AuthUser | null
|
|
117
|
+
tokens, // AuthTokens | null
|
|
118
|
+
signOut, // () => Promise<void>
|
|
119
|
+
setAuthData, // Update auth state manually
|
|
120
|
+
} = useAuth();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Auth Component
|
|
124
|
+
|
|
125
|
+
Main authentication flow component.
|
|
126
|
+
|
|
127
|
+
```tsx
|
|
128
|
+
<Auth
|
|
129
|
+
screen={screen} // Current screen state
|
|
130
|
+
setScreen={setScreen} // Screen navigation function
|
|
131
|
+
onSignIn={handleSignIn} // Sign in handler
|
|
132
|
+
onSignUp={handleSignUp} // Sign up handler (optional)
|
|
133
|
+
onForgotPassword={handleForgotPassword} // Forgot password handler (optional)
|
|
134
|
+
passwordMinimumLength={8} // Password validation (optional)
|
|
135
|
+
logo={<MyLogo />} // Custom logo (optional)
|
|
136
|
+
layout={{
|
|
137
|
+
// Layout configuration (optional)
|
|
138
|
+
fullScreen: true,
|
|
139
|
+
sideContent: <BrandingContent />,
|
|
140
|
+
sideContentPosition: 'left',
|
|
141
|
+
}}
|
|
142
|
+
/>
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### useAuthScreen Hook
|
|
146
|
+
|
|
147
|
+
Manages authentication screen state and transitions.
|
|
148
|
+
|
|
149
|
+
```tsx
|
|
150
|
+
const { screen, setScreen } = useAuthScreen({
|
|
151
|
+
value: 'signIn', // Initial screen (optional, defaults to 'signIn')
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## TypeScript Types
|
|
156
|
+
|
|
157
|
+
Key type definitions for implementing authentication handlers:
|
|
158
|
+
|
|
159
|
+
```tsx
|
|
160
|
+
type AuthUser = {
|
|
161
|
+
id: string;
|
|
162
|
+
email: string;
|
|
163
|
+
emailVerified?: boolean;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
type AuthTokens = {
|
|
167
|
+
accessToken: string;
|
|
168
|
+
refreshToken?: string;
|
|
169
|
+
idToken?: string;
|
|
170
|
+
expiresIn?: number;
|
|
171
|
+
expiresAt?: number;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
type OnSignIn = (params: { email: string; password: string }) => Promise<void>;
|
|
175
|
+
type OnSignUp = (params: { email: string; password: string }) => Promise<void>;
|
|
176
|
+
type OnForgotPassword = (params: { email: string }) => Promise<void>;
|
|
177
|
+
```
|