prostgles-client 4.0.426 → 4.0.427
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,6 +1,31 @@
|
|
|
1
|
-
import type { AuthHandler } from "../getAuthHandler";
|
|
2
|
-
|
|
1
|
+
import type { AuthHandler, ConfirmEmail, PasswordLogin, PasswordRegister } from "../getAuthHandler";
|
|
2
|
+
type PasswordLoginDataAndFunc = {
|
|
3
|
+
onCall: PasswordLogin;
|
|
4
|
+
result: undefined | Awaited<ReturnType<PasswordLogin>>;
|
|
5
|
+
} & Parameters<PasswordLogin>[0];
|
|
6
|
+
type PasswordRegisterDataAndFunc = {
|
|
7
|
+
onCall: PasswordRegister;
|
|
8
|
+
result: undefined | Awaited<ReturnType<PasswordRegister>>;
|
|
9
|
+
} & Parameters<PasswordRegister>[0];
|
|
10
|
+
type FormData = ({
|
|
11
|
+
type: "login";
|
|
12
|
+
} & PasswordLoginDataAndFunc) | ({
|
|
13
|
+
type: "loginTotp";
|
|
14
|
+
} & PasswordLoginDataAndFunc) | ({
|
|
15
|
+
type: "loginTotpRecovery";
|
|
16
|
+
} & PasswordLoginDataAndFunc) | ({
|
|
17
|
+
type: "registerWithPassword";
|
|
18
|
+
} & PasswordRegisterDataAndFunc) | ({
|
|
19
|
+
type: "registerWithPasswordConfirmationCode";
|
|
20
|
+
onCall: ConfirmEmail;
|
|
21
|
+
result: undefined | Awaited<ReturnType<ConfirmEmail>>;
|
|
22
|
+
} & Parameters<ConfirmEmail>[0]);
|
|
23
|
+
type FormStates = FormData["type"];
|
|
24
|
+
export declare const useAuthState: ({ auth, initialState, initialUsername, invitationToken, }: {
|
|
3
25
|
auth: AuthHandler;
|
|
26
|
+
initialState?: FormStates;
|
|
27
|
+
initialUsername?: string;
|
|
28
|
+
invitationToken?: string;
|
|
4
29
|
}) => {
|
|
5
30
|
formHandlers: {
|
|
6
31
|
state: "login" | "loginTotp" | "loginTotpRecovery" | "registerWithPassword" | "registerWithPasswordConfirmationCode";
|
|
@@ -44,7 +69,7 @@ export declare const useAuthState: ({ auth }: {
|
|
|
44
69
|
};
|
|
45
70
|
}>) => Promise<import("./authRequest").ClientAuthResponse<"login">>) | undefined;
|
|
46
71
|
} | {
|
|
47
|
-
onCall: (data: import("prostgles-types").JSONB.GetObjectType<{
|
|
72
|
+
onCall: ((data: import("prostgles-types").JSONB.GetObjectType<{
|
|
48
73
|
readonly invitationToken: {
|
|
49
74
|
readonly type: "string";
|
|
50
75
|
readonly optional: true;
|
|
@@ -58,7 +83,7 @@ export declare const useAuthState: ({ auth }: {
|
|
|
58
83
|
readonly type: "string";
|
|
59
84
|
readonly optional: true;
|
|
60
85
|
};
|
|
61
|
-
}>) => Promise<import("./authRequest").ClientAuthResponse<"register"
|
|
86
|
+
}>) => Promise<import("./authRequest").ClientAuthResponse<"register">>) | undefined;
|
|
62
87
|
result: ({
|
|
63
88
|
code: "server-error" | "rate-limit-exceeded" | "something-went-wrong";
|
|
64
89
|
success: false;
|
|
@@ -505,4 +530,5 @@ export declare const ERR_CODE_MESSAGES: {
|
|
|
505
530
|
"expired-otp-code": string;
|
|
506
531
|
"provider-issue": string;
|
|
507
532
|
};
|
|
533
|
+
export {};
|
|
508
534
|
//# sourceMappingURL=useAuthState.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuthState.d.ts","sourceRoot":"","sources":["../../lib/auth/useAuthState.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"useAuthState.d.ts","sourceRoot":"","sources":["../../lib/auth/useAuthState.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAEpG,KAAK,wBAAwB,GAAG;IAC9B,MAAM,EAAE,aAAa,CAAC;IACtB,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,UAA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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAaY,OAAO;iBACP,MAAM;;;CAwNlB,CAAC;AAEF,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmC7B,CAAC"}
|
|
@@ -5,10 +5,10 @@ const prostgles_types_1 = require("prostgles-types");
|
|
|
5
5
|
const reactImports_1 = require("../hooks/reactImports");
|
|
6
6
|
const { useEffect, useState } = reactImports_1.reactImports;
|
|
7
7
|
const loginStates = ["login", "loginTotp", "loginTotpRecovery"];
|
|
8
|
-
const useAuthState = ({ auth }) => {
|
|
8
|
+
const useAuthState = ({ auth, initialState = "login", initialUsername = "", invitationToken, }) => {
|
|
9
9
|
const [loading, setIsLoading] = useState(false);
|
|
10
|
-
const [state, setState] = useState(
|
|
11
|
-
const [username, setUsername] = useState(
|
|
10
|
+
const [state, setState] = useState(initialState);
|
|
11
|
+
const [username, setUsername] = useState(initialUsername);
|
|
12
12
|
const [password, setPassword] = useState("");
|
|
13
13
|
const [emailVerificationCode, setEmailVerificationCode] = useState("");
|
|
14
14
|
const [totpToken, setTotpToken] = useState("");
|
|
@@ -30,16 +30,18 @@ const useAuthState = ({ auth }) => {
|
|
|
30
30
|
useEffect(() => {
|
|
31
31
|
_setError("");
|
|
32
32
|
}, [username, password, totpToken, totpRecoveryCode, confirmPassword]);
|
|
33
|
-
const
|
|
33
|
+
const canRegister = auth.signupWithEmail && (!auth.signupWithEmailOptions?.requireInvitation || invitationToken);
|
|
34
|
+
const requiresPassword = auth.signupWithEmailOptions?.requirePassword ?? true;
|
|
35
|
+
const showPassword = (state === "registerWithPassword" && canRegister && requiresPassword) ||
|
|
34
36
|
(state === "login" && auth.loginType === "email" && usernamesWithPassword.includes(username)) ||
|
|
35
37
|
(state === "login" && auth.loginType === "email+password");
|
|
36
|
-
const showUsername = (state === "registerWithPassword" &&
|
|
38
|
+
const showUsername = (state === "registerWithPassword" && canRegister) ||
|
|
37
39
|
(state === "login" && auth.loginType === "email") ||
|
|
38
40
|
(state === "login" && auth.loginType === "email+password");
|
|
39
|
-
const showConfirmPassword = state === "registerWithPassword" &&
|
|
41
|
+
const showConfirmPassword = state === "registerWithPassword" && showPassword;
|
|
40
42
|
const showTotpCode = state === "loginTotp" && auth.login;
|
|
41
43
|
const showTotpRecoveryCode = state === "loginTotpRecovery" && auth.login;
|
|
42
|
-
const showEmailVerificationCode = state === "registerWithPasswordConfirmationCode" && auth.
|
|
44
|
+
const showEmailVerificationCode = state === "registerWithPasswordConfirmationCode" && auth.confirmEmail;
|
|
43
45
|
const show = {
|
|
44
46
|
username: Boolean(showUsername),
|
|
45
47
|
password: Boolean(showPassword),
|
|
@@ -69,13 +71,13 @@ const useAuthState = ({ auth }) => {
|
|
|
69
71
|
: state === "login" && auth.loginType === "email+password" ? commonFormHandlers
|
|
70
72
|
: state === "loginTotp" && auth.login ? commonFormHandlers
|
|
71
73
|
: state === "loginTotpRecovery" && auth.login ? commonFormHandlers
|
|
72
|
-
: state === "registerWithPassword" &&
|
|
74
|
+
: state === "registerWithPassword" && canRegister ?
|
|
73
75
|
{
|
|
74
76
|
...commonFormHandlers,
|
|
75
|
-
onCall: auth.
|
|
77
|
+
onCall: auth.signupWithEmail,
|
|
76
78
|
result,
|
|
77
79
|
}
|
|
78
|
-
: state === "registerWithPasswordConfirmationCode" && auth.
|
|
80
|
+
: state === "registerWithPasswordConfirmationCode" && auth.confirmEmail ?
|
|
79
81
|
{
|
|
80
82
|
...commonFormHandlers,
|
|
81
83
|
onCall: () => {
|
|
@@ -101,6 +103,7 @@ const useAuthState = ({ auth }) => {
|
|
|
101
103
|
remember_me: false,
|
|
102
104
|
totp_token: isOnlyShowingUsername ? undefined : totpToken,
|
|
103
105
|
totp_recovery_code: isOnlyShowingUsername ? undefined : totpRecoveryCode,
|
|
106
|
+
...(state === "registerWithPassword" && invitationToken && { invitationToken }),
|
|
104
107
|
};
|
|
105
108
|
const errorMap = {
|
|
106
109
|
"no match": "Invalid credentials",
|
|
@@ -135,7 +138,7 @@ const useAuthState = ({ auth }) => {
|
|
|
135
138
|
if (!username) {
|
|
136
139
|
return setError("Email cannot be empty");
|
|
137
140
|
}
|
|
138
|
-
if (formHandlers.state === "registerWithPassword") {
|
|
141
|
+
if (formHandlers.state === "registerWithPassword" && requiresPassword) {
|
|
139
142
|
if (!password) {
|
|
140
143
|
return setError("Password cannot be empty");
|
|
141
144
|
}
|
|
@@ -171,7 +174,7 @@ const useAuthState = ({ auth }) => {
|
|
|
171
174
|
window.location.href = res.redirect_url;
|
|
172
175
|
}
|
|
173
176
|
if (state === "registerWithPassword") {
|
|
174
|
-
setState("registerWithPasswordConfirmationCode");
|
|
177
|
+
setState(res.code === "email-verified" ? "login" : "registerWithPasswordConfirmationCode");
|
|
175
178
|
}
|
|
176
179
|
let message = (res.code && SIGNUP_CODE_MESSAGES[res.code]) ?? res.message ?? "Success";
|
|
177
180
|
if (formHandlers.state === "registerWithPasswordConfirmationCode") {
|