@mezo-org/passport 0.4.0-dev.2 → 0.4.0-dev.4

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 (75) hide show
  1. package/dist/src/api/auth.d.ts +43 -15
  2. package/dist/src/api/auth.d.ts.map +1 -1
  3. package/dist/src/api/auth.js +23 -26
  4. package/dist/src/api/auth.js.map +1 -1
  5. package/dist/src/hooks/constants.d.ts +2 -3
  6. package/dist/src/hooks/constants.d.ts.map +1 -1
  7. package/dist/src/hooks/constants.js +3 -4
  8. package/dist/src/hooks/constants.js.map +1 -1
  9. package/dist/src/hooks/index.d.ts +0 -3
  10. package/dist/src/hooks/index.d.ts.map +1 -1
  11. package/dist/src/hooks/index.js +0 -3
  12. package/dist/src/hooks/index.js.map +1 -1
  13. package/dist/src/hooks/useCreateAccount.d.ts +126 -72
  14. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
  15. package/dist/src/hooks/useCreateAccount.js +1 -6
  16. package/dist/src/hooks/useCreateAccount.js.map +1 -1
  17. package/dist/src/hooks/useCreateSession.d.ts +45 -126
  18. package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
  19. package/dist/src/hooks/useCreateSession.js +3 -11
  20. package/dist/src/hooks/useCreateSession.js.map +1 -1
  21. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts +5 -0
  22. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -0
  23. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +33 -0
  24. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -0
  25. package/dist/src/hooks/useGetAccountByAddress.d.ts +3 -5
  26. package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
  27. package/dist/src/hooks/useGetAccountByAddress.js +3 -4
  28. package/dist/src/hooks/useGetAccountByAddress.js.map +1 -1
  29. package/dist/src/hooks/useGetAccountByMezoId.d.ts +3 -5
  30. package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
  31. package/dist/src/hooks/useGetAccountByMezoId.js +3 -4
  32. package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -1
  33. package/dist/src/hooks/useGetCurrentAccount.d.ts +3 -11
  34. package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
  35. package/dist/src/hooks/useGetCurrentAccount.js +3 -3
  36. package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
  37. package/dist/src/hooks/useGetSession.d.ts +3 -20
  38. package/dist/src/hooks/useGetSession.d.ts.map +1 -1
  39. package/dist/src/hooks/useGetSession.js +2 -2
  40. package/dist/src/hooks/useGetSession.js.map +1 -1
  41. package/dist/src/hooks/useLinkAccount.d.ts +126 -72
  42. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
  43. package/dist/src/hooks/useLinkAccount.js +2 -18
  44. package/dist/src/hooks/useLinkAccount.js.map +1 -1
  45. package/dist/src/hooks/useSignInWithDiscord.d.ts +53 -134
  46. package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
  47. package/dist/src/hooks/useSignInWithDiscord.js +5 -19
  48. package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
  49. package/dist/src/hooks/useSignInWithWallet.d.ts +53 -134
  50. package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
  51. package/dist/src/hooks/useSignInWithWallet.js +7 -17
  52. package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
  53. package/dist/src/hooks/useSignOut.d.ts.map +1 -1
  54. package/dist/src/hooks/useSignOut.js +4 -2
  55. package/dist/src/hooks/useSignOut.js.map +1 -1
  56. package/dist/src/hooks/useUpdateMezoId.d.ts +153 -99
  57. package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
  58. package/dist/src/hooks/useUpdateMezoId.js +1 -8
  59. package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
  60. package/package.json +1 -1
  61. package/src/api/auth.ts +84 -52
  62. package/src/hooks/constants.ts +3 -4
  63. package/src/hooks/index.ts +0 -3
  64. package/src/hooks/useCreateAccount.ts +1 -6
  65. package/src/hooks/useCreateSession.ts +3 -10
  66. package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +45 -0
  67. package/src/hooks/useGetAccountByAddress.ts +11 -5
  68. package/src/hooks/useGetAccountByMezoId.ts +11 -5
  69. package/src/hooks/useGetCurrentAccount.ts +10 -4
  70. package/src/hooks/useGetSession.ts +10 -3
  71. package/src/hooks/useLinkAccount.ts +2 -27
  72. package/src/hooks/useSignInWithDiscord.ts +5 -23
  73. package/src/hooks/useSignInWithWallet.ts +7 -20
  74. package/src/hooks/useSignOut.ts +4 -2
  75. package/src/hooks/useUpdateMezoId.ts +1 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mezo-org/passport",
3
- "version": "0.4.0-dev.2",
3
+ "version": "0.4.0-dev.4",
4
4
  "main": "dist/src/index.js",
5
5
  "license": "GPL-3.0-only",
6
6
  "scripts": {
package/src/api/auth.ts CHANGED
@@ -10,7 +10,8 @@ type AccountPublicData = {
10
10
  evmAddress: string
11
11
  }
12
12
 
13
- type Account = {
13
+ type AuthUser = {
14
+ id: string
14
15
  app_metadata: {
15
16
  provider: PassportIdentityProvider
16
17
  providers: PassportIdentityProvider[]
@@ -18,13 +19,36 @@ type Account = {
18
19
  user_metadata: AccountPublicData
19
20
  }
20
21
 
22
+ type AuthProvider = "discord"
23
+
24
+ export type LinkedAccount =
25
+ | {
26
+ type: "wallet"
27
+ btcAddress?: string
28
+ evmAddress: string
29
+ }
30
+ | {
31
+ type: AuthProvider
32
+ name: string
33
+ id: string
34
+ avatarUrl: string
35
+ }
36
+
37
+ type Account = {
38
+ mezoId: string
39
+ hasModifiedMezoId: boolean
40
+ linkedAccounts: LinkedAccount[]
41
+ authUser: AuthUser
42
+ }
43
+
21
44
  type Session = {
22
- access_token: string
23
- token_type: string
24
- expires_in: number
25
- expires_at: number
26
- refresh_token: string
27
- user: Account
45
+ expiresIn: number
46
+ expiresAt: number
47
+ sessionLinks: {
48
+ _links: {
49
+ account: "/session/account"
50
+ }
51
+ }
28
52
  }
29
53
 
30
54
  type AuthenticationProviderRedirectResponse = {
@@ -45,28 +69,38 @@ export type LinkAccountRequest = CreateSessionRequest
45
69
 
46
70
  export type CreateAccountRequest = WalletAccountCredentials
47
71
 
72
+ export type GetSessionResponse = Session | { nonce: string }
73
+
74
+ export type GetCurrentAccountResponse = Partial<Account> | null
75
+
76
+ export type GetAccountByMezoIdOrAddressResponse = {
77
+ mezoId?: string
78
+ linkedAccounts?: LinkedAccount[]
79
+ }
80
+
48
81
  export const API_ENDPOINTS_BY_ENV = {
49
82
  mainnet: "https://api.mezo.org",
50
83
  testnet: "https://api.test.mezo.org",
51
84
  }
52
85
 
53
- const DEFAULT_API_ENDPOINT = API_ENDPOINTS_BY_ENV.mainnet
54
-
55
86
  export class AuthApiClient {
56
87
  private apiUrl: string
57
88
 
58
89
  constructor(apiUrl?: string) {
59
- this.apiUrl = apiUrl ?? DEFAULT_API_ENDPOINT
90
+ this.apiUrl = apiUrl ?? API_ENDPOINTS_BY_ENV.mainnet
60
91
  }
61
92
 
62
93
  /**
63
94
  * The request handler for the Passport API. It accepts an API endpoint and
64
95
  * request options.
65
96
  * @dev Generic type `D` is a type of returned object from the function
97
+ * @dev WARNING: This function hardcodes `credentials: "include"`, meaning
98
+ * cookies and authentication headers will be sent with every request. Ensure
99
+ * this is safe if you plan to use it in other contexts than auth api worker.
66
100
  * @param endpoint - The API endpoint to request. Should start with
67
101
  * forward slash ("/")
68
- * @param {RequestHandlerOptions<B>} options - The request options, the
69
- * `RequestInit` type with additional type-safe properties
102
+ * @param options - The request options, the `RequestInit` type with
103
+ * additional type-safe properties
70
104
  * @returns The promise of the API response
71
105
  */
72
106
  protected async handleRequest<D>(
@@ -91,42 +125,34 @@ export class AuthApiClient {
91
125
  .filter(([, value]) => value) // Remove falsy values
92
126
  .forEach(([key, value]) => url.searchParams.append(key, value!))
93
127
 
94
- try {
95
- const response = await fetch(url, {
96
- method,
97
- credentials: "include",
98
- headers: {
99
- "Content-Type": "application/json",
100
- ...headers,
101
- },
102
- body: body ? JSON.stringify(body) : undefined,
103
- ...restOptions,
104
- })
105
-
106
- const data = await response.json()
107
-
108
- if (!response.ok || data?.error) {
109
- const error = data?.error || "An error occurred"
110
- throw new Error(error)
111
- }
112
-
113
- return data as D
114
- } catch (error) {
115
- const errorMessage =
116
- error instanceof Error ? error.message : "An error occurred"
117
-
118
- throw new Error(errorMessage)
128
+ const response = await fetch(url, {
129
+ method,
130
+ credentials: "include",
131
+ headers: {
132
+ "Content-Type": "application/json",
133
+ ...headers,
134
+ },
135
+ body: body ? JSON.stringify(body) : undefined,
136
+ ...restOptions,
137
+ })
138
+
139
+ const data = await response.json()
140
+
141
+ if (!response.ok || data?.error) {
142
+ const error =
143
+ data?.error ||
144
+ `An error occurred when handling API request: ${JSON.stringify(data)}`
145
+ throw new Error(`Error [${response.status}]: ${error}`)
119
146
  }
147
+
148
+ return data as D
120
149
  }
121
150
 
122
151
  async getSession(code?: string) {
123
- const data = await this.handleRequest<Session | { nonce: string }>(
124
- "/session",
125
- {
126
- method: "GET",
127
- queryParams: { code },
128
- },
129
- )
152
+ const data = await this.handleRequest<GetSessionResponse>("/session", {
153
+ method: "GET",
154
+ queryParams: { code },
155
+ })
130
156
 
131
157
  return data
132
158
  }
@@ -154,15 +180,18 @@ export class AuthApiClient {
154
180
  }
155
181
 
156
182
  async getCurrentAccount() {
157
- const data = await this.handleRequest<Account>("/session/account", {
158
- method: "GET",
159
- })
183
+ const data = await this.handleRequest<GetCurrentAccountResponse>(
184
+ "/session/account",
185
+ {
186
+ method: "GET",
187
+ },
188
+ )
160
189
 
161
190
  return data
162
191
  }
163
192
 
164
193
  async getAccountByMezoIdOrAddress(mezoIdOrAddress: string) {
165
- const data = await this.handleRequest<AccountPublicData>(
194
+ const data = await this.handleRequest<GetAccountByMezoIdOrAddressResponse>(
166
195
  `/accounts/${mezoIdOrAddress}`,
167
196
  {
168
197
  method: "GET",
@@ -193,10 +222,13 @@ export class AuthApiClient {
193
222
  }
194
223
 
195
224
  async updateMezoId(newMezoId: string) {
196
- const data = await this.handleRequest<Account>("/session/account", {
197
- method: "PATCH",
198
- body: { newMezoId },
199
- })
225
+ const data = await this.handleRequest<Partial<Account> | null>(
226
+ "/session/account",
227
+ {
228
+ method: "PATCH",
229
+ body: { mezoId: newMezoId },
230
+ },
231
+ )
200
232
 
201
233
  return data
202
234
  }
@@ -1,6 +1,5 @@
1
1
  export const QUERY_KEYS = {
2
- ACCOUNT_BY_ADDRESS: "getAccountByAddress",
3
- ACCOUNT_BY_MEZO_ID: "getAccountByMezoId",
4
- SESSION: "getSession",
5
- CURRENT_ACCOUNT: "getCurrentAccount",
2
+ ACCOUNT: "account",
3
+ SESSION: "session",
4
+ CURRENT: "current",
6
5
  }
@@ -6,15 +6,12 @@ export {
6
6
  useSubscribeToConnectorEvent,
7
7
  useSubscribeToWalletNetworkDoesNotMatchProviderChain,
8
8
  } from "@mezo-org/orangekit"
9
- export * from "./constants"
10
- export * from "./useAuthApiClient"
11
9
  export * from "./useCreateAccount"
12
10
  export * from "./useGetAccountByAddress"
13
11
  export * from "./useGetAccountByMezoId"
14
12
  export * from "./useGetCurrentAccount"
15
13
  export * from "./useGetSession"
16
14
  export * from "./useLinkAccount"
17
- export * from "./usePassportContext"
18
15
  export * from "./useSignInWithDiscord"
19
16
  export * from "./useSignInWithWallet"
20
17
  export * from "./useSignOut"
@@ -12,12 +12,7 @@ export function useCreateAccount(useMutationOptions = {}) {
12
12
  authApiClient.createAccount(createAccountRequest),
13
13
  onSuccess: () => {
14
14
  queryClient.resetQueries({
15
- queryKey: [QUERY_KEYS.ACCOUNT_BY_ADDRESS],
16
- exact: false,
17
- })
18
- queryClient.resetQueries({
19
- queryKey: [QUERY_KEYS.ACCOUNT_BY_MEZO_ID],
20
- exact: false,
15
+ queryKey: [QUERY_KEYS.ACCOUNT],
21
16
  })
22
17
  },
23
18
  ...useMutationOptions,
@@ -14,22 +14,15 @@ export function useCreateSession(useMutationOptions = {}) {
14
14
  const { mutate, mutateAsync, ...rest } = useMutation({
15
15
  mutationFn: (createSessionRequest: CreateSessionRequest) =>
16
16
  authApiClient.createSession(createSessionRequest),
17
- onSuccess: (data) => {
17
+ onSuccess: () => {
18
18
  // Since `getSession` query also has code in it's query keys we can't
19
19
  // set query data for a specific query, because we don't know if the
20
20
  // code was used or not. Because of that we just reset all getSession
21
21
  // queries
22
+ queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION] })
22
23
  queryClient.resetQueries({
23
- queryKey: [QUERY_KEYS.SESSION],
24
- exact: false,
24
+ queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
25
25
  })
26
- if ("user" in data) {
27
- queryClient.setQueryData([QUERY_KEYS.CURRENT_ACCOUNT], data.user)
28
- } else {
29
- queryClient.resetQueries({
30
- queryKey: [QUERY_KEYS.CURRENT_ACCOUNT],
31
- })
32
- }
33
26
  },
34
27
  ...useMutationOptions,
35
28
  })
@@ -0,0 +1,45 @@
1
+ import { useCallback } from "react"
2
+ import { useGetSession } from "./useGetSession"
3
+ import { useSignOut } from "./useSignOut"
4
+
5
+ function useEnsureNoSessionAndFetchNonce() {
6
+ const { refetch: getSession } = useGetSession(undefined, {
7
+ enabled: false,
8
+ })
9
+ const { signOutAsync } = useSignOut()
10
+
11
+ const getAndValidateSession = useCallback(async () => {
12
+ const getSessionResult = await getSession()
13
+
14
+ // TODO: We should create a separate endpoint that will always return nonce
15
+ if (getSessionResult.error) {
16
+ throw new Error(
17
+ `Sign in error: Failed to fetch the current session: ${getSessionResult.error}`,
18
+ )
19
+ }
20
+ if (!getSessionResult.data) {
21
+ throw new Error(
22
+ "Sign in error: Failed to fetch the current session; no data returned.",
23
+ )
24
+ }
25
+
26
+ return getSessionResult.data
27
+ }, [getSession])
28
+
29
+ const ensureNoSessionAndFetchNonce = useCallback(async () => {
30
+ let getSessionResult = await getAndValidateSession()
31
+
32
+ if (!("nonce" in getSessionResult)) {
33
+ // If there is active session we are terminating it
34
+ await signOutAsync()
35
+ // Because we've signed out we can assume nonce will be returned here
36
+ getSessionResult = (await getAndValidateSession()) as { nonce: string }
37
+ }
38
+
39
+ return getSessionResult.nonce
40
+ }, [getAndValidateSession, signOutAsync])
41
+
42
+ return { ensureNoSessionAndFetchNonce }
43
+ }
44
+
45
+ export { useEnsureNoSessionAndFetchNonce }
@@ -1,19 +1,25 @@
1
- import { useQuery, skipToken } from "@tanstack/react-query"
1
+ import { useQuery, skipToken, UseBaseQueryOptions } from "@tanstack/react-query"
2
2
  import { useAuthApiClient } from "./useAuthApiClient"
3
3
  import { QUERY_KEYS } from "./constants"
4
4
  import { ONE_MINUTE_MS } from "../utils/time"
5
+ import { GetAccountByMezoIdOrAddressResponse } from "../api/auth"
5
6
 
6
- export function useGetAccountByAddress(address?: string, useQueryOptions = {}) {
7
+ export function useGetAccountByAddress(
8
+ address?: string,
9
+ queryOptions: Omit<
10
+ UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse>,
11
+ "queryKey" | "queryFn"
12
+ > = {},
13
+ ) {
7
14
  const authApiClient = useAuthApiClient()
8
15
 
9
16
  return useQuery({
10
- queryKey: [QUERY_KEYS.ACCOUNT_BY_ADDRESS, address],
17
+ queryKey: [QUERY_KEYS.ACCOUNT, address],
11
18
  queryFn: address
12
19
  ? () => authApiClient.getAccountByMezoIdOrAddress(address)
13
20
  : skipToken,
14
21
  staleTime: ONE_MINUTE_MS,
15
22
  retry: 1,
16
- enabled: !!address,
17
- ...useQueryOptions,
23
+ ...queryOptions,
18
24
  })
19
25
  }
@@ -1,19 +1,25 @@
1
- import { useQuery, skipToken } from "@tanstack/react-query"
1
+ import { useQuery, skipToken, UseBaseQueryOptions } from "@tanstack/react-query"
2
2
  import { useAuthApiClient } from "./useAuthApiClient"
3
3
  import { QUERY_KEYS } from "./constants"
4
4
  import { ONE_MINUTE_MS } from "../utils/time"
5
+ import { GetAccountByMezoIdOrAddressResponse } from "../api/auth"
5
6
 
6
- export function useGetAccountByMezoId(mezoId?: string, useQueryOptions = {}) {
7
+ export function useGetAccountByMezoId(
8
+ mezoId?: string,
9
+ queryOptions: Omit<
10
+ UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse>,
11
+ "queryKey" | "queryFn"
12
+ > = {},
13
+ ) {
7
14
  const authApiClient = useAuthApiClient()
8
15
 
9
16
  return useQuery({
10
- queryKey: [QUERY_KEYS.ACCOUNT_BY_MEZO_ID, mezoId],
17
+ queryKey: [QUERY_KEYS.ACCOUNT, mezoId],
11
18
  queryFn: mezoId
12
19
  ? () => authApiClient.getAccountByMezoIdOrAddress(mezoId)
13
20
  : skipToken,
14
21
  staleTime: ONE_MINUTE_MS,
15
22
  retry: 1,
16
- enabled: !!mezoId,
17
- ...useQueryOptions,
23
+ ...queryOptions,
18
24
  })
19
25
  }
@@ -1,16 +1,22 @@
1
- import { useQuery } from "@tanstack/react-query"
1
+ import { useQuery, UseBaseQueryOptions } from "@tanstack/react-query"
2
2
  import { useAuthApiClient } from "./useAuthApiClient"
3
3
  import { QUERY_KEYS } from "./constants"
4
4
  import { ONE_MINUTE_MS } from "../utils/time"
5
+ import { GetCurrentAccountResponse } from "../api/auth"
5
6
 
6
- export function useGetCurrentAccount(useQueryOptions = {}) {
7
+ export function useGetCurrentAccount(
8
+ queryOptions: Omit<
9
+ UseBaseQueryOptions<GetCurrentAccountResponse>,
10
+ "queryKey" | "queryFn"
11
+ > = {},
12
+ ) {
7
13
  const authApiClient = useAuthApiClient()
8
14
 
9
15
  return useQuery({
10
- queryKey: [QUERY_KEYS.CURRENT_ACCOUNT],
16
+ queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
11
17
  queryFn: () => authApiClient.getCurrentAccount(),
12
18
  staleTime: ONE_MINUTE_MS,
13
19
  retry: 1,
14
- ...useQueryOptions,
20
+ ...queryOptions,
15
21
  })
16
22
  }
@@ -1,15 +1,22 @@
1
- import { useQuery } from "@tanstack/react-query"
1
+ import { useQuery, UseBaseQueryOptions } from "@tanstack/react-query"
2
2
  import { useAuthApiClient } from "./useAuthApiClient"
3
3
  import { QUERY_KEYS } from "./constants"
4
4
  import { ONE_MINUTE_MS } from "../utils/time"
5
+ import { GetSessionResponse } from "../api/auth"
5
6
 
6
- export function useGetSession(code?: string, useQueryOptions = {}) {
7
+ export function useGetSession(
8
+ code?: string,
9
+ queryOptions: Omit<
10
+ UseBaseQueryOptions<GetSessionResponse>,
11
+ "queryKey" | "queryFn"
12
+ > = {},
13
+ ) {
7
14
  const authApiClient = useAuthApiClient()
8
15
  return useQuery({
9
16
  queryKey: [QUERY_KEYS.SESSION, code],
10
17
  queryFn: () => authApiClient.getSession(code),
11
18
  staleTime: ONE_MINUTE_MS,
12
19
  retry: 1,
13
- ...useQueryOptions,
20
+ ...queryOptions,
14
21
  })
15
22
  }
@@ -10,33 +10,8 @@ export function useLinkAccount(useMutationOptions = {}) {
10
10
  const { mutate, mutateAsync, ...rest } = useMutation({
11
11
  mutationFn: (linkAccountRequest: LinkAccountRequest) =>
12
12
  authApiClient.linkAccount(linkAccountRequest),
13
- onSuccess: (data) => {
14
- if ("user_metadata" in data) {
15
- if (data.user_metadata.mezoId) {
16
- queryClient.setQueryData(
17
- [QUERY_KEYS.ACCOUNT_BY_MEZO_ID, data.user_metadata.mezoId],
18
- data,
19
- )
20
- }
21
-
22
- queryClient.setQueryData(
23
- [QUERY_KEYS.ACCOUNT_BY_ADDRESS, data.user_metadata.btcAddress],
24
- data,
25
- )
26
- queryClient.setQueryData(
27
- [QUERY_KEYS.ACCOUNT_BY_ADDRESS, data.user_metadata.evmAddress],
28
- data,
29
- )
30
- } else {
31
- queryClient.resetQueries({
32
- queryKey: [QUERY_KEYS.ACCOUNT_BY_MEZO_ID],
33
- exact: false,
34
- })
35
- queryClient.resetQueries({
36
- queryKey: [QUERY_KEYS.ACCOUNT_BY_ADDRESS],
37
- exact: false,
38
- })
39
- }
13
+ onSuccess: () => {
14
+ queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
40
15
  },
41
16
  ...useMutationOptions,
42
17
  })
@@ -1,32 +1,14 @@
1
- import { useAccount } from "wagmi"
2
1
  import { useMutation } from "@tanstack/react-query"
3
- import { useGetSession } from "./useGetSession"
4
2
  import { useCreateSession } from "./useCreateSession"
3
+ import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
5
4
 
6
5
  function useSignInWithDiscord() {
7
- const { address } = useAccount()
8
- const { refetch: getSession } = useGetSession(undefined, {
9
- enabled: false,
10
- })
6
+ const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
11
7
  const { createSessionAsync } = useCreateSession()
12
8
 
13
9
  const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
14
10
  mutationFn: async () => {
15
- if (!address) {
16
- throw new Error("Sign in error: User not connected!")
17
- }
18
-
19
- const getSessionResult = await getSession()
20
-
21
- // TODO: We should create a separate endpoint that will always return nonce
22
- if (!getSessionResult.data || !("nonce" in getSessionResult.data)) {
23
- if (getSessionResult.error) {
24
- throw new Error(`Sign in error: ${getSessionResult.error}`)
25
- }
26
- throw new Error(
27
- "Sign in error: Nonce not available! Remove the session first.",
28
- )
29
- }
11
+ await ensureNoSessionAndFetchNonce()
30
12
 
31
13
  return createSessionAsync({
32
14
  type: "discord",
@@ -35,8 +17,8 @@ function useSignInWithDiscord() {
35
17
  })
36
18
 
37
19
  return {
38
- signIn: mutate,
39
- signInAsync: mutateAsync,
20
+ signInWithDiscord: mutate,
21
+ signInWithDiscordAsync: mutateAsync,
40
22
  ...signInMutationRestParameters,
41
23
  }
42
24
  }
@@ -1,42 +1,29 @@
1
1
  import { useAccount, useSignMessage } from "wagmi"
2
2
  import { useBitcoinAccount } from "@mezo-org/orangekit"
3
3
  import { useMutation } from "@tanstack/react-query"
4
- import { useGetSession } from "./useGetSession"
5
4
  import { useCreateSession } from "./useCreateSession"
6
5
  import { createSignInWithWalletMessage } from "../utils/siww"
6
+ import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
7
7
 
8
8
  function useSignInWithWallet() {
9
- console.log("use sign int with wallet broooo")
10
9
  const { chainId, address, connector } = useAccount()
11
10
  const { btcAddress } = useBitcoinAccount()
12
- const { refetch: getSession } = useGetSession(undefined, {
13
- enabled: false,
14
- })
11
+ const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
15
12
  const { createSessionAsync } = useCreateSession()
16
13
  const { signMessageAsync } = useSignMessage()
17
14
 
18
15
  const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
19
16
  mutationFn: async () => {
20
17
  if (!address) {
21
- throw new Error("Sign in error: User not connected!")
18
+ throw new Error("Sign in error: Wallet not connected!")
22
19
  }
23
20
 
24
- const getSessionResult = await getSession()
25
-
26
- // TODO: We should create a separate endpoint that will always return nonce
27
- if (!getSessionResult.data || !("nonce" in getSessionResult.data)) {
28
- if (getSessionResult.error) {
29
- throw new Error(`Sign in error: ${getSessionResult.error}`)
30
- }
31
- throw new Error(
32
- "Sign in error: Nonce not available! Remove the session first.",
33
- )
34
- }
21
+ const nonce = await ensureNoSessionAndFetchNonce()
35
22
 
36
23
  const networkFamily = btcAddress ? "bitcoin" : "evm"
37
24
  const messageResult = createSignInWithWalletMessage(
38
25
  btcAddress ? btcAddress! : address!,
39
- getSessionResult.data.nonce,
26
+ nonce,
40
27
  networkFamily,
41
28
  chainId,
42
29
  )
@@ -53,8 +40,8 @@ function useSignInWithWallet() {
53
40
  })
54
41
 
55
42
  return {
56
- signIn: mutate,
57
- signInAsync: mutateAsync,
43
+ signInWithWallet: mutate,
44
+ signInWithWalletAsync: mutateAsync,
58
45
  ...signInMutationRestParameters,
59
46
  }
60
47
  }
@@ -9,8 +9,10 @@ export function useSignOut(useMutationOptions = {}) {
9
9
  const { mutate, mutateAsync, ...rest } = useMutation({
10
10
  mutationFn: () => authApiClient.deleteSession(),
11
11
  onSuccess: () => {
12
- queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION], exact: false })
13
- queryClient.resetQueries({ queryKey: [QUERY_KEYS.CURRENT_ACCOUNT] })
12
+ queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION] })
13
+ queryClient.resetQueries({
14
+ queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
15
+ })
14
16
  },
15
17
  ...useMutationOptions,
16
18
  })
@@ -9,14 +9,7 @@ export function useUpdateMezoId(useMutationOptions = {}) {
9
9
  const { mutate, mutateAsync, ...rest } = useMutation({
10
10
  mutationFn: (newMezoId: string) => authApiClient.updateMezoId(newMezoId),
11
11
  onSuccess: () => {
12
- queryClient.resetQueries({
13
- queryKey: [QUERY_KEYS.ACCOUNT_BY_ADDRESS],
14
- exact: false,
15
- })
16
- queryClient.resetQueries({
17
- queryKey: [QUERY_KEYS.ACCOUNT_BY_MEZO_ID],
18
- exact: false,
19
- })
12
+ queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
20
13
  },
21
14
  ...useMutationOptions,
22
15
  })