@ttoss/react-auth 2.6.30 → 2.6.32
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 +100 -131
- package/dist/esm/index.js +97 -1090
- package/dist/index.d.ts +11 -101
- package/i18n/compiled/en.json +36 -10
- package/i18n/lang/en.json +60 -13
- package/package.json +10 -9
package/README.md
CHANGED
|
@@ -1,178 +1,147 @@
|
|
|
1
1
|
# @ttoss/react-auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
AWS Cognito authentication module for React applications using AWS Amplify, built on top of `@ttoss/react-auth-core` for provider-agnostic authentication patterns.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
```shell
|
|
8
|
+
pnpm add @ttoss/react-auth @ttoss/react-notifications aws-amplify
|
|
9
|
+
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Core Concepts
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
This package provides AWS Cognito-specific implementations of the authentication patterns defined in `@ttoss/react-auth-core`. It automatically handles Amplify configuration, auth state management, and integrates with ttoss notification system.
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
**Key Features:**
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
- AWS Cognito authentication with Amplify
|
|
18
|
+
- Automatic auth state synchronization
|
|
19
|
+
- Built-in error handling and notifications
|
|
20
|
+
- TypeScript support with full type safety
|
|
21
|
+
- ESM-only package
|
|
20
22
|
|
|
21
|
-
##
|
|
23
|
+
## Quick Start
|
|
22
24
|
|
|
23
|
-
### Amplify
|
|
25
|
+
### 1. Configure AWS Amplify
|
|
24
26
|
|
|
25
27
|
```ts
|
|
26
28
|
import { Amplify, type ResourcesConfig } from 'aws-amplify';
|
|
27
|
-
import {
|
|
28
|
-
CookieStorage,
|
|
29
|
-
KeyValueStorageInterface,
|
|
30
|
-
defaultStorage,
|
|
31
|
-
sessionStorage,
|
|
32
|
-
} from 'aws-amplify/utils';
|
|
33
|
-
import { cognitoUserPoolsTokenProvider } from 'aws-amplify/auth/cognito';
|
|
34
29
|
|
|
30
|
+
/**
|
|
31
|
+
* https://docs.amplify.aws/gen1/react/build-a-backend/auth/set-up-auth/
|
|
32
|
+
*/
|
|
35
33
|
const authConfig: ResourcesConfig['Auth'] = {
|
|
36
34
|
Cognito: {
|
|
37
|
-
//
|
|
38
|
-
userPoolClientId: 'a1b2c3d4e5f6g7h8i9j0k1l2m3',
|
|
39
|
-
|
|
40
|
-
// OPTIONAL - Amazon Cognito User Pool ID
|
|
41
|
-
userPoolId: 'XX-XXXX-X_abcd1234',
|
|
42
|
-
|
|
43
|
-
loginWith: {
|
|
44
|
-
// OPTIONAL - Hosted UI configuration
|
|
45
|
-
oauth: {
|
|
46
|
-
domain: 'your_cognito_domain',
|
|
47
|
-
scopes: [
|
|
48
|
-
'phone',
|
|
49
|
-
'email',
|
|
50
|
-
'profile',
|
|
51
|
-
'openid',
|
|
52
|
-
'aws.cognito.signin.user.admin',
|
|
53
|
-
],
|
|
54
|
-
redirectSignIn: ['http://localhost:3000/'],
|
|
55
|
-
redirectSignOut: ['http://localhost:3000/'],
|
|
56
|
-
responseType: 'code', // or 'token', note that REFRESH token will only be generated when the responseType is code
|
|
57
|
-
},
|
|
58
|
-
},
|
|
35
|
+
// ... your Cognito config
|
|
59
36
|
},
|
|
60
37
|
};
|
|
61
38
|
|
|
62
|
-
Amplify.configure({
|
|
63
|
-
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// Browser Local Storage
|
|
67
|
-
// In Amplify the localStorage is the default storage mechanism. It saves the tokens in the browser's localStorage.
|
|
68
|
-
// This local storage will persist across browser sessions and tabs. You can explicitly set to this storage by calling:
|
|
69
|
-
cognitoUserPoolsTokenProvider.setKeyValueStorage(defaultStorage);
|
|
70
|
-
|
|
71
|
-
// Cookie Storage
|
|
72
|
-
// CookieStorage saves the tokens in the browser's Cookies. The cookies will persist across browser sessions and tabs.
|
|
73
|
-
// You can explicitly set to this storage by calling:
|
|
74
|
-
cognitoUserPoolsTokenProvider.setKeyValueStorage(
|
|
75
|
-
new CookieStorage(
|
|
76
|
-
// OPTIONAL - Configuration for cookie storage
|
|
77
|
-
{
|
|
78
|
-
// REQUIRED - Cookie domain (only required if cookieStorage is provided)
|
|
79
|
-
domain: '.yourdomain.com',
|
|
80
|
-
// OPTIONAL - Cookie path
|
|
81
|
-
path: '/',
|
|
82
|
-
// OPTIONAL - Cookie expiration in days
|
|
83
|
-
expires: 365,
|
|
84
|
-
// OPTIONAL - See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
|
|
85
|
-
sameSite: 'strict' | 'lax' | 'none',
|
|
86
|
-
// OPTIONAL - Cookie secure flag
|
|
87
|
-
// Either true or false, indicating if the cookie transmission requires a secure protocol (https).
|
|
88
|
-
secure: true,
|
|
89
|
-
}
|
|
90
|
-
)
|
|
91
|
-
);
|
|
39
|
+
Amplify.configure({ Auth: authConfig });
|
|
40
|
+
```
|
|
92
41
|
|
|
93
|
-
|
|
94
|
-
// sessionStorage saves the tokens in the browser's sessionStorage and these tokens will clear when a tab is closed.
|
|
95
|
-
// The benefit to this storage mechanism is that the session only lasts as long as the browser is open and you
|
|
96
|
-
// can sign out users when they close the tab. You can update to this storage by calling:
|
|
97
|
-
cognitoUserPoolsTokenProvider.setKeyValueStorage(sessionStorage);
|
|
42
|
+
### 2. Setup Authentication Provider
|
|
98
43
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
44
|
+
```tsx
|
|
45
|
+
import { AuthProvider } from '@ttoss/react-auth';
|
|
46
|
+
import { NotificationsProvider } from '@ttoss/react-notifications';
|
|
102
47
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
async removeItem(key: string): Promise<void> {
|
|
112
|
-
delete this.storageObject[key];
|
|
113
|
-
}
|
|
114
|
-
async clear(): Promise<void> {
|
|
115
|
-
this.storageObject = {};
|
|
116
|
-
}
|
|
48
|
+
function App() {
|
|
49
|
+
return (
|
|
50
|
+
<NotificationsProvider>
|
|
51
|
+
<AuthProvider>
|
|
52
|
+
<YourApp />
|
|
53
|
+
</AuthProvider>
|
|
54
|
+
</NotificationsProvider>
|
|
55
|
+
);
|
|
117
56
|
}
|
|
118
|
-
|
|
119
|
-
cognitoUserPoolsTokenProvider.setKeyValueStorage(new MyCustomStorage());
|
|
120
57
|
```
|
|
121
58
|
|
|
122
|
-
###
|
|
59
|
+
### 3. Use Authentication in Components
|
|
123
60
|
|
|
124
61
|
```tsx
|
|
125
|
-
import { useAuth } from '@ttoss/react-auth';
|
|
62
|
+
import { Auth, useAuth } from '@ttoss/react-auth';
|
|
63
|
+
import { Navigate } from 'react-router-dom';
|
|
64
|
+
|
|
65
|
+
// Authentication form component
|
|
66
|
+
function LoginPage() {
|
|
67
|
+
return <Auth />;
|
|
68
|
+
}
|
|
126
69
|
|
|
127
|
-
|
|
70
|
+
// Protected route component
|
|
71
|
+
function PrivateRoute({ children }: { children: React.ReactNode }) {
|
|
128
72
|
const { isAuthenticated } = useAuth();
|
|
129
73
|
|
|
130
74
|
if (!isAuthenticated) {
|
|
131
|
-
return <Navigate to="/login"
|
|
75
|
+
return <Navigate to="/login" />;
|
|
132
76
|
}
|
|
133
|
-
|
|
134
|
-
}
|
|
77
|
+
|
|
78
|
+
return <>{children}</>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Using auth state
|
|
82
|
+
function UserProfile() {
|
|
83
|
+
const { user, signOut } = useAuth();
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<div>
|
|
87
|
+
<h1>Welcome, {user?.email}</h1>
|
|
88
|
+
<button onClick={signOut}>Sign Out</button>
|
|
89
|
+
</div>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
135
92
|
```
|
|
136
93
|
|
|
137
|
-
|
|
94
|
+
## API Reference
|
|
138
95
|
|
|
139
|
-
|
|
140
|
-
import { Auth, useAuth } from '@ttoss/react-auth';
|
|
96
|
+
### `useAuth()`
|
|
141
97
|
|
|
142
|
-
|
|
143
|
-
const auth = useAuth();
|
|
98
|
+
Returns authentication state and methods:
|
|
144
99
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
100
|
+
```tsx
|
|
101
|
+
const {
|
|
102
|
+
user, // Current user data or null
|
|
103
|
+
isAuthenticated, // Boolean authentication status
|
|
104
|
+
signOut, // Function to sign out user
|
|
105
|
+
} = useAuth();
|
|
106
|
+
```
|
|
148
107
|
|
|
149
|
-
|
|
150
|
-
<div>
|
|
151
|
-
<h1>Login Page</h1>
|
|
108
|
+
### `getAuthData(options?)`
|
|
152
109
|
|
|
153
|
-
|
|
110
|
+
Retrieve current authentication data programmatically:
|
|
154
111
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
};
|
|
159
|
-
export default Login;
|
|
112
|
+
```tsx
|
|
113
|
+
import { getAuthData } from '@ttoss/react-auth';
|
|
114
|
+
|
|
115
|
+
const authData = await getAuthData({ includeTokens: true });
|
|
160
116
|
```
|
|
161
117
|
|
|
162
|
-
|
|
118
|
+
### `checkAuth()`
|
|
119
|
+
|
|
120
|
+
Check if user is currently authenticated:
|
|
163
121
|
|
|
164
122
|
```tsx
|
|
165
|
-
import {
|
|
166
|
-
import { NotificationsProvider } from '@ttoss/react-notifications';
|
|
123
|
+
import { checkAuth } from '@ttoss/react-auth';
|
|
167
124
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
125
|
+
const isAuthenticated = await checkAuth();
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Storage Configuration
|
|
129
|
+
|
|
130
|
+
Configure token storage mechanism using Amplify's storage options:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
import { cognitoUserPoolsTokenProvider } from 'aws-amplify/auth/cognito';
|
|
134
|
+
import { CookieStorage, sessionStorage } from 'aws-amplify/utils';
|
|
135
|
+
|
|
136
|
+
// Cookie storage (recommended for production)
|
|
137
|
+
cognitoUserPoolsTokenProvider.setKeyValueStorage(
|
|
138
|
+
new CookieStorage({
|
|
139
|
+
domain: '.yourdomain.com',
|
|
140
|
+
secure: true,
|
|
141
|
+
sameSite: 'strict',
|
|
142
|
+
})
|
|
177
143
|
);
|
|
144
|
+
|
|
145
|
+
// Session storage (clears on tab close)
|
|
146
|
+
cognitoUserPoolsTokenProvider.setKeyValueStorage(sessionStorage);
|
|
178
147
|
```
|