@greatapps/common 1.1.478 → 1.1.479
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/dist/components/account/hooks/useDisableTwoFactorAuthForm.mjs +3 -1
- package/dist/components/account/hooks/useDisableTwoFactorAuthForm.mjs.map +1 -1
- package/dist/hooks/useAuthQueryKey.mjs +23 -0
- package/dist/hooks/useAuthQueryKey.mjs.map +1 -0
- package/dist/index.mjs +17 -2
- package/dist/index.mjs.map +1 -1
- package/dist/modules/accounts/hooks/current-account.hook.mjs +5 -2
- package/dist/modules/accounts/hooks/current-account.hook.mjs.map +1 -1
- package/dist/modules/accounts/hooks/useAccountManagement.mjs +19 -9
- package/dist/modules/accounts/hooks/useAccountManagement.mjs.map +1 -1
- package/dist/modules/accounts/hooks/useCurrencies.mjs +3 -1
- package/dist/modules/accounts/hooks/useCurrencies.mjs.map +1 -1
- package/dist/modules/accounts/hooks/useTimezones.mjs +3 -1
- package/dist/modules/accounts/hooks/useTimezones.mjs.map +1 -1
- package/dist/modules/auth/hooks/useUserQuery.mjs +15 -6
- package/dist/modules/auth/hooks/useUserQuery.mjs.map +1 -1
- package/dist/modules/cards/hooks/cards.hook.mjs +3 -1
- package/dist/modules/cards/hooks/cards.hook.mjs.map +1 -1
- package/dist/modules/cards/hooks/create-card.hook.mjs +3 -1
- package/dist/modules/cards/hooks/create-card.hook.mjs.map +1 -1
- package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs +3 -1
- package/dist/modules/ia-credits/hooks/ia-credits.hook.mjs.map +1 -1
- package/dist/modules/plans/hooks/list-plans.hook.mjs +3 -1
- package/dist/modules/plans/hooks/list-plans.hook.mjs.map +1 -1
- package/dist/modules/plans/hooks/use-plan-by-id.hook.mjs +4 -2
- package/dist/modules/plans/hooks/use-plan-by-id.hook.mjs.map +1 -1
- package/dist/modules/plans/services/plans.service.mjs +4 -2
- package/dist/modules/plans/services/plans.service.mjs.map +1 -1
- package/dist/modules/projects/hooks/add-project-user.hook.mjs +15 -12
- package/dist/modules/projects/hooks/add-project-user.hook.mjs.map +1 -1
- package/dist/modules/projects/hooks/list-all-account-users.hook.mjs +3 -1
- package/dist/modules/projects/hooks/list-all-account-users.hook.mjs.map +1 -1
- package/dist/modules/projects/hooks/list-available-users.hook.mjs +3 -1
- package/dist/modules/projects/hooks/list-available-users.hook.mjs.map +1 -1
- package/dist/modules/projects/hooks/list-project-users.hook.mjs +3 -1
- package/dist/modules/projects/hooks/list-project-users.hook.mjs.map +1 -1
- package/dist/modules/projects/hooks/remove-project-user.hook.mjs +15 -12
- package/dist/modules/projects/hooks/remove-project-user.hook.mjs.map +1 -1
- package/dist/modules/subscriptions/constants/query-keys.constants.mjs +2 -0
- package/dist/modules/subscriptions/constants/query-keys.constants.mjs.map +1 -1
- package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs +3 -1
- package/dist/modules/subscriptions/hooks/find-active-subscription.hook.mjs.map +1 -1
- package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs +3 -1
- package/dist/modules/subscriptions/hooks/list-subscriptions.hook.mjs.map +1 -1
- package/dist/modules/subscriptions/hooks/update-subscription-plan.hook.mjs +6 -3
- package/dist/modules/subscriptions/hooks/update-subscription-plan.hook.mjs.map +1 -1
- package/dist/modules/users/hooks/messages.hook.mjs +9 -4
- package/dist/modules/users/hooks/messages.hook.mjs.map +1 -1
- package/dist/providers/auth.provider.mjs +6 -4
- package/dist/providers/auth.provider.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/account/hooks/useDisableTwoFactorAuthForm.tsx +3 -1
- package/src/hooks/useAuthQueryKey.ts +32 -0
- package/src/index.ts +10 -1
- package/src/modules/accounts/hooks/current-account.hook.tsx +7 -3
- package/src/modules/accounts/hooks/useAccountManagement.ts +20 -11
- package/src/modules/accounts/hooks/useCurrencies.ts +5 -2
- package/src/modules/accounts/hooks/useTimezones.ts +5 -2
- package/src/modules/auth/hooks/useUserQuery.ts +75 -65
- package/src/modules/cards/hooks/cards.hook.ts +5 -2
- package/src/modules/cards/hooks/create-card.hook.ts +3 -1
- package/src/modules/ia-credits/hooks/ia-credits.hook.ts +99 -97
- package/src/modules/plans/hooks/list-plans.hook.ts +5 -2
- package/src/modules/plans/hooks/use-plan-by-id.hook.ts +4 -2
- package/src/modules/plans/services/plans.service.ts +4 -2
- package/src/modules/projects/hooks/add-project-user.hook.ts +16 -13
- package/src/modules/projects/hooks/list-all-account-users.hook.ts +8 -3
- package/src/modules/projects/hooks/list-available-users.hook.ts +8 -3
- package/src/modules/projects/hooks/list-project-users.hook.ts +8 -3
- package/src/modules/projects/hooks/remove-project-user.hook.ts +16 -13
- package/src/modules/subscriptions/constants/query-keys.constants.ts +3 -1
- package/src/modules/subscriptions/hooks/find-active-subscription.hook.ts +4 -1
- package/src/modules/subscriptions/hooks/list-subscriptions.hook.ts +4 -1
- package/src/modules/subscriptions/hooks/update-subscription-plan.hook.ts +6 -3
- package/src/modules/users/hooks/messages.hook.ts +12 -5
- package/src/providers/auth.provider.tsx +162 -160
|
@@ -1,160 +1,162 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
createContext,
|
|
5
|
-
ReactNode,
|
|
6
|
-
useCallback,
|
|
7
|
-
useContext,
|
|
8
|
-
useMemo,
|
|
9
|
-
} from "react";
|
|
10
|
-
import { useQueryClient } from "@tanstack/react-query";
|
|
11
|
-
import {
|
|
12
|
-
useUserQuery,
|
|
13
|
-
useSetUserData,
|
|
14
|
-
useUserValidateSession,
|
|
15
|
-
} from "../modules/auth/hooks/useUserQuery";
|
|
16
|
-
import { useLogin } from "../modules/auth/hooks/login.hook";
|
|
17
|
-
import { useRegister } from "../modules/auth/hooks/register.hook";
|
|
18
|
-
import { useLogout } from "../modules/auth/hooks/logout.hook";
|
|
19
|
-
import {
|
|
20
|
-
AuthState,
|
|
21
|
-
LoginRequest,
|
|
22
|
-
RegisterRequest,
|
|
23
|
-
} from "../modules/auth/schema";
|
|
24
|
-
|
|
25
|
-
export type LoginResult =
|
|
26
|
-
| { result: "success" }
|
|
27
|
-
| { result: "two_factor_required"; cookie: string; twoFactorMode: "verify" | "setup" }
|
|
28
|
-
| { result: "error"; message: string };
|
|
29
|
-
import {
|
|
30
|
-
useCurrentAccount,
|
|
31
|
-
ACCOUNT_QUERY_KEY,
|
|
32
|
-
} from "../modules/accounts/hooks/current-account.hook";
|
|
33
|
-
import { useWhitelabelUrls } from "./whitelabel.provider";
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const { data:
|
|
52
|
-
const { isLoading:
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const { mutateAsync:
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
queryClient.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
queryClient.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
ReactNode,
|
|
6
|
+
useCallback,
|
|
7
|
+
useContext,
|
|
8
|
+
useMemo,
|
|
9
|
+
} from "react";
|
|
10
|
+
import { useQueryClient } from "@tanstack/react-query";
|
|
11
|
+
import {
|
|
12
|
+
useUserQuery,
|
|
13
|
+
useSetUserData,
|
|
14
|
+
useUserValidateSession,
|
|
15
|
+
} from "../modules/auth/hooks/useUserQuery";
|
|
16
|
+
import { useLogin } from "../modules/auth/hooks/login.hook";
|
|
17
|
+
import { useRegister } from "../modules/auth/hooks/register.hook";
|
|
18
|
+
import { useLogout } from "../modules/auth/hooks/logout.hook";
|
|
19
|
+
import {
|
|
20
|
+
AuthState,
|
|
21
|
+
LoginRequest,
|
|
22
|
+
RegisterRequest,
|
|
23
|
+
} from "../modules/auth/schema";
|
|
24
|
+
|
|
25
|
+
export type LoginResult =
|
|
26
|
+
| { result: "success" }
|
|
27
|
+
| { result: "two_factor_required"; cookie: string; twoFactorMode: "verify" | "setup" }
|
|
28
|
+
| { result: "error"; message: string };
|
|
29
|
+
import {
|
|
30
|
+
useCurrentAccount,
|
|
31
|
+
ACCOUNT_QUERY_KEY,
|
|
32
|
+
} from "../modules/accounts/hooks/current-account.hook";
|
|
33
|
+
import { useWhitelabelUrls } from "./whitelabel.provider";
|
|
34
|
+
import { useWlQueryKey } from "../hooks/useAuthQueryKey";
|
|
35
|
+
|
|
36
|
+
interface AuthContextProps extends AuthState {
|
|
37
|
+
login: (credentials: LoginRequest) => Promise<LoginResult>;
|
|
38
|
+
register: (data: RegisterRequest) => Promise<void>;
|
|
39
|
+
logout: () => Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const AuthContext = createContext<AuthContextProps | undefined>(
|
|
43
|
+
undefined,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
interface AuthProviderProps {
|
|
47
|
+
children: ReactNode;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function AuthProvider({ children }: AuthProviderProps) {
|
|
51
|
+
const { data: user, isLoading: isQueryLoading } = useUserQuery();
|
|
52
|
+
const { data: account, isLoading: isAccountLoading } = useCurrentAccount();
|
|
53
|
+
const { isLoading: isSessionLoading } = useUserValidateSession();
|
|
54
|
+
|
|
55
|
+
const queryClient = useQueryClient();
|
|
56
|
+
const setUserData = useSetUserData();
|
|
57
|
+
const { accountsUrl, pagesUrl } = useWhitelabelUrls();
|
|
58
|
+
const accountKey = useWlQueryKey([...ACCOUNT_QUERY_KEY]);
|
|
59
|
+
|
|
60
|
+
const { mutateAsync: loginMutate, isPending: isLogging } = useLogin();
|
|
61
|
+
const { mutateAsync: registerMutate, isPending: isRegistering } =
|
|
62
|
+
useRegister();
|
|
63
|
+
const { mutateAsync: logoutMutate, isPending: isLoggingOut } = useLogout();
|
|
64
|
+
|
|
65
|
+
const isAuthenticated = !!user;
|
|
66
|
+
const isLoading =
|
|
67
|
+
isLogging ||
|
|
68
|
+
isRegistering ||
|
|
69
|
+
isLoggingOut ||
|
|
70
|
+
isQueryLoading ||
|
|
71
|
+
isAccountLoading ||
|
|
72
|
+
isSessionLoading;
|
|
73
|
+
|
|
74
|
+
const login = useCallback(
|
|
75
|
+
async (credentials: LoginRequest): Promise<LoginResult> => {
|
|
76
|
+
try {
|
|
77
|
+
const data = await loginMutate(credentials);
|
|
78
|
+
|
|
79
|
+
if (data.result === "two_factor_required") {
|
|
80
|
+
return { result: "two_factor_required", cookie: data.cookie, twoFactorMode: data.twoFactorMode };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
queryClient.clear();
|
|
84
|
+
setUserData(data.user);
|
|
85
|
+
queryClient.setQueryData(accountKey, data.account);
|
|
86
|
+
|
|
87
|
+
return { result: "success" };
|
|
88
|
+
} catch (error) {
|
|
89
|
+
const message =
|
|
90
|
+
error instanceof Error
|
|
91
|
+
? error.message
|
|
92
|
+
: "Ocorreu um erro ao fazer login. Tente novamente.";
|
|
93
|
+
return { result: "error", message };
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
[queryClient, setUserData, accountKey],
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const register = useCallback(
|
|
100
|
+
async (data: RegisterRequest): Promise<void> => {
|
|
101
|
+
await registerMutate(data, {
|
|
102
|
+
onSuccess: (data) => {
|
|
103
|
+
queryClient.clear();
|
|
104
|
+
setUserData(data.user);
|
|
105
|
+
queryClient.setQueryData(accountKey, data.account);
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
[queryClient, setUserData, accountKey],
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const resolveLogoutRedirect = useCallback(() => {
|
|
113
|
+
try {
|
|
114
|
+
const currentOrigin = window.location.origin;
|
|
115
|
+
const pagesOrigin = pagesUrl ? new URL(pagesUrl).origin : "";
|
|
116
|
+
|
|
117
|
+
if (pagesOrigin && currentOrigin === pagesOrigin) {
|
|
118
|
+
return `${accountsUrl}/login?redirect=${encodeURIComponent(pagesUrl)}`;
|
|
119
|
+
}
|
|
120
|
+
} catch {
|
|
121
|
+
// fallback below
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return `${accountsUrl}/login?redirect=${encodeURIComponent(pagesUrl || "/")}`;
|
|
125
|
+
}, [accountsUrl, pagesUrl]);
|
|
126
|
+
|
|
127
|
+
const logout = useCallback(async (): Promise<void> => {
|
|
128
|
+
try {
|
|
129
|
+
await logoutMutate(undefined);
|
|
130
|
+
} catch {
|
|
131
|
+
// cookie já removido no servidor mesmo se a API falhou
|
|
132
|
+
} finally {
|
|
133
|
+
queryClient.clear();
|
|
134
|
+
window.location.assign(resolveLogoutRedirect());
|
|
135
|
+
}
|
|
136
|
+
}, [queryClient, logoutMutate, resolveLogoutRedirect]);
|
|
137
|
+
|
|
138
|
+
const value: AuthContextProps = useMemo(
|
|
139
|
+
() => ({
|
|
140
|
+
user: user ?? null,
|
|
141
|
+
account: account ?? null,
|
|
142
|
+
isAuthenticated,
|
|
143
|
+
isLoading,
|
|
144
|
+
login,
|
|
145
|
+
register,
|
|
146
|
+
logout,
|
|
147
|
+
}),
|
|
148
|
+
[user, account, isAuthenticated, isLoading, login, register, logout],
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function useAuth(): AuthContextProps {
|
|
155
|
+
const context = useContext(AuthContext);
|
|
156
|
+
|
|
157
|
+
if (context === undefined) {
|
|
158
|
+
throw new Error("useAuth deve ser usado dentro de um AuthProvider");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return context;
|
|
162
|
+
}
|