@ttoss/react-auth-core 0.4.0 → 0.4.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 +24 -0
- package/dist/esm/index.js +79 -47
- package/dist/index.d.cts +19 -8
- package/dist/index.d.ts +19 -8
- package/dist/index.js +79 -47
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -124,6 +124,8 @@ const {
|
|
|
124
124
|
|
|
125
125
|
Main authentication flow component.
|
|
126
126
|
|
|
127
|
+
#### With External Screen State Management
|
|
128
|
+
|
|
127
129
|
```tsx
|
|
128
130
|
<Auth
|
|
129
131
|
screen={screen} // Current screen state
|
|
@@ -142,6 +144,28 @@ Main authentication flow component.
|
|
|
142
144
|
/>
|
|
143
145
|
```
|
|
144
146
|
|
|
147
|
+
#### With Internal Screen State Management
|
|
148
|
+
|
|
149
|
+
You can also let the Auth component manage its own screen state by using the `initialScreen` prop instead of `screen`/`setScreen`:
|
|
150
|
+
|
|
151
|
+
```tsx
|
|
152
|
+
<Auth
|
|
153
|
+
initialScreen={{ value: 'signUp' }} // Start on sign up screen (optional)
|
|
154
|
+
onSignIn={handleSignIn}
|
|
155
|
+
onSignUp={handleSignUp}
|
|
156
|
+
onForgotPassword={handleForgotPassword}
|
|
157
|
+
/>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
This is useful when you don't need to control the screen state externally. The `initialScreen` prop accepts any valid `AuthScreen` value:
|
|
161
|
+
|
|
162
|
+
- `{ value: 'signIn' }` - Sign in screen (default)
|
|
163
|
+
- `{ value: 'signUp' }` - Sign up screen
|
|
164
|
+
- `{ value: 'forgotPassword' }` - Forgot password screen
|
|
165
|
+
- `{ value: 'confirmSignUpCheckEmail' }` - Email confirmation reminder
|
|
166
|
+
- `{ value: 'confirmSignUpWithCode', context: { email: string } }` - Code confirmation with email context
|
|
167
|
+
- `{ value: 'confirmResetPassword', context: { email: string } }` - Password reset with email context
|
|
168
|
+
|
|
145
169
|
### useAuthScreen Hook
|
|
146
170
|
|
|
147
171
|
Manages authentication screen state and transitions.
|
package/dist/esm/index.js
CHANGED
|
@@ -9,7 +9,7 @@ var __name = (target, value) => __defProp(target, "name", {
|
|
|
9
9
|
// src/Auth.tsx
|
|
10
10
|
import { useNotifications as useNotifications2 } from "@ttoss/react-notifications";
|
|
11
11
|
import { Flex as Flex5 } from "@ttoss/ui";
|
|
12
|
-
import * as
|
|
12
|
+
import * as React5 from "react";
|
|
13
13
|
|
|
14
14
|
// src/AuthCard.tsx
|
|
15
15
|
import { useNotifications } from "@ttoss/react-notifications";
|
|
@@ -310,14 +310,29 @@ var AuthForgotPasswordResetPassword = /* @__PURE__ */__name(props => {
|
|
|
310
310
|
const {
|
|
311
311
|
intl
|
|
312
312
|
} = useI18n4();
|
|
313
|
+
const codeValidation = yup3.string().required(intl.formatMessage({
|
|
314
|
+
id: "0XOzcH",
|
|
315
|
+
defaultMessage: [{
|
|
316
|
+
"type": 0,
|
|
317
|
+
"value": "Required field"
|
|
318
|
+
}]
|
|
319
|
+
}));
|
|
313
320
|
const schema = yup3.object().shape({
|
|
314
|
-
code:
|
|
315
|
-
id: "
|
|
321
|
+
code: props.maxCodeLength ? codeValidation.min(props.maxCodeLength, intl.formatMessage({
|
|
322
|
+
id: "S3pjKw",
|
|
316
323
|
defaultMessage: [{
|
|
317
324
|
"type": 0,
|
|
318
|
-
"value": "
|
|
325
|
+
"value": "Minimum "
|
|
326
|
+
}, {
|
|
327
|
+
"type": 1,
|
|
328
|
+
"value": "value"
|
|
329
|
+
}, {
|
|
330
|
+
"type": 0,
|
|
331
|
+
"value": " characters"
|
|
319
332
|
}]
|
|
320
|
-
}
|
|
333
|
+
}, {
|
|
334
|
+
value: props.maxCodeLength
|
|
335
|
+
})).max(props.maxCodeLength, intl.formatMessage({
|
|
321
336
|
id: "SfWKyS",
|
|
322
337
|
defaultMessage: [{
|
|
323
338
|
"type": 0,
|
|
@@ -330,8 +345,8 @@ var AuthForgotPasswordResetPassword = /* @__PURE__ */__name(props => {
|
|
|
330
345
|
"value": " characters"
|
|
331
346
|
}]
|
|
332
347
|
}, {
|
|
333
|
-
value:
|
|
334
|
-
})),
|
|
348
|
+
value: props.maxCodeLength
|
|
349
|
+
})) : codeValidation,
|
|
335
350
|
newPassword: yup3.string().required(intl.formatMessage({
|
|
336
351
|
id: "kdFYba",
|
|
337
352
|
defaultMessage: [{
|
|
@@ -812,6 +827,18 @@ var ErrorBoundary = /* @__PURE__ */__name(({
|
|
|
812
827
|
}, children);
|
|
813
828
|
}, "ErrorBoundary");
|
|
814
829
|
|
|
830
|
+
// src/useAuthScreen.ts
|
|
831
|
+
import * as React4 from "react";
|
|
832
|
+
var useAuthScreen = /* @__PURE__ */__name(initialScreen => {
|
|
833
|
+
const [screen, setScreen] = React4.useState(initialScreen || {
|
|
834
|
+
value: "signIn"
|
|
835
|
+
});
|
|
836
|
+
return {
|
|
837
|
+
screen,
|
|
838
|
+
setScreen
|
|
839
|
+
};
|
|
840
|
+
}, "useAuthScreen");
|
|
841
|
+
|
|
815
842
|
// src/Auth.tsx
|
|
816
843
|
var AuthLogic = /* @__PURE__ */__name(props => {
|
|
817
844
|
const {
|
|
@@ -824,16 +851,17 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
824
851
|
onConfirmSignUpWithCode,
|
|
825
852
|
onForgotPasswordResetPassword,
|
|
826
853
|
passwordMinimumLength,
|
|
827
|
-
signUpTerms
|
|
854
|
+
signUpTerms,
|
|
855
|
+
maxForgotPasswordCodeLength
|
|
828
856
|
} = props;
|
|
829
857
|
const {
|
|
830
858
|
clearNotifications,
|
|
831
859
|
setLoading
|
|
832
860
|
} = useNotifications2();
|
|
833
|
-
|
|
861
|
+
React5.useEffect(() => {
|
|
834
862
|
clearNotifications();
|
|
835
863
|
}, [screen.value, clearNotifications]);
|
|
836
|
-
|
|
864
|
+
React5.useEffect(() => {
|
|
837
865
|
return clearNotifications;
|
|
838
866
|
}, [clearNotifications]);
|
|
839
867
|
const onGoToSignIn = /* @__PURE__ */__name(() => {
|
|
@@ -841,7 +869,7 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
841
869
|
value: "signIn"
|
|
842
870
|
});
|
|
843
871
|
}, "onGoToSignIn");
|
|
844
|
-
const onGoToSignUp =
|
|
872
|
+
const onGoToSignUp = React5.useCallback(() => {
|
|
845
873
|
if (!onSignUp) {
|
|
846
874
|
return void 0;
|
|
847
875
|
}
|
|
@@ -849,7 +877,7 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
849
877
|
value: "signUp"
|
|
850
878
|
});
|
|
851
879
|
}, [onSignUp, setScreen]);
|
|
852
|
-
const onGoToForgotPassword =
|
|
880
|
+
const onGoToForgotPassword = React5.useCallback(() => {
|
|
853
881
|
if (!onForgotPassword) {
|
|
854
882
|
return void 0;
|
|
855
883
|
}
|
|
@@ -869,7 +897,7 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
869
897
|
};
|
|
870
898
|
}, "notificationsWrapper");
|
|
871
899
|
if (screen.value === "signIn") {
|
|
872
|
-
return /* @__PURE__ */
|
|
900
|
+
return /* @__PURE__ */React5.createElement(AuthSignIn, {
|
|
873
901
|
onSignIn: notificationsWrapper(onSignIn),
|
|
874
902
|
onGoToSignUp: onSignUp && onGoToSignUp,
|
|
875
903
|
onGoToForgotPassword: onForgotPassword && onGoToForgotPassword,
|
|
@@ -877,7 +905,7 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
877
905
|
});
|
|
878
906
|
}
|
|
879
907
|
if (screen.value === "signUp" && onSignUp) {
|
|
880
|
-
return /* @__PURE__ */
|
|
908
|
+
return /* @__PURE__ */React5.createElement(AuthSignUp, {
|
|
881
909
|
onSignUp: notificationsWrapper(onSignUp),
|
|
882
910
|
passwordMinimumLength,
|
|
883
911
|
onGoToSignIn,
|
|
@@ -885,27 +913,28 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
885
913
|
});
|
|
886
914
|
}
|
|
887
915
|
if (screen.value === "forgotPassword" && onForgotPassword) {
|
|
888
|
-
return /* @__PURE__ */
|
|
916
|
+
return /* @__PURE__ */React5.createElement(AuthForgotPassword, {
|
|
889
917
|
onForgotPassword: notificationsWrapper(onForgotPassword),
|
|
890
918
|
onGoToSignIn,
|
|
891
919
|
onGoToSignUp: onSignUp && onGoToSignUp
|
|
892
920
|
});
|
|
893
921
|
}
|
|
894
922
|
if (screen.value === "confirmResetPassword" && onForgotPasswordResetPassword) {
|
|
895
|
-
return /* @__PURE__ */
|
|
923
|
+
return /* @__PURE__ */React5.createElement(AuthForgotPasswordResetPassword, {
|
|
896
924
|
onForgotPasswordResetPassword: notificationsWrapper(onForgotPasswordResetPassword),
|
|
897
925
|
onGoToSignIn,
|
|
898
|
-
email: screen.context.email
|
|
926
|
+
email: screen.context.email,
|
|
927
|
+
maxCodeLength: maxForgotPasswordCodeLength
|
|
899
928
|
});
|
|
900
929
|
}
|
|
901
930
|
if (screen.value === "confirmSignUpWithCode" && onConfirmSignUpWithCode) {
|
|
902
|
-
return /* @__PURE__ */
|
|
931
|
+
return /* @__PURE__ */React5.createElement(AuthConfirmSignUpWithCode, {
|
|
903
932
|
onConfirmSignUpWithCode: notificationsWrapper(onConfirmSignUpWithCode),
|
|
904
933
|
email: screen.context.email
|
|
905
934
|
});
|
|
906
935
|
}
|
|
907
936
|
if (screen.value === "confirmSignUpCheckEmail" && onConfirmSignUpCheckEmail) {
|
|
908
|
-
return /* @__PURE__ */
|
|
937
|
+
return /* @__PURE__ */React5.createElement(AuthConfirmSignUpCheckEmail, {
|
|
909
938
|
onConfirmSignUpCheckEmail
|
|
910
939
|
});
|
|
911
940
|
}
|
|
@@ -917,20 +946,35 @@ var Auth = /* @__PURE__ */__name(props => {
|
|
|
917
946
|
fullScreen: true
|
|
918
947
|
}
|
|
919
948
|
} = props;
|
|
920
|
-
const
|
|
921
|
-
|
|
949
|
+
const authScreenHook = useAuthScreen(props.initialScreen);
|
|
950
|
+
const screen = props.screen ?? authScreenHook.screen;
|
|
951
|
+
const setScreen = props.setScreen ?? authScreenHook.setScreen;
|
|
952
|
+
const withLogoNode = React5.useMemo(() => {
|
|
953
|
+
return /* @__PURE__ */React5.createElement(LogoProvider, {
|
|
922
954
|
logo: props.logo
|
|
923
|
-
}, /* @__PURE__ */
|
|
924
|
-
|
|
955
|
+
}, /* @__PURE__ */React5.createElement(ErrorBoundary, null, /* @__PURE__ */React5.createElement(AuthLogic, {
|
|
956
|
+
screen,
|
|
957
|
+
setScreen,
|
|
958
|
+
signUpTerms: props.signUpTerms,
|
|
959
|
+
passwordMinimumLength: props.passwordMinimumLength,
|
|
960
|
+
maxForgotPasswordCodeLength: props.maxForgotPasswordCodeLength,
|
|
961
|
+
onSignIn: props.onSignIn,
|
|
962
|
+
onSignUp: props.onSignUp,
|
|
963
|
+
onConfirmSignUpCheckEmail: props.onConfirmSignUpCheckEmail,
|
|
964
|
+
onConfirmSignUpWithCode: props.onConfirmSignUpWithCode,
|
|
965
|
+
onForgotPassword: props.onForgotPassword,
|
|
966
|
+
onForgotPasswordResetPassword: props.onForgotPasswordResetPassword
|
|
967
|
+
})));
|
|
968
|
+
}, [props, screen, setScreen]);
|
|
925
969
|
if (layout.fullScreen) {
|
|
926
970
|
if (layout.sideContentPosition) {
|
|
927
|
-
return /* @__PURE__ */
|
|
971
|
+
return /* @__PURE__ */React5.createElement(AuthFullScreen, null, /* @__PURE__ */React5.createElement(Flex5, {
|
|
928
972
|
sx: {
|
|
929
973
|
width: "100%",
|
|
930
974
|
height: "100%",
|
|
931
975
|
flexDirection: layout.sideContentPosition === "left" ? "row" : "row-reverse"
|
|
932
976
|
}
|
|
933
|
-
}, /* @__PURE__ */
|
|
977
|
+
}, /* @__PURE__ */React5.createElement(Flex5, {
|
|
934
978
|
sx: {
|
|
935
979
|
width: "100%",
|
|
936
980
|
height: "100%",
|
|
@@ -938,7 +982,7 @@ var Auth = /* @__PURE__ */__name(props => {
|
|
|
938
982
|
justifyContent: "center",
|
|
939
983
|
alignItems: "center"
|
|
940
984
|
}
|
|
941
|
-
}, layout.sideContent), /* @__PURE__ */
|
|
985
|
+
}, layout.sideContent), /* @__PURE__ */React5.createElement(Flex5, {
|
|
942
986
|
sx: {
|
|
943
987
|
width: "100%",
|
|
944
988
|
height: "100%",
|
|
@@ -948,15 +992,15 @@ var Auth = /* @__PURE__ */__name(props => {
|
|
|
948
992
|
}
|
|
949
993
|
}, withLogoNode)));
|
|
950
994
|
}
|
|
951
|
-
return /* @__PURE__ */
|
|
995
|
+
return /* @__PURE__ */React5.createElement(AuthFullScreen, null, withLogoNode);
|
|
952
996
|
}
|
|
953
997
|
return withLogoNode;
|
|
954
998
|
}, "Auth");
|
|
955
999
|
|
|
956
1000
|
// src/AuthProvider.tsx
|
|
957
1001
|
import { useNotifications as useNotifications3 } from "@ttoss/react-notifications";
|
|
958
|
-
import * as
|
|
959
|
-
var AuthContext = /* @__PURE__ */
|
|
1002
|
+
import * as React6 from "react";
|
|
1003
|
+
var AuthContext = /* @__PURE__ */React6.createContext(null);
|
|
960
1004
|
var UNAUTHENTICATED_USER = {
|
|
961
1005
|
user: null,
|
|
962
1006
|
tokens: null,
|
|
@@ -970,15 +1014,15 @@ var AuthProvider = /* @__PURE__ */__name(props => {
|
|
|
970
1014
|
const {
|
|
971
1015
|
setLoading
|
|
972
1016
|
} = useNotifications3();
|
|
973
|
-
const [authData, setAuthData] =
|
|
1017
|
+
const [authData, setAuthData] = React6.useState({
|
|
974
1018
|
user: null,
|
|
975
1019
|
tokens: null,
|
|
976
1020
|
isAuthenticated: void 0
|
|
977
1021
|
});
|
|
978
|
-
|
|
1022
|
+
React6.useEffect(() => {
|
|
979
1023
|
setLoading(authData.isAuthenticated === void 0);
|
|
980
1024
|
}, [authData.isAuthenticated, setLoading]);
|
|
981
|
-
|
|
1025
|
+
React6.useEffect(() => {
|
|
982
1026
|
const fetchAuthData = /* @__PURE__ */__name(async () => {
|
|
983
1027
|
try {
|
|
984
1028
|
const data = await getAuthData?.();
|
|
@@ -993,14 +1037,14 @@ var AuthProvider = /* @__PURE__ */__name(props => {
|
|
|
993
1037
|
}, "fetchAuthData");
|
|
994
1038
|
fetchAuthData();
|
|
995
1039
|
}, [getAuthData]);
|
|
996
|
-
const signOut =
|
|
1040
|
+
const signOut = React6.useCallback(async () => {
|
|
997
1041
|
await signOutProp?.();
|
|
998
1042
|
setAuthData(UNAUTHENTICATED_USER);
|
|
999
1043
|
}, [signOutProp]);
|
|
1000
1044
|
if (authData.isAuthenticated === void 0) {
|
|
1001
1045
|
return null;
|
|
1002
1046
|
}
|
|
1003
|
-
return /* @__PURE__ */
|
|
1047
|
+
return /* @__PURE__ */React6.createElement(AuthContext.Provider, {
|
|
1004
1048
|
value: {
|
|
1005
1049
|
signOut,
|
|
1006
1050
|
isAuthenticated: authData.isAuthenticated ?? false,
|
|
@@ -1011,22 +1055,10 @@ var AuthProvider = /* @__PURE__ */__name(props => {
|
|
|
1011
1055
|
}, props.children);
|
|
1012
1056
|
}, "AuthProvider");
|
|
1013
1057
|
var useAuth = /* @__PURE__ */__name(() => {
|
|
1014
|
-
const context =
|
|
1058
|
+
const context = React6.useContext(AuthContext);
|
|
1015
1059
|
if (!context) {
|
|
1016
1060
|
throw new Error("useAuth must be used within an AuthProvider");
|
|
1017
1061
|
}
|
|
1018
1062
|
return context;
|
|
1019
1063
|
}, "useAuth");
|
|
1020
|
-
|
|
1021
|
-
// src/useAuthScreen.ts
|
|
1022
|
-
import * as React6 from "react";
|
|
1023
|
-
var useAuthScreen = /* @__PURE__ */__name(initialScreen => {
|
|
1024
|
-
const [screen, setScreen] = React6.useState(initialScreen || {
|
|
1025
|
-
value: "signIn"
|
|
1026
|
-
});
|
|
1027
|
-
return {
|
|
1028
|
-
screen,
|
|
1029
|
-
setScreen
|
|
1030
|
-
};
|
|
1031
|
-
}, "useAuthScreen");
|
|
1032
1064
|
export { Auth, AuthConfirmSignUpCheckEmail, AuthConfirmSignUpWithCode, AuthForgotPassword, AuthForgotPasswordResetPassword, AuthFullScreen, AuthProvider, AuthSignIn, AuthSignUp, ErrorBoundary, useAuth, useAuthScreen };
|
package/dist/index.d.cts
CHANGED
|
@@ -46,7 +46,8 @@ type AuthScreen = {
|
|
|
46
46
|
} | {
|
|
47
47
|
value: 'confirmResetPassword';
|
|
48
48
|
context: {
|
|
49
|
-
email
|
|
49
|
+
email?: string;
|
|
50
|
+
code?: string;
|
|
50
51
|
};
|
|
51
52
|
};
|
|
52
53
|
type OnSignInInput = {
|
|
@@ -67,8 +68,8 @@ type OnForgotPasswordInput = {
|
|
|
67
68
|
email: string;
|
|
68
69
|
};
|
|
69
70
|
type OnForgotPasswordResetPasswordInput = {
|
|
70
|
-
email
|
|
71
|
-
code
|
|
71
|
+
email?: string;
|
|
72
|
+
code?: string;
|
|
72
73
|
newPassword: string;
|
|
73
74
|
};
|
|
74
75
|
type OnSignIn = (input: OnSignInInput) => Promise<void> | void;
|
|
@@ -85,11 +86,10 @@ type SignUpTerms = {
|
|
|
85
86
|
}[];
|
|
86
87
|
};
|
|
87
88
|
|
|
88
|
-
type
|
|
89
|
-
screen: AuthScreen;
|
|
90
|
-
setScreen: (screen: AuthScreen) => void;
|
|
89
|
+
type AuthPropsBase = {
|
|
91
90
|
signUpTerms?: SignUpTerms;
|
|
92
91
|
passwordMinimumLength?: number;
|
|
92
|
+
maxForgotPasswordCodeLength?: number;
|
|
93
93
|
onSignIn: OnSignIn;
|
|
94
94
|
onSignUp?: OnSignUp;
|
|
95
95
|
onConfirmSignUpCheckEmail?: OnConfirmSignUpCheckEmail;
|
|
@@ -97,12 +97,22 @@ type AuthLogicProps = {
|
|
|
97
97
|
onForgotPassword?: OnForgotPassword;
|
|
98
98
|
onForgotPasswordResetPassword?: OnForgotPasswordResetPassword;
|
|
99
99
|
};
|
|
100
|
+
type AuthPropsWithScreen = AuthPropsBase & {
|
|
101
|
+
screen: AuthScreen;
|
|
102
|
+
setScreen: (screen: AuthScreen) => void;
|
|
103
|
+
initialScreen?: never;
|
|
104
|
+
};
|
|
105
|
+
type AuthPropsWithInitialScreen = AuthPropsBase & {
|
|
106
|
+
screen?: never;
|
|
107
|
+
setScreen?: never;
|
|
108
|
+
initialScreen?: AuthScreen;
|
|
109
|
+
};
|
|
100
110
|
type AuthLayout = {
|
|
101
111
|
fullScreen?: boolean;
|
|
102
112
|
sideContent?: React$1.ReactNode;
|
|
103
113
|
sideContentPosition?: 'left' | 'right';
|
|
104
114
|
};
|
|
105
|
-
type AuthProps = LogoContextProps &
|
|
115
|
+
type AuthProps = LogoContextProps & (AuthPropsWithScreen | AuthPropsWithInitialScreen) & {
|
|
106
116
|
layout?: AuthLayout;
|
|
107
117
|
};
|
|
108
118
|
declare const Auth: (props: AuthProps) => react_jsx_runtime.JSX.Element;
|
|
@@ -126,7 +136,8 @@ type AuthForgotPasswordProps = {
|
|
|
126
136
|
declare const AuthForgotPassword: (props: AuthForgotPasswordProps) => react_jsx_runtime.JSX.Element;
|
|
127
137
|
|
|
128
138
|
type AuthForgotPasswordResetPasswordProps = {
|
|
129
|
-
email
|
|
139
|
+
email?: string;
|
|
140
|
+
maxCodeLength?: number;
|
|
130
141
|
onForgotPasswordResetPassword: OnForgotPasswordResetPassword;
|
|
131
142
|
onGoToSignIn: () => void;
|
|
132
143
|
passwordMinimumLength?: number;
|
package/dist/index.d.ts
CHANGED
|
@@ -46,7 +46,8 @@ type AuthScreen = {
|
|
|
46
46
|
} | {
|
|
47
47
|
value: 'confirmResetPassword';
|
|
48
48
|
context: {
|
|
49
|
-
email
|
|
49
|
+
email?: string;
|
|
50
|
+
code?: string;
|
|
50
51
|
};
|
|
51
52
|
};
|
|
52
53
|
type OnSignInInput = {
|
|
@@ -67,8 +68,8 @@ type OnForgotPasswordInput = {
|
|
|
67
68
|
email: string;
|
|
68
69
|
};
|
|
69
70
|
type OnForgotPasswordResetPasswordInput = {
|
|
70
|
-
email
|
|
71
|
-
code
|
|
71
|
+
email?: string;
|
|
72
|
+
code?: string;
|
|
72
73
|
newPassword: string;
|
|
73
74
|
};
|
|
74
75
|
type OnSignIn = (input: OnSignInInput) => Promise<void> | void;
|
|
@@ -85,11 +86,10 @@ type SignUpTerms = {
|
|
|
85
86
|
}[];
|
|
86
87
|
};
|
|
87
88
|
|
|
88
|
-
type
|
|
89
|
-
screen: AuthScreen;
|
|
90
|
-
setScreen: (screen: AuthScreen) => void;
|
|
89
|
+
type AuthPropsBase = {
|
|
91
90
|
signUpTerms?: SignUpTerms;
|
|
92
91
|
passwordMinimumLength?: number;
|
|
92
|
+
maxForgotPasswordCodeLength?: number;
|
|
93
93
|
onSignIn: OnSignIn;
|
|
94
94
|
onSignUp?: OnSignUp;
|
|
95
95
|
onConfirmSignUpCheckEmail?: OnConfirmSignUpCheckEmail;
|
|
@@ -97,12 +97,22 @@ type AuthLogicProps = {
|
|
|
97
97
|
onForgotPassword?: OnForgotPassword;
|
|
98
98
|
onForgotPasswordResetPassword?: OnForgotPasswordResetPassword;
|
|
99
99
|
};
|
|
100
|
+
type AuthPropsWithScreen = AuthPropsBase & {
|
|
101
|
+
screen: AuthScreen;
|
|
102
|
+
setScreen: (screen: AuthScreen) => void;
|
|
103
|
+
initialScreen?: never;
|
|
104
|
+
};
|
|
105
|
+
type AuthPropsWithInitialScreen = AuthPropsBase & {
|
|
106
|
+
screen?: never;
|
|
107
|
+
setScreen?: never;
|
|
108
|
+
initialScreen?: AuthScreen;
|
|
109
|
+
};
|
|
100
110
|
type AuthLayout = {
|
|
101
111
|
fullScreen?: boolean;
|
|
102
112
|
sideContent?: React$1.ReactNode;
|
|
103
113
|
sideContentPosition?: 'left' | 'right';
|
|
104
114
|
};
|
|
105
|
-
type AuthProps = LogoContextProps &
|
|
115
|
+
type AuthProps = LogoContextProps & (AuthPropsWithScreen | AuthPropsWithInitialScreen) & {
|
|
106
116
|
layout?: AuthLayout;
|
|
107
117
|
};
|
|
108
118
|
declare const Auth: (props: AuthProps) => react_jsx_runtime.JSX.Element;
|
|
@@ -126,7 +136,8 @@ type AuthForgotPasswordProps = {
|
|
|
126
136
|
declare const AuthForgotPassword: (props: AuthForgotPasswordProps) => react_jsx_runtime.JSX.Element;
|
|
127
137
|
|
|
128
138
|
type AuthForgotPasswordResetPasswordProps = {
|
|
129
|
-
email
|
|
139
|
+
email?: string;
|
|
140
|
+
maxCodeLength?: number;
|
|
130
141
|
onForgotPasswordResetPassword: OnForgotPasswordResetPassword;
|
|
131
142
|
onGoToSignIn: () => void;
|
|
132
143
|
passwordMinimumLength?: number;
|
package/dist/index.js
CHANGED
|
@@ -61,7 +61,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
61
61
|
// src/Auth.tsx
|
|
62
62
|
var import_react_notifications6 = require("@ttoss/react-notifications");
|
|
63
63
|
var import_ui7 = require("@ttoss/ui");
|
|
64
|
-
var
|
|
64
|
+
var React5 = __toESM(require("react"), 1);
|
|
65
65
|
|
|
66
66
|
// src/AuthCard.tsx
|
|
67
67
|
var import_react_notifications = require("@ttoss/react-notifications");
|
|
@@ -362,14 +362,29 @@ var AuthForgotPasswordResetPassword = /* @__PURE__ */__name(props => {
|
|
|
362
362
|
const {
|
|
363
363
|
intl
|
|
364
364
|
} = (0, import_react_i18n4.useI18n)();
|
|
365
|
+
const codeValidation = import_forms3.yup.string().required(intl.formatMessage({
|
|
366
|
+
id: "0XOzcH",
|
|
367
|
+
defaultMessage: [{
|
|
368
|
+
"type": 0,
|
|
369
|
+
"value": "Required field"
|
|
370
|
+
}]
|
|
371
|
+
}));
|
|
365
372
|
const schema = import_forms3.yup.object().shape({
|
|
366
|
-
code:
|
|
367
|
-
id: "
|
|
373
|
+
code: props.maxCodeLength ? codeValidation.min(props.maxCodeLength, intl.formatMessage({
|
|
374
|
+
id: "S3pjKw",
|
|
368
375
|
defaultMessage: [{
|
|
369
376
|
"type": 0,
|
|
370
|
-
"value": "
|
|
377
|
+
"value": "Minimum "
|
|
378
|
+
}, {
|
|
379
|
+
"type": 1,
|
|
380
|
+
"value": "value"
|
|
381
|
+
}, {
|
|
382
|
+
"type": 0,
|
|
383
|
+
"value": " characters"
|
|
371
384
|
}]
|
|
372
|
-
}
|
|
385
|
+
}, {
|
|
386
|
+
value: props.maxCodeLength
|
|
387
|
+
})).max(props.maxCodeLength, intl.formatMessage({
|
|
373
388
|
id: "SfWKyS",
|
|
374
389
|
defaultMessage: [{
|
|
375
390
|
"type": 0,
|
|
@@ -382,8 +397,8 @@ var AuthForgotPasswordResetPassword = /* @__PURE__ */__name(props => {
|
|
|
382
397
|
"value": " characters"
|
|
383
398
|
}]
|
|
384
399
|
}, {
|
|
385
|
-
value:
|
|
386
|
-
})),
|
|
400
|
+
value: props.maxCodeLength
|
|
401
|
+
})) : codeValidation,
|
|
387
402
|
newPassword: import_forms3.yup.string().required(intl.formatMessage({
|
|
388
403
|
id: "kdFYba",
|
|
389
404
|
defaultMessage: [{
|
|
@@ -864,6 +879,18 @@ var ErrorBoundary = /* @__PURE__ */__name(({
|
|
|
864
879
|
}, children);
|
|
865
880
|
}, "ErrorBoundary");
|
|
866
881
|
|
|
882
|
+
// src/useAuthScreen.ts
|
|
883
|
+
var React4 = __toESM(require("react"), 1);
|
|
884
|
+
var useAuthScreen = /* @__PURE__ */__name(initialScreen => {
|
|
885
|
+
const [screen, setScreen] = React4.useState(initialScreen || {
|
|
886
|
+
value: "signIn"
|
|
887
|
+
});
|
|
888
|
+
return {
|
|
889
|
+
screen,
|
|
890
|
+
setScreen
|
|
891
|
+
};
|
|
892
|
+
}, "useAuthScreen");
|
|
893
|
+
|
|
867
894
|
// src/Auth.tsx
|
|
868
895
|
var AuthLogic = /* @__PURE__ */__name(props => {
|
|
869
896
|
const {
|
|
@@ -876,16 +903,17 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
876
903
|
onConfirmSignUpWithCode,
|
|
877
904
|
onForgotPasswordResetPassword,
|
|
878
905
|
passwordMinimumLength,
|
|
879
|
-
signUpTerms
|
|
906
|
+
signUpTerms,
|
|
907
|
+
maxForgotPasswordCodeLength
|
|
880
908
|
} = props;
|
|
881
909
|
const {
|
|
882
910
|
clearNotifications,
|
|
883
911
|
setLoading
|
|
884
912
|
} = (0, import_react_notifications6.useNotifications)();
|
|
885
|
-
|
|
913
|
+
React5.useEffect(() => {
|
|
886
914
|
clearNotifications();
|
|
887
915
|
}, [screen.value, clearNotifications]);
|
|
888
|
-
|
|
916
|
+
React5.useEffect(() => {
|
|
889
917
|
return clearNotifications;
|
|
890
918
|
}, [clearNotifications]);
|
|
891
919
|
const onGoToSignIn = /* @__PURE__ */__name(() => {
|
|
@@ -893,7 +921,7 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
893
921
|
value: "signIn"
|
|
894
922
|
});
|
|
895
923
|
}, "onGoToSignIn");
|
|
896
|
-
const onGoToSignUp =
|
|
924
|
+
const onGoToSignUp = React5.useCallback(() => {
|
|
897
925
|
if (!onSignUp) {
|
|
898
926
|
return void 0;
|
|
899
927
|
}
|
|
@@ -901,7 +929,7 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
901
929
|
value: "signUp"
|
|
902
930
|
});
|
|
903
931
|
}, [onSignUp, setScreen]);
|
|
904
|
-
const onGoToForgotPassword =
|
|
932
|
+
const onGoToForgotPassword = React5.useCallback(() => {
|
|
905
933
|
if (!onForgotPassword) {
|
|
906
934
|
return void 0;
|
|
907
935
|
}
|
|
@@ -921,7 +949,7 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
921
949
|
};
|
|
922
950
|
}, "notificationsWrapper");
|
|
923
951
|
if (screen.value === "signIn") {
|
|
924
|
-
return /* @__PURE__ */
|
|
952
|
+
return /* @__PURE__ */React5.createElement(AuthSignIn, {
|
|
925
953
|
onSignIn: notificationsWrapper(onSignIn),
|
|
926
954
|
onGoToSignUp: onSignUp && onGoToSignUp,
|
|
927
955
|
onGoToForgotPassword: onForgotPassword && onGoToForgotPassword,
|
|
@@ -929,7 +957,7 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
929
957
|
});
|
|
930
958
|
}
|
|
931
959
|
if (screen.value === "signUp" && onSignUp) {
|
|
932
|
-
return /* @__PURE__ */
|
|
960
|
+
return /* @__PURE__ */React5.createElement(AuthSignUp, {
|
|
933
961
|
onSignUp: notificationsWrapper(onSignUp),
|
|
934
962
|
passwordMinimumLength,
|
|
935
963
|
onGoToSignIn,
|
|
@@ -937,27 +965,28 @@ var AuthLogic = /* @__PURE__ */__name(props => {
|
|
|
937
965
|
});
|
|
938
966
|
}
|
|
939
967
|
if (screen.value === "forgotPassword" && onForgotPassword) {
|
|
940
|
-
return /* @__PURE__ */
|
|
968
|
+
return /* @__PURE__ */React5.createElement(AuthForgotPassword, {
|
|
941
969
|
onForgotPassword: notificationsWrapper(onForgotPassword),
|
|
942
970
|
onGoToSignIn,
|
|
943
971
|
onGoToSignUp: onSignUp && onGoToSignUp
|
|
944
972
|
});
|
|
945
973
|
}
|
|
946
974
|
if (screen.value === "confirmResetPassword" && onForgotPasswordResetPassword) {
|
|
947
|
-
return /* @__PURE__ */
|
|
975
|
+
return /* @__PURE__ */React5.createElement(AuthForgotPasswordResetPassword, {
|
|
948
976
|
onForgotPasswordResetPassword: notificationsWrapper(onForgotPasswordResetPassword),
|
|
949
977
|
onGoToSignIn,
|
|
950
|
-
email: screen.context.email
|
|
978
|
+
email: screen.context.email,
|
|
979
|
+
maxCodeLength: maxForgotPasswordCodeLength
|
|
951
980
|
});
|
|
952
981
|
}
|
|
953
982
|
if (screen.value === "confirmSignUpWithCode" && onConfirmSignUpWithCode) {
|
|
954
|
-
return /* @__PURE__ */
|
|
983
|
+
return /* @__PURE__ */React5.createElement(AuthConfirmSignUpWithCode, {
|
|
955
984
|
onConfirmSignUpWithCode: notificationsWrapper(onConfirmSignUpWithCode),
|
|
956
985
|
email: screen.context.email
|
|
957
986
|
});
|
|
958
987
|
}
|
|
959
988
|
if (screen.value === "confirmSignUpCheckEmail" && onConfirmSignUpCheckEmail) {
|
|
960
|
-
return /* @__PURE__ */
|
|
989
|
+
return /* @__PURE__ */React5.createElement(AuthConfirmSignUpCheckEmail, {
|
|
961
990
|
onConfirmSignUpCheckEmail
|
|
962
991
|
});
|
|
963
992
|
}
|
|
@@ -969,20 +998,35 @@ var Auth = /* @__PURE__ */__name(props => {
|
|
|
969
998
|
fullScreen: true
|
|
970
999
|
}
|
|
971
1000
|
} = props;
|
|
972
|
-
const
|
|
973
|
-
|
|
1001
|
+
const authScreenHook = useAuthScreen(props.initialScreen);
|
|
1002
|
+
const screen = props.screen ?? authScreenHook.screen;
|
|
1003
|
+
const setScreen = props.setScreen ?? authScreenHook.setScreen;
|
|
1004
|
+
const withLogoNode = React5.useMemo(() => {
|
|
1005
|
+
return /* @__PURE__ */React5.createElement(LogoProvider, {
|
|
974
1006
|
logo: props.logo
|
|
975
|
-
}, /* @__PURE__ */
|
|
976
|
-
|
|
1007
|
+
}, /* @__PURE__ */React5.createElement(ErrorBoundary, null, /* @__PURE__ */React5.createElement(AuthLogic, {
|
|
1008
|
+
screen,
|
|
1009
|
+
setScreen,
|
|
1010
|
+
signUpTerms: props.signUpTerms,
|
|
1011
|
+
passwordMinimumLength: props.passwordMinimumLength,
|
|
1012
|
+
maxForgotPasswordCodeLength: props.maxForgotPasswordCodeLength,
|
|
1013
|
+
onSignIn: props.onSignIn,
|
|
1014
|
+
onSignUp: props.onSignUp,
|
|
1015
|
+
onConfirmSignUpCheckEmail: props.onConfirmSignUpCheckEmail,
|
|
1016
|
+
onConfirmSignUpWithCode: props.onConfirmSignUpWithCode,
|
|
1017
|
+
onForgotPassword: props.onForgotPassword,
|
|
1018
|
+
onForgotPasswordResetPassword: props.onForgotPasswordResetPassword
|
|
1019
|
+
})));
|
|
1020
|
+
}, [props, screen, setScreen]);
|
|
977
1021
|
if (layout.fullScreen) {
|
|
978
1022
|
if (layout.sideContentPosition) {
|
|
979
|
-
return /* @__PURE__ */
|
|
1023
|
+
return /* @__PURE__ */React5.createElement(AuthFullScreen, null, /* @__PURE__ */React5.createElement(import_ui7.Flex, {
|
|
980
1024
|
sx: {
|
|
981
1025
|
width: "100%",
|
|
982
1026
|
height: "100%",
|
|
983
1027
|
flexDirection: layout.sideContentPosition === "left" ? "row" : "row-reverse"
|
|
984
1028
|
}
|
|
985
|
-
}, /* @__PURE__ */
|
|
1029
|
+
}, /* @__PURE__ */React5.createElement(import_ui7.Flex, {
|
|
986
1030
|
sx: {
|
|
987
1031
|
width: "100%",
|
|
988
1032
|
height: "100%",
|
|
@@ -990,7 +1034,7 @@ var Auth = /* @__PURE__ */__name(props => {
|
|
|
990
1034
|
justifyContent: "center",
|
|
991
1035
|
alignItems: "center"
|
|
992
1036
|
}
|
|
993
|
-
}, layout.sideContent), /* @__PURE__ */
|
|
1037
|
+
}, layout.sideContent), /* @__PURE__ */React5.createElement(import_ui7.Flex, {
|
|
994
1038
|
sx: {
|
|
995
1039
|
width: "100%",
|
|
996
1040
|
height: "100%",
|
|
@@ -1000,15 +1044,15 @@ var Auth = /* @__PURE__ */__name(props => {
|
|
|
1000
1044
|
}
|
|
1001
1045
|
}, withLogoNode)));
|
|
1002
1046
|
}
|
|
1003
|
-
return /* @__PURE__ */
|
|
1047
|
+
return /* @__PURE__ */React5.createElement(AuthFullScreen, null, withLogoNode);
|
|
1004
1048
|
}
|
|
1005
1049
|
return withLogoNode;
|
|
1006
1050
|
}, "Auth");
|
|
1007
1051
|
|
|
1008
1052
|
// src/AuthProvider.tsx
|
|
1009
1053
|
var import_react_notifications7 = require("@ttoss/react-notifications");
|
|
1010
|
-
var
|
|
1011
|
-
var AuthContext = /* @__PURE__ */
|
|
1054
|
+
var React6 = __toESM(require("react"), 1);
|
|
1055
|
+
var AuthContext = /* @__PURE__ */React6.createContext(null);
|
|
1012
1056
|
var UNAUTHENTICATED_USER = {
|
|
1013
1057
|
user: null,
|
|
1014
1058
|
tokens: null,
|
|
@@ -1022,15 +1066,15 @@ var AuthProvider = /* @__PURE__ */__name(props => {
|
|
|
1022
1066
|
const {
|
|
1023
1067
|
setLoading
|
|
1024
1068
|
} = (0, import_react_notifications7.useNotifications)();
|
|
1025
|
-
const [authData, setAuthData] =
|
|
1069
|
+
const [authData, setAuthData] = React6.useState({
|
|
1026
1070
|
user: null,
|
|
1027
1071
|
tokens: null,
|
|
1028
1072
|
isAuthenticated: void 0
|
|
1029
1073
|
});
|
|
1030
|
-
|
|
1074
|
+
React6.useEffect(() => {
|
|
1031
1075
|
setLoading(authData.isAuthenticated === void 0);
|
|
1032
1076
|
}, [authData.isAuthenticated, setLoading]);
|
|
1033
|
-
|
|
1077
|
+
React6.useEffect(() => {
|
|
1034
1078
|
const fetchAuthData = /* @__PURE__ */__name(async () => {
|
|
1035
1079
|
try {
|
|
1036
1080
|
const data = await getAuthData?.();
|
|
@@ -1045,14 +1089,14 @@ var AuthProvider = /* @__PURE__ */__name(props => {
|
|
|
1045
1089
|
}, "fetchAuthData");
|
|
1046
1090
|
fetchAuthData();
|
|
1047
1091
|
}, [getAuthData]);
|
|
1048
|
-
const signOut =
|
|
1092
|
+
const signOut = React6.useCallback(async () => {
|
|
1049
1093
|
await signOutProp?.();
|
|
1050
1094
|
setAuthData(UNAUTHENTICATED_USER);
|
|
1051
1095
|
}, [signOutProp]);
|
|
1052
1096
|
if (authData.isAuthenticated === void 0) {
|
|
1053
1097
|
return null;
|
|
1054
1098
|
}
|
|
1055
|
-
return /* @__PURE__ */
|
|
1099
|
+
return /* @__PURE__ */React6.createElement(AuthContext.Provider, {
|
|
1056
1100
|
value: {
|
|
1057
1101
|
signOut,
|
|
1058
1102
|
isAuthenticated: authData.isAuthenticated ?? false,
|
|
@@ -1063,24 +1107,12 @@ var AuthProvider = /* @__PURE__ */__name(props => {
|
|
|
1063
1107
|
}, props.children);
|
|
1064
1108
|
}, "AuthProvider");
|
|
1065
1109
|
var useAuth = /* @__PURE__ */__name(() => {
|
|
1066
|
-
const context =
|
|
1110
|
+
const context = React6.useContext(AuthContext);
|
|
1067
1111
|
if (!context) {
|
|
1068
1112
|
throw new Error("useAuth must be used within an AuthProvider");
|
|
1069
1113
|
}
|
|
1070
1114
|
return context;
|
|
1071
1115
|
}, "useAuth");
|
|
1072
|
-
|
|
1073
|
-
// src/useAuthScreen.ts
|
|
1074
|
-
var React6 = __toESM(require("react"), 1);
|
|
1075
|
-
var useAuthScreen = /* @__PURE__ */__name(initialScreen => {
|
|
1076
|
-
const [screen, setScreen] = React6.useState(initialScreen || {
|
|
1077
|
-
value: "signIn"
|
|
1078
|
-
});
|
|
1079
|
-
return {
|
|
1080
|
-
screen,
|
|
1081
|
-
setScreen
|
|
1082
|
-
};
|
|
1083
|
-
}, "useAuthScreen");
|
|
1084
1116
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1085
1117
|
0 && (module.exports = {
|
|
1086
1118
|
Auth,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/react-auth-core",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Core authentication components and abstractions for React apps.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "ttoss",
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"react": ">=16.8.0",
|
|
32
|
-
"@ttoss/components": "^2.13.0",
|
|
33
|
-
"@ttoss/react-i18n": "^2.1.0",
|
|
34
32
|
"@ttoss/forms": "^0.41.0",
|
|
35
33
|
"@ttoss/react-notifications": "^2.6.0",
|
|
34
|
+
"@ttoss/react-i18n": "^2.1.0",
|
|
35
|
+
"@ttoss/logger": "^0.7.4",
|
|
36
36
|
"@ttoss/ui": "^6.6.0",
|
|
37
|
-
"@ttoss/
|
|
37
|
+
"@ttoss/components": "^2.13.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@jest/globals": "^29.7.0",
|
|
@@ -43,14 +43,14 @@
|
|
|
43
43
|
"react": "^19.2.4",
|
|
44
44
|
"tsup": "^8.5.1",
|
|
45
45
|
"@ttoss/components": "^2.13.0",
|
|
46
|
-
"@ttoss/
|
|
46
|
+
"@ttoss/forms": "^0.41.0",
|
|
47
47
|
"@ttoss/i18n-cli": "^0.7.39",
|
|
48
48
|
"@ttoss/react-i18n": "^2.1.0",
|
|
49
|
-
"@ttoss/react-notifications": "^2.6.0",
|
|
50
|
-
"@ttoss/test-utils": "^4.1.0",
|
|
51
|
-
"@ttoss/forms": "^0.41.0",
|
|
52
49
|
"@ttoss/logger": "^0.7.4",
|
|
53
|
-
"@ttoss/
|
|
50
|
+
"@ttoss/config": "^1.36.0",
|
|
51
|
+
"@ttoss/react-notifications": "^2.6.0",
|
|
52
|
+
"@ttoss/ui": "^6.6.0",
|
|
53
|
+
"@ttoss/test-utils": "^4.1.0"
|
|
54
54
|
},
|
|
55
55
|
"keywords": [
|
|
56
56
|
"React",
|