@mezo-org/passport 0.4.0-dev.1 → 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.
- package/dist/src/api/auth.d.ts +43 -15
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/api/auth.js +23 -26
- package/dist/src/api/auth.js.map +1 -1
- package/dist/src/hooks/constants.d.ts +2 -3
- package/dist/src/hooks/constants.d.ts.map +1 -1
- package/dist/src/hooks/constants.js +3 -4
- package/dist/src/hooks/constants.js.map +1 -1
- package/dist/src/hooks/index.d.ts +0 -3
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +0 -3
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useCreateAccount.d.ts +126 -72
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +1 -6
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useCreateSession.d.ts +45 -126
- package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
- package/dist/src/hooks/useCreateSession.js +3 -11
- package/dist/src/hooks/useCreateSession.js.map +1 -1
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts +5 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +33 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -0
- package/dist/src/hooks/useGetAccountByAddress.d.ts +3 -5
- package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByAddress.js +3 -4
- package/dist/src/hooks/useGetAccountByAddress.js.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.d.ts +3 -5
- package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.js +3 -4
- package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.d.ts +3 -11
- package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.js +3 -3
- package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
- package/dist/src/hooks/useGetSession.d.ts +3 -20
- package/dist/src/hooks/useGetSession.d.ts.map +1 -1
- package/dist/src/hooks/useGetSession.js +2 -2
- package/dist/src/hooks/useGetSession.js.map +1 -1
- package/dist/src/hooks/useLinkAccount.d.ts +126 -72
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +2 -18
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- package/dist/src/hooks/useSignInWithDiscord.d.ts +53 -134
- package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithDiscord.js +5 -19
- package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.d.ts +53 -134
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.js +7 -16
- package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
- package/dist/src/hooks/useSignOut.d.ts.map +1 -1
- package/dist/src/hooks/useSignOut.js +4 -2
- package/dist/src/hooks/useSignOut.js.map +1 -1
- package/dist/src/hooks/useUpdateMezoId.d.ts +153 -99
- package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useUpdateMezoId.js +1 -8
- package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
- package/package.json +4 -2
- package/src/api/auth.ts +84 -52
- package/src/hooks/constants.ts +3 -4
- package/src/hooks/index.ts +0 -3
- package/src/hooks/useCreateAccount.ts +1 -6
- package/src/hooks/useCreateSession.ts +3 -10
- package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +45 -0
- package/src/hooks/useGetAccountByAddress.ts +11 -5
- package/src/hooks/useGetAccountByMezoId.ts +11 -5
- package/src/hooks/useGetCurrentAccount.ts +10 -4
- package/src/hooks/useGetSession.ts +10 -3
- package/src/hooks/useLinkAccount.ts +2 -27
- package/src/hooks/useSignInWithDiscord.ts +5 -23
- package/src/hooks/useSignInWithWallet.ts +7 -19
- package/src/hooks/useSignOut.ts +4 -2
- 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.
|
|
3
|
+
"version": "0.4.0-dev.4",
|
|
4
4
|
"main": "dist/src/index.js",
|
|
5
5
|
"license": "GPL-3.0-only",
|
|
6
6
|
"scripts": {
|
|
@@ -37,9 +37,11 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@mezo-org/orangekit": "1.0.0-beta.40-dev.0",
|
|
40
|
+
"@mezo-org/sign-in-with-wallet": "1.0.0-beta.8"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
40
43
|
"@rainbow-me/rainbowkit": "2.0.2",
|
|
41
44
|
"@tanstack/react-query": "^5.28.4",
|
|
42
|
-
"@mezo-org/sign-in-with-wallet": "1.0.0-beta.8",
|
|
43
45
|
"react": "^18.2.0",
|
|
44
46
|
"viem": "2.22.8",
|
|
45
47
|
"wagmi": "2.5.12"
|
package/src/api/auth.ts
CHANGED
|
@@ -10,7 +10,8 @@ type AccountPublicData = {
|
|
|
10
10
|
evmAddress: string
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
type
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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 ??
|
|
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
|
|
69
|
-
*
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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<
|
|
124
|
-
"
|
|
125
|
-
{
|
|
126
|
-
|
|
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<
|
|
158
|
-
|
|
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<
|
|
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>(
|
|
197
|
-
|
|
198
|
-
|
|
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
|
}
|
package/src/hooks/constants.ts
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -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.
|
|
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: (
|
|
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.
|
|
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(
|
|
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.
|
|
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
|
-
|
|
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(
|
|
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.
|
|
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
|
-
|
|
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(
|
|
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.
|
|
16
|
+
queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
|
|
11
17
|
queryFn: () => authApiClient.getCurrentAccount(),
|
|
12
18
|
staleTime: ONE_MINUTE_MS,
|
|
13
19
|
retry: 1,
|
|
14
|
-
...
|
|
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(
|
|
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
|
-
...
|
|
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: (
|
|
14
|
-
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
39
|
-
|
|
20
|
+
signInWithDiscord: mutate,
|
|
21
|
+
signInWithDiscordAsync: mutateAsync,
|
|
40
22
|
...signInMutationRestParameters,
|
|
41
23
|
}
|
|
42
24
|
}
|
|
@@ -1,41 +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
9
|
const { chainId, address, connector } = useAccount()
|
|
10
10
|
const { btcAddress } = useBitcoinAccount()
|
|
11
|
-
const {
|
|
12
|
-
enabled: false,
|
|
13
|
-
})
|
|
11
|
+
const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
|
|
14
12
|
const { createSessionAsync } = useCreateSession()
|
|
15
13
|
const { signMessageAsync } = useSignMessage()
|
|
16
14
|
|
|
17
15
|
const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
|
|
18
16
|
mutationFn: async () => {
|
|
19
17
|
if (!address) {
|
|
20
|
-
throw new Error("Sign in error:
|
|
18
|
+
throw new Error("Sign in error: Wallet not connected!")
|
|
21
19
|
}
|
|
22
20
|
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
// TODO: We should create a separate endpoint that will always return nonce
|
|
26
|
-
if (!getSessionResult.data || !("nonce" in getSessionResult.data)) {
|
|
27
|
-
if (getSessionResult.error) {
|
|
28
|
-
throw new Error(`Sign in error: ${getSessionResult.error}`)
|
|
29
|
-
}
|
|
30
|
-
throw new Error(
|
|
31
|
-
"Sign in error: Nonce not available! Remove the session first.",
|
|
32
|
-
)
|
|
33
|
-
}
|
|
21
|
+
const nonce = await ensureNoSessionAndFetchNonce()
|
|
34
22
|
|
|
35
23
|
const networkFamily = btcAddress ? "bitcoin" : "evm"
|
|
36
24
|
const messageResult = createSignInWithWalletMessage(
|
|
37
25
|
btcAddress ? btcAddress! : address!,
|
|
38
|
-
|
|
26
|
+
nonce,
|
|
39
27
|
networkFamily,
|
|
40
28
|
chainId,
|
|
41
29
|
)
|
|
@@ -52,8 +40,8 @@ function useSignInWithWallet() {
|
|
|
52
40
|
})
|
|
53
41
|
|
|
54
42
|
return {
|
|
55
|
-
|
|
56
|
-
|
|
43
|
+
signInWithWallet: mutate,
|
|
44
|
+
signInWithWalletAsync: mutateAsync,
|
|
57
45
|
...signInMutationRestParameters,
|
|
58
46
|
}
|
|
59
47
|
}
|
package/src/hooks/useSignOut.ts
CHANGED
|
@@ -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]
|
|
13
|
-
queryClient.resetQueries({
|
|
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
|
})
|