@mezo-org/passport 0.4.0-dev.0 → 0.4.0-dev.1
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 +73 -0
- package/dist/src/api/auth.d.ts.map +1 -0
- package/dist/src/api/auth.js +108 -0
- package/dist/src/api/auth.js.map +1 -0
- package/dist/src/hooks/constants.d.ts +7 -0
- package/dist/src/hooks/constants.d.ts.map +1 -0
- package/dist/src/hooks/constants.js +7 -0
- package/dist/src/hooks/constants.js.map +1 -0
- package/dist/src/hooks/index.d.ts +13 -0
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +13 -0
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAuthApiClient.d.ts +2 -0
- package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -0
- package/dist/src/hooks/useAuthApiClient.js +6 -0
- package/dist/src/hooks/useAuthApiClient.js.map +1 -0
- package/dist/src/hooks/useCreateAccount.d.ts +204 -0
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -0
- package/dist/src/hooks/useCreateAccount.js +23 -0
- package/dist/src/hooks/useCreateAccount.js.map +1 -0
- package/dist/src/hooks/useCreateSession.d.ts +246 -0
- package/dist/src/hooks/useCreateSession.d.ts.map +1 -0
- package/dist/src/hooks/useCreateSession.js +35 -0
- package/dist/src/hooks/useCreateSession.js.map +1 -0
- package/dist/src/hooks/useGetAccountByAddress.d.ts +6 -0
- package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -0
- package/dist/src/hooks/useGetAccountByAddress.js +18 -0
- package/dist/src/hooks/useGetAccountByAddress.js.map +1 -0
- package/dist/src/hooks/useGetAccountByMezoId.d.ts +6 -0
- package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -0
- package/dist/src/hooks/useGetAccountByMezoId.js +18 -0
- package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -0
- package/dist/src/hooks/useGetCurrentAccount.d.ts +12 -0
- package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -0
- package/dist/src/hooks/useGetCurrentAccount.js +15 -0
- package/dist/src/hooks/useGetCurrentAccount.js.map +1 -0
- package/dist/src/hooks/useGetSession.d.ts +21 -0
- package/dist/src/hooks/useGetSession.d.ts.map +1 -0
- package/dist/src/hooks/useGetSession.js +15 -0
- package/dist/src/hooks/useGetSession.js.map +1 -0
- package/dist/src/hooks/useLinkAccount.d.ts +178 -0
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -0
- package/dist/src/hooks/useLinkAccount.js +32 -0
- package/dist/src/hooks/useLinkAccount.js.map +1 -0
- package/dist/src/hooks/usePassportContext.d.ts +3 -0
- package/dist/src/hooks/usePassportContext.d.ts.map +1 -0
- package/dist/src/hooks/usePassportContext.js +10 -0
- package/dist/src/hooks/usePassportContext.js.map +1 -0
- package/dist/src/hooks/useSignInWithDiscord.d.ts +242 -0
- package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -0
- package/dist/src/hooks/useSignInWithDiscord.js +36 -0
- package/dist/src/hooks/useSignInWithDiscord.js.map +1 -0
- package/dist/src/hooks/useSignInWithWallet.d.ts +242 -0
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useSignInWithWallet.js +48 -0
- package/dist/src/hooks/useSignInWithWallet.js.map +1 -0
- package/dist/src/hooks/useSignOut.d.ts +88 -0
- package/dist/src/hooks/useSignOut.d.ts.map +1 -0
- package/dist/src/hooks/useSignOut.js +17 -0
- package/dist/src/hooks/useSignOut.js.map +1 -0
- package/dist/src/hooks/useUpdateMezoId.d.ts +160 -0
- package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -0
- package/dist/src/hooks/useUpdateMezoId.js +23 -0
- package/dist/src/hooks/useUpdateMezoId.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/provider.d.ts +20 -0
- package/dist/src/provider.d.ts.map +1 -0
- package/dist/src/provider.js +25 -0
- package/dist/src/provider.js.map +1 -0
- package/dist/src/utils/siww.d.ts +4 -0
- package/dist/src/utils/siww.d.ts.map +1 -0
- package/dist/src/utils/siww.js +19 -0
- package/dist/src/utils/siww.js.map +1 -0
- package/dist/src/utils/time.d.ts +5 -0
- package/dist/src/utils/time.d.ts.map +1 -0
- package/dist/src/utils/time.js +5 -0
- package/dist/src/utils/time.js.map +1 -0
- package/package.json +7 -5
- package/src/api/auth.ts +203 -0
- package/src/hooks/constants.ts +6 -0
- package/src/hooks/index.ts +13 -0
- package/src/hooks/useAuthApiClient.ts +6 -0
- package/src/hooks/useCreateAccount.ts +27 -0
- package/src/hooks/useCreateSession.ts +38 -0
- package/src/hooks/useGetAccountByAddress.ts +19 -0
- package/src/hooks/useGetAccountByMezoId.ts +19 -0
- package/src/hooks/useGetCurrentAccount.ts +16 -0
- package/src/hooks/useGetSession.ts +15 -0
- package/src/hooks/useLinkAccount.ts +45 -0
- package/src/hooks/usePassportContext.ts +11 -0
- package/src/hooks/useSignInWithDiscord.ts +44 -0
- package/src/hooks/useSignInWithWallet.ts +61 -0
- package/src/hooks/useSignOut.ts +19 -0
- package/src/hooks/useUpdateMezoId.ts +25 -0
- package/src/index.ts +1 -0
- package/src/provider.ts +57 -0
- package/src/utils/siww.ts +31 -0
- package/src/utils/time.ts +4 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useQueryClient, useMutation } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { CreateSessionRequest } from "../api/auth"
|
|
4
|
+
import { QUERY_KEYS } from "./constants"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* This hook is not exposed for external consumers. For creating session they
|
|
8
|
+
* should use `useSignIn` hook related to the authentication method.
|
|
9
|
+
*/
|
|
10
|
+
export function useCreateSession(useMutationOptions = {}) {
|
|
11
|
+
const queryClient = useQueryClient()
|
|
12
|
+
const authApiClient = useAuthApiClient()
|
|
13
|
+
|
|
14
|
+
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
15
|
+
mutationFn: (createSessionRequest: CreateSessionRequest) =>
|
|
16
|
+
authApiClient.createSession(createSessionRequest),
|
|
17
|
+
onSuccess: (data) => {
|
|
18
|
+
// Since `getSession` query also has code in it's query keys we can't
|
|
19
|
+
// set query data for a specific query, because we don't know if the
|
|
20
|
+
// code was used or not. Because of that we just reset all getSession
|
|
21
|
+
// queries
|
|
22
|
+
queryClient.resetQueries({
|
|
23
|
+
queryKey: [QUERY_KEYS.SESSION],
|
|
24
|
+
exact: false,
|
|
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
|
+
},
|
|
34
|
+
...useMutationOptions,
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
return { createSession: mutate, createSessionAsync: mutateAsync, ...rest }
|
|
38
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useQuery, skipToken } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
|
|
6
|
+
export function useGetAccountByAddress(address?: string, useQueryOptions = {}) {
|
|
7
|
+
const authApiClient = useAuthApiClient()
|
|
8
|
+
|
|
9
|
+
return useQuery({
|
|
10
|
+
queryKey: [QUERY_KEYS.ACCOUNT_BY_ADDRESS, address],
|
|
11
|
+
queryFn: address
|
|
12
|
+
? () => authApiClient.getAccountByMezoIdOrAddress(address)
|
|
13
|
+
: skipToken,
|
|
14
|
+
staleTime: ONE_MINUTE_MS,
|
|
15
|
+
retry: 1,
|
|
16
|
+
enabled: !!address,
|
|
17
|
+
...useQueryOptions,
|
|
18
|
+
})
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useQuery, skipToken } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
|
|
6
|
+
export function useGetAccountByMezoId(mezoId?: string, useQueryOptions = {}) {
|
|
7
|
+
const authApiClient = useAuthApiClient()
|
|
8
|
+
|
|
9
|
+
return useQuery({
|
|
10
|
+
queryKey: [QUERY_KEYS.ACCOUNT_BY_MEZO_ID, mezoId],
|
|
11
|
+
queryFn: mezoId
|
|
12
|
+
? () => authApiClient.getAccountByMezoIdOrAddress(mezoId)
|
|
13
|
+
: skipToken,
|
|
14
|
+
staleTime: ONE_MINUTE_MS,
|
|
15
|
+
retry: 1,
|
|
16
|
+
enabled: !!mezoId,
|
|
17
|
+
...useQueryOptions,
|
|
18
|
+
})
|
|
19
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useQuery } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
|
|
6
|
+
export function useGetCurrentAccount(useQueryOptions = {}) {
|
|
7
|
+
const authApiClient = useAuthApiClient()
|
|
8
|
+
|
|
9
|
+
return useQuery({
|
|
10
|
+
queryKey: [QUERY_KEYS.CURRENT_ACCOUNT],
|
|
11
|
+
queryFn: () => authApiClient.getCurrentAccount(),
|
|
12
|
+
staleTime: ONE_MINUTE_MS,
|
|
13
|
+
retry: 1,
|
|
14
|
+
...useQueryOptions,
|
|
15
|
+
})
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useQuery } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
|
|
6
|
+
export function useGetSession(code?: string, useQueryOptions = {}) {
|
|
7
|
+
const authApiClient = useAuthApiClient()
|
|
8
|
+
return useQuery({
|
|
9
|
+
queryKey: [QUERY_KEYS.SESSION, code],
|
|
10
|
+
queryFn: () => authApiClient.getSession(code),
|
|
11
|
+
staleTime: ONE_MINUTE_MS,
|
|
12
|
+
retry: 1,
|
|
13
|
+
...useQueryOptions,
|
|
14
|
+
})
|
|
15
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useQueryClient, useMutation } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { LinkAccountRequest } from "../api/auth"
|
|
4
|
+
import { QUERY_KEYS } from "./constants"
|
|
5
|
+
|
|
6
|
+
export function useLinkAccount(useMutationOptions = {}) {
|
|
7
|
+
const queryClient = useQueryClient()
|
|
8
|
+
const authApiClient = useAuthApiClient()
|
|
9
|
+
|
|
10
|
+
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
11
|
+
mutationFn: (linkAccountRequest: LinkAccountRequest) =>
|
|
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
|
+
}
|
|
40
|
+
},
|
|
41
|
+
...useMutationOptions,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
return { linkAccount: mutate, linkAccountAsync: mutateAsync, ...rest }
|
|
45
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useContext } from "react"
|
|
2
|
+
import { PassportContext, PassportContextValue } from "../provider"
|
|
3
|
+
|
|
4
|
+
export function usePassportContext(): PassportContextValue {
|
|
5
|
+
const passportContext = useContext(PassportContext)
|
|
6
|
+
if (!passportContext) {
|
|
7
|
+
throw new Error("usePassportContext must be used within PassportProvider!")
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return passportContext
|
|
11
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useAccount } from "wagmi"
|
|
2
|
+
import { useMutation } from "@tanstack/react-query"
|
|
3
|
+
import { useGetSession } from "./useGetSession"
|
|
4
|
+
import { useCreateSession } from "./useCreateSession"
|
|
5
|
+
|
|
6
|
+
function useSignInWithDiscord() {
|
|
7
|
+
const { address } = useAccount()
|
|
8
|
+
const { refetch: getSession } = useGetSession(undefined, {
|
|
9
|
+
enabled: false,
|
|
10
|
+
})
|
|
11
|
+
const { createSessionAsync } = useCreateSession()
|
|
12
|
+
|
|
13
|
+
const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
|
|
14
|
+
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
|
+
}
|
|
30
|
+
|
|
31
|
+
return createSessionAsync({
|
|
32
|
+
type: "discord",
|
|
33
|
+
})
|
|
34
|
+
},
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
signIn: mutate,
|
|
39
|
+
signInAsync: mutateAsync,
|
|
40
|
+
...signInMutationRestParameters,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { useSignInWithDiscord }
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { useAccount, useSignMessage } from "wagmi"
|
|
2
|
+
import { useBitcoinAccount } from "@mezo-org/orangekit"
|
|
3
|
+
import { useMutation } from "@tanstack/react-query"
|
|
4
|
+
import { useGetSession } from "./useGetSession"
|
|
5
|
+
import { useCreateSession } from "./useCreateSession"
|
|
6
|
+
import { createSignInWithWalletMessage } from "../utils/siww"
|
|
7
|
+
|
|
8
|
+
function useSignInWithWallet() {
|
|
9
|
+
const { chainId, address, connector } = useAccount()
|
|
10
|
+
const { btcAddress } = useBitcoinAccount()
|
|
11
|
+
const { refetch: getSession } = useGetSession(undefined, {
|
|
12
|
+
enabled: false,
|
|
13
|
+
})
|
|
14
|
+
const { createSessionAsync } = useCreateSession()
|
|
15
|
+
const { signMessageAsync } = useSignMessage()
|
|
16
|
+
|
|
17
|
+
const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
|
|
18
|
+
mutationFn: async () => {
|
|
19
|
+
if (!address) {
|
|
20
|
+
throw new Error("Sign in error: User not connected!")
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const getSessionResult = await getSession()
|
|
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
|
+
}
|
|
34
|
+
|
|
35
|
+
const networkFamily = btcAddress ? "bitcoin" : "evm"
|
|
36
|
+
const messageResult = createSignInWithWalletMessage(
|
|
37
|
+
btcAddress ? btcAddress! : address!,
|
|
38
|
+
getSessionResult.data.nonce,
|
|
39
|
+
networkFamily,
|
|
40
|
+
chainId,
|
|
41
|
+
)
|
|
42
|
+
const signatureResult = await signMessageAsync({
|
|
43
|
+
message: messageResult,
|
|
44
|
+
connector,
|
|
45
|
+
})
|
|
46
|
+
return createSessionAsync({
|
|
47
|
+
type: "wallet",
|
|
48
|
+
message: messageResult,
|
|
49
|
+
signature: signatureResult,
|
|
50
|
+
})
|
|
51
|
+
},
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
signIn: mutate,
|
|
56
|
+
signInAsync: mutateAsync,
|
|
57
|
+
...signInMutationRestParameters,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { useSignInWithWallet }
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
|
|
5
|
+
export function useSignOut(useMutationOptions = {}) {
|
|
6
|
+
const queryClient = useQueryClient()
|
|
7
|
+
const authApiClient = useAuthApiClient()
|
|
8
|
+
|
|
9
|
+
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
10
|
+
mutationFn: () => authApiClient.deleteSession(),
|
|
11
|
+
onSuccess: () => {
|
|
12
|
+
queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION], exact: false })
|
|
13
|
+
queryClient.resetQueries({ queryKey: [QUERY_KEYS.CURRENT_ACCOUNT] })
|
|
14
|
+
},
|
|
15
|
+
...useMutationOptions,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
return { signOut: mutate, signOutAsync: mutateAsync, ...rest }
|
|
19
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useQueryClient, useMutation } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
|
|
5
|
+
export function useUpdateMezoId(useMutationOptions = {}) {
|
|
6
|
+
const queryClient = useQueryClient()
|
|
7
|
+
const authApiClient = useAuthApiClient()
|
|
8
|
+
|
|
9
|
+
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
10
|
+
mutationFn: (newMezoId: string) => authApiClient.updateMezoId(newMezoId),
|
|
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
|
+
})
|
|
20
|
+
},
|
|
21
|
+
...useMutationOptions,
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
return { updateMezoId: mutate, updateMezoIdAsync: mutateAsync, ...rest }
|
|
25
|
+
}
|
package/src/index.ts
CHANGED
package/src/provider.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createContext, createElement, useMemo } from "react"
|
|
2
|
+
import { API_ENDPOINTS_BY_ENV, AuthApiClient } from "./api/auth"
|
|
3
|
+
|
|
4
|
+
export interface PassportContextValue {
|
|
5
|
+
authApiClient: AuthApiClient
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const PassportContext = createContext<PassportContextValue | undefined>(
|
|
9
|
+
undefined,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
interface PassportProviderProps {
|
|
13
|
+
// eslint-disable-next-line react/require-default-props
|
|
14
|
+
options: {
|
|
15
|
+
environment?: keyof typeof API_ENDPOINTS_BY_ENV
|
|
16
|
+
apiUrl?: string
|
|
17
|
+
} & (
|
|
18
|
+
| {
|
|
19
|
+
environment: keyof typeof API_ENDPOINTS_BY_ENV
|
|
20
|
+
}
|
|
21
|
+
| {
|
|
22
|
+
apiUrl: string
|
|
23
|
+
}
|
|
24
|
+
)
|
|
25
|
+
children: React.ReactNode
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function PassportProvider({ options, children }: PassportProviderProps) {
|
|
29
|
+
const { environment, apiUrl } = options
|
|
30
|
+
|
|
31
|
+
const finalApiUrl = useMemo(() => {
|
|
32
|
+
if (!environment && !apiUrl) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
"Neither apiUrl or environment are specified in Passport Provider",
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
if (apiUrl) return apiUrl
|
|
38
|
+
|
|
39
|
+
if (!(environment! in API_ENDPOINTS_BY_ENV)) {
|
|
40
|
+
throw new Error("Wrong environment passed to PassportProvider.")
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return API_ENDPOINTS_BY_ENV[environment!]
|
|
44
|
+
}, [environment, apiUrl])
|
|
45
|
+
|
|
46
|
+
return createElement(
|
|
47
|
+
PassportContext.Provider,
|
|
48
|
+
{
|
|
49
|
+
value: finalApiUrl
|
|
50
|
+
? {
|
|
51
|
+
authApiClient: new AuthApiClient(finalApiUrl),
|
|
52
|
+
}
|
|
53
|
+
: undefined,
|
|
54
|
+
},
|
|
55
|
+
children,
|
|
56
|
+
)
|
|
57
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SignInWithWalletMessage } from "@mezo-org/sign-in-with-wallet"
|
|
2
|
+
import { ONE_DAY_MS } from "./time"
|
|
3
|
+
|
|
4
|
+
const SESSION_EXPIRATION_DURATION_MS = 14 * ONE_DAY_MS
|
|
5
|
+
|
|
6
|
+
type NetworkFamily = "evm" | "bitcoin"
|
|
7
|
+
|
|
8
|
+
export function createSignInWithWalletMessage(
|
|
9
|
+
address: string,
|
|
10
|
+
nonce: string,
|
|
11
|
+
networkFamily: NetworkFamily,
|
|
12
|
+
chainId?: number,
|
|
13
|
+
) {
|
|
14
|
+
const { host: domain, origin: uri } = window.location
|
|
15
|
+
|
|
16
|
+
const message = new SignInWithWalletMessage({
|
|
17
|
+
domain,
|
|
18
|
+
address, // if "bitcoin" this should be btc address, not underlaying eth address
|
|
19
|
+
uri,
|
|
20
|
+
nonce,
|
|
21
|
+
issuedAt: new Date().toISOString(),
|
|
22
|
+
expirationTime: new Date(
|
|
23
|
+
Date.now() + SESSION_EXPIRATION_DURATION_MS,
|
|
24
|
+
).toISOString(),
|
|
25
|
+
version: "1",
|
|
26
|
+
chainId: networkFamily === "evm" ? chainId : undefined,
|
|
27
|
+
networkFamily,
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
return message.prepareMessage()
|
|
31
|
+
}
|