@ttoss/react-auth 2.1.0 → 2.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.
@@ -1,158 +0,0 @@
1
- import * as React from 'react';
2
- import { AuthCard } from './AuthCard';
3
- import { Button } from '@ttoss/ui';
4
- import {
5
- Form,
6
- FormFieldInput,
7
- FormFieldPassword,
8
- useForm,
9
- yup,
10
- yupResolver,
11
- } from '@ttoss/forms';
12
- import { NotificationsBox } from '@ttoss/react-notifications';
13
- import { PASSWORD_MINIMUM_LENGTH } from '@ttoss/cloud-auth';
14
- import { useI18n } from '@ttoss/react-i18n';
15
- import type { OnForgotPasswordResetPassword } from './types';
16
-
17
- export type AuthForgotPasswordResetPasswordProps = {
18
- email: string;
19
- onForgotPasswordResetPassword: OnForgotPasswordResetPassword;
20
- onCancel: () => void;
21
- };
22
-
23
- export const AuthForgotPasswordResetPassword = ({
24
- email,
25
- onForgotPasswordResetPassword,
26
- onCancel,
27
- }: AuthForgotPasswordResetPasswordProps) => {
28
- const { intl } = useI18n();
29
-
30
- const schema = React.useMemo(() => {
31
- return yup
32
- .object()
33
- .shape({
34
- code: yup
35
- .string()
36
- .required(
37
- intl.formatMessage({
38
- description: 'Required field.',
39
- defaultMessage: 'Required field',
40
- })
41
- )
42
- .max(
43
- 6,
44
- intl.formatMessage(
45
- {
46
- description: 'Minimum {value} characters.',
47
- defaultMessage: 'Minimum {value} characters',
48
- },
49
- { value: 6 }
50
- )
51
- ),
52
- password: yup
53
- .string()
54
- .required(
55
- intl.formatMessage({
56
- description: 'Password is required.',
57
- defaultMessage: 'Password field is required',
58
- })
59
- )
60
- .min(
61
- PASSWORD_MINIMUM_LENGTH,
62
- intl.formatMessage(
63
- {
64
- description:
65
- 'Password must be at least {value} characters long.',
66
- defaultMessage: 'Password requires {value} characters',
67
- },
68
- { value: PASSWORD_MINIMUM_LENGTH }
69
- )
70
- )
71
- .trim(),
72
- confirmPassword: yup
73
- .string()
74
- .required(
75
- intl.formatMessage({
76
- description: 'Confirm Password is required.',
77
- defaultMessage: 'Confirm password field is required',
78
- })
79
- )
80
- .oneOf(
81
- [yup.ref('password')],
82
- intl.formatMessage({
83
- description: 'Passwords are not the same',
84
- defaultMessage: 'Passwords are not the same',
85
- })
86
- ),
87
- })
88
- .required();
89
- }, [intl]);
90
-
91
- const formMethods = useForm<yup.InferType<typeof schema>>({
92
- resolver: yupResolver(schema),
93
- mode: 'onBlur',
94
- });
95
-
96
- return (
97
- <Form
98
- {...formMethods}
99
- sx={{
100
- maxWidth: '390px',
101
- }}
102
- onSubmit={({ code, password }) => {
103
- return onForgotPasswordResetPassword({
104
- email,
105
- code,
106
- newPassword: password,
107
- });
108
- }}
109
- >
110
- <AuthCard
111
- buttonLabel={intl.formatMessage({
112
- description: 'Recover Password',
113
- defaultMessage: 'Recover Password',
114
- })}
115
- isValidForm={formMethods.formState.isValid}
116
- title={intl.formatMessage({
117
- description: 'Recovering Password',
118
- defaultMessage: 'Recovering Password',
119
- })}
120
- extraButton={
121
- <Button
122
- sx={{ textAlign: 'center', display: 'initial' }}
123
- variant="secondary"
124
- onClick={onCancel}
125
- >
126
- {intl.formatMessage({
127
- description: 'Cancel',
128
- defaultMessage: 'Cancel',
129
- })}
130
- </Button>
131
- }
132
- >
133
- <FormFieldInput
134
- name="code"
135
- label={intl.formatMessage({
136
- description: 'Code',
137
- defaultMessage: 'Code',
138
- })}
139
- />
140
- <FormFieldPassword
141
- name="password"
142
- label={intl.formatMessage({
143
- description: 'Password label.',
144
- defaultMessage: 'Password',
145
- })}
146
- />
147
- <FormFieldPassword
148
- name="confirmPassword"
149
- label={intl.formatMessage({
150
- description: 'Confirm Password label.',
151
- defaultMessage: 'Confirm password',
152
- })}
153
- />
154
- <NotificationsBox />
155
- </AuthCard>
156
- </Form>
157
- );
158
- };
@@ -1,25 +0,0 @@
1
- import { Flex } from '@ttoss/ui';
2
-
3
- export type AuthFullScreenProps = {
4
- backgroundImageUrl?: string;
5
- children: React.ReactNode;
6
- };
7
-
8
- export const AuthFullScreen = ({ children }: AuthFullScreenProps) => {
9
- return (
10
- <Flex
11
- sx={{
12
- height: '100vh',
13
- width: '100vw',
14
- justifyContent: 'center',
15
- alignItems: 'center',
16
- margin: 0,
17
- backgroundPosition: 'center',
18
- backgroundRepeat: 'no-repeat',
19
- backgroundSize: 'cover',
20
- }}
21
- >
22
- {children}
23
- </Flex>
24
- );
25
- };
@@ -1,119 +0,0 @@
1
- import * as React from 'react';
2
- import { Hub } from 'aws-amplify/utils';
3
- import {
4
- signOut as awsSignOut,
5
- fetchAuthSession,
6
- fetchUserAttributes,
7
- getCurrentUser,
8
- } from 'aws-amplify/auth';
9
-
10
- type User = {
11
- id: string;
12
- email: string;
13
- emailVerified: string;
14
- } | null;
15
-
16
- type Tokens = {
17
- idToken: string;
18
- accessToken: string;
19
- refreshToken: string;
20
- } | null;
21
-
22
- const signOut = () => {
23
- return awsSignOut();
24
- };
25
-
26
- const AuthContext = React.createContext<{
27
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
- signOut: () => Promise<any>;
29
- isAuthenticated: boolean;
30
- user: User;
31
- tokens: Tokens;
32
- }>({
33
- signOut,
34
- isAuthenticated: false,
35
- user: null,
36
- tokens: null,
37
- });
38
-
39
- export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
40
- const [{ user, tokens, isAuthenticated }, setAuthState] = React.useState<{
41
- user: User;
42
- tokens: Tokens;
43
- isAuthenticated: boolean | undefined;
44
- }>({
45
- user: null,
46
- tokens: null,
47
- isAuthenticated: undefined,
48
- });
49
-
50
- React.useEffect(() => {
51
- const updateUser = () => {
52
- getCurrentUser()
53
- .then(async ({ userId }) => {
54
- const [session, user] = await Promise.all([
55
- fetchAuthSession(),
56
- fetchUserAttributes(),
57
- ]);
58
-
59
- const idToken = session.tokens?.idToken?.toString() ?? '';
60
- const accessToken = session.tokens?.accessToken.toString() ?? '';
61
-
62
- setAuthState({
63
- user: {
64
- id: userId,
65
- email: user.email ?? '',
66
- emailVerified: user.email_verified ?? '',
67
- },
68
- tokens: {
69
- idToken,
70
- accessToken,
71
- refreshToken: '',
72
- },
73
- isAuthenticated: true,
74
- });
75
- })
76
- .catch((e) => {
77
- // eslint-disable-next-line no-console
78
- console.error(e);
79
- setAuthState({
80
- user: null,
81
- tokens: null,
82
- isAuthenticated: false,
83
- });
84
- });
85
- };
86
-
87
- const updateUserListener = Hub.listen('auth', updateUser);
88
-
89
- /**
90
- * Check manually the first time.
91
- */
92
- updateUser();
93
-
94
- return () => {
95
- updateUserListener();
96
- };
97
- }, []);
98
-
99
- if (isAuthenticated === undefined) {
100
- return null;
101
- }
102
-
103
- return (
104
- <AuthContext.Provider
105
- value={{
106
- signOut,
107
- isAuthenticated,
108
- user,
109
- tokens,
110
- }}
111
- >
112
- {children}
113
- </AuthContext.Provider>
114
- );
115
- };
116
-
117
- export const useAuth = () => {
118
- return React.useContext(AuthContext);
119
- };
@@ -1,159 +0,0 @@
1
- import * as React from 'react';
2
- import { AuthCard } from './AuthCard';
3
- import { Button, Flex, Link, Text } from '@ttoss/ui';
4
- import {
5
- Form,
6
- FormFieldInput,
7
- FormFieldPassword,
8
- useForm,
9
- yup,
10
- yupResolver,
11
- } from '@ttoss/forms';
12
- import { NotificationsBox, useNotifications } from '@ttoss/react-notifications';
13
- import { PASSWORD_MINIMUM_LENGTH } from '@ttoss/cloud-auth';
14
- import { useI18n } from '@ttoss/react-i18n';
15
- import type { OnSignIn, OnSignInInput } from './types';
16
-
17
- export type AuthSignInProps = {
18
- onSignIn: OnSignIn;
19
- onSignUp: () => void;
20
- onForgotPassword: () => void;
21
- defaultValues?: Partial<OnSignInInput>;
22
- urlLogo?: string;
23
- };
24
-
25
- export const AuthSignIn = ({
26
- onSignIn,
27
- onSignUp,
28
- defaultValues,
29
- onForgotPassword,
30
- }: AuthSignInProps) => {
31
- const { intl } = useI18n();
32
- const { setNotifications } = useNotifications();
33
-
34
- React.useEffect(() => {
35
- setNotifications(undefined);
36
- }, [setNotifications]);
37
-
38
- const schema = yup.object().shape({
39
- email: yup
40
- .string()
41
- .required(
42
- intl.formatMessage({
43
- description: 'Email is a required field.',
44
- defaultMessage: 'Enter your email address',
45
- })
46
- )
47
- .email(
48
- intl.formatMessage({
49
- description: 'Invalid email.',
50
- defaultMessage: 'Please, insert a valid e-mail',
51
- })
52
- ),
53
- password: yup
54
- .string()
55
- .required(
56
- intl.formatMessage({
57
- description: 'Password is required.',
58
- defaultMessage: 'Password field is required',
59
- })
60
- )
61
- .min(
62
- PASSWORD_MINIMUM_LENGTH,
63
- intl.formatMessage(
64
- {
65
- description: 'Password must be at least {value} characters long.',
66
- defaultMessage: 'Password requires {value} characters',
67
- },
68
- { value: PASSWORD_MINIMUM_LENGTH }
69
- )
70
- )
71
- .trim(),
72
- // remember: yup.boolean(),
73
- });
74
-
75
- const formMethods = useForm<OnSignInInput>({
76
- defaultValues,
77
- mode: 'onChange',
78
- resolver: yupResolver(schema),
79
- });
80
-
81
- const onSubmitForm = (data: OnSignInInput) => {
82
- return onSignIn(data);
83
- };
84
-
85
- return (
86
- <Form
87
- sx={{ maxWidth: '390px', width: '100%' }}
88
- {...formMethods}
89
- onSubmit={onSubmitForm}
90
- >
91
- <AuthCard
92
- title={intl.formatMessage({
93
- description: 'Sign in title.',
94
- defaultMessage: 'Log in',
95
- })}
96
- buttonLabel={intl.formatMessage({
97
- description: 'Button label.',
98
- defaultMessage: 'Log in',
99
- })}
100
- isValidForm={formMethods.formState.isValid}
101
- extraButton={
102
- <Button
103
- type="button"
104
- variant="secondary"
105
- sx={{ textAlign: 'center', display: 'initial' }}
106
- onClick={onSignUp}
107
- aria-label="sign-up"
108
- >
109
- {intl.formatMessage({
110
- description: 'Sign up',
111
- defaultMessage: 'Sign up',
112
- })}
113
- </Button>
114
- }
115
- >
116
- <Flex sx={{ flexDirection: 'column', gap: 'xl' }}>
117
- <FormFieldInput
118
- name="email"
119
- label={intl.formatMessage({
120
- description: 'Email label.',
121
- defaultMessage: 'Email',
122
- })}
123
- />
124
- <FormFieldPassword
125
- name="password"
126
- label={intl.formatMessage({
127
- description: 'Password label.',
128
- defaultMessage: 'Password',
129
- })}
130
- />
131
- </Flex>
132
-
133
- <Flex sx={{ justifyContent: 'space-between', marginTop: 'lg' }}>
134
- {/* TODO: temporally commented */}
135
- {/* <FormFieldCheckbox
136
- name="remember"
137
- label={intl.formatMessage({
138
- description: 'Remember',
139
- defaultMessage: 'Remember',
140
- })}
141
- /> */}
142
-
143
- <Text
144
- sx={{ marginLeft: 'auto', cursor: 'pointer' }}
145
- as={Link}
146
- onClick={onForgotPassword}
147
- >
148
- {intl.formatMessage({
149
- description: 'Forgot password?',
150
- defaultMessage: 'Forgot password?',
151
- })}
152
- </Text>
153
- </Flex>
154
-
155
- <NotificationsBox />
156
- </AuthCard>
157
- </Form>
158
- );
159
- };
@@ -1,143 +0,0 @@
1
- import * as React from 'react';
2
- import { AuthCard } from './AuthCard';
3
- import { Flex, Link, Text } from '@ttoss/ui';
4
- import {
5
- Form,
6
- FormFieldInput,
7
- FormFieldPassword,
8
- useForm,
9
- yup,
10
- yupResolver,
11
- } from '@ttoss/forms';
12
- import { NotificationsBox, useNotifications } from '@ttoss/react-notifications';
13
- import { PASSWORD_MINIMUM_LENGTH } from '@ttoss/cloud-auth';
14
- import { useI18n } from '@ttoss/react-i18n';
15
- import type { OnSignUp, OnSignUpInput } from './types';
16
-
17
- export type AuthSignUpProps = {
18
- onSignUp: OnSignUp;
19
- onReturnToSignIn: () => void;
20
- };
21
-
22
- export const AuthSignUp = ({ onSignUp, onReturnToSignIn }: AuthSignUpProps) => {
23
- const { intl } = useI18n();
24
- const { setNotifications } = useNotifications();
25
-
26
- React.useEffect(() => {
27
- setNotifications(undefined);
28
- }, [setNotifications]);
29
-
30
- const schema = yup.object().shape({
31
- email: yup
32
- .string()
33
- .required(
34
- intl.formatMessage({
35
- description: 'Email is a required field.',
36
- defaultMessage: 'Enter your email address',
37
- })
38
- )
39
- .email(
40
- intl.formatMessage({
41
- description: 'Invalid email.',
42
- defaultMessage: 'Invalid email',
43
- })
44
- ),
45
- password: yup
46
- .string()
47
- .required(
48
- intl.formatMessage({
49
- description: 'Password is required.',
50
- defaultMessage: 'Password field is required',
51
- })
52
- )
53
- .min(
54
- PASSWORD_MINIMUM_LENGTH,
55
- intl.formatMessage(
56
- {
57
- description: 'Password must be at least {value} characters long.',
58
- defaultMessage: 'Password requires {value} characters',
59
- },
60
- { value: PASSWORD_MINIMUM_LENGTH }
61
- )
62
- )
63
- .trim(),
64
- confirmPassword: yup
65
- .string()
66
- .required(
67
- intl.formatMessage({
68
- description: 'Confirm Password is required.',
69
- defaultMessage: 'Confirm password field is required',
70
- })
71
- )
72
- .oneOf(
73
- [yup.ref('password')],
74
- intl.formatMessage({
75
- description: 'Passwords are not the same',
76
- defaultMessage: 'Passwords are not the same',
77
- })
78
- ),
79
- });
80
-
81
- const formMethods = useForm<OnSignUpInput>({
82
- mode: 'all',
83
- resolver: yupResolver(schema),
84
- });
85
-
86
- const onSubmitForm = (data: OnSignUpInput) => {
87
- return onSignUp(data);
88
- };
89
-
90
- return (
91
- <Form
92
- sx={{ maxWidth: '390px', width: '100%' }}
93
- {...formMethods}
94
- onSubmit={onSubmitForm}
95
- >
96
- <AuthCard
97
- buttonLabel={intl.formatMessage({
98
- description: 'Create account.',
99
- defaultMessage: 'Sign up',
100
- })}
101
- title={intl.formatMessage({
102
- description: 'Title on sign up.',
103
- defaultMessage: 'Sign up',
104
- })}
105
- isValidForm={formMethods.formState.isValid}
106
- extraButton={
107
- <Text sx={{ cursor: 'pointer' }} onClick={onReturnToSignIn} as={Link}>
108
- {intl.formatMessage({
109
- description: 'Link to sign in on sign up.',
110
- defaultMessage: "I'm already registered",
111
- })}
112
- </Text>
113
- }
114
- >
115
- <Flex sx={{ flexDirection: 'column', gap: 'xl' }}>
116
- <FormFieldInput
117
- name="email"
118
- label={intl.formatMessage({
119
- description: 'Email label.',
120
- defaultMessage: 'Email',
121
- })}
122
- />
123
- <FormFieldPassword
124
- name="password"
125
- label={intl.formatMessage({
126
- description: 'Password label.',
127
- defaultMessage: 'Password',
128
- })}
129
- />
130
- <FormFieldPassword
131
- name="confirmPassword"
132
- label={intl.formatMessage({
133
- description: 'Confirm Password label.',
134
- defaultMessage: 'Confirm password',
135
- })}
136
- />
137
- </Flex>
138
-
139
- <NotificationsBox />
140
- </AuthCard>
141
- </Form>
142
- );
143
- };
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export { AuthProvider, useAuth } from './AuthProvider';
2
- export { Auth } from './Auth';
3
- export * from './types';
package/src/types.ts DELETED
@@ -1,24 +0,0 @@
1
- export type OnSignInInput = {
2
- email: string;
3
- password: string;
4
- };
5
-
6
- export type OnSignIn = (input: OnSignInInput) => void;
7
-
8
- export type OnSignUpInput = {
9
- email: string;
10
- password: string;
11
- confirmPassword: string;
12
- };
13
-
14
- export type OnSignUp = (input: OnSignUpInput) => void;
15
-
16
- export type OnConfirmSignUp = (input: { email: string; code: string }) => void;
17
-
18
- export type OnForgotPassword = (input: { email: string }) => void;
19
-
20
- export type OnForgotPasswordResetPassword = (input: {
21
- email: string;
22
- code: string;
23
- newPassword: string;
24
- }) => void;