@greatapps/common 1.1.478 → 1.1.480

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