-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add social validator hook * update social hook * lint fixes * fix pending state * update packages * Clean up hooks for rendering issues * chore: Update package version
- Loading branch information
1 parent
a96f7c4
commit 7fd0b64
Showing
9 changed files
with
291 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"editor.tabSize": 4, | ||
"editor.insertSpaces": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { type UseMutationResult, useMutation } from "@tanstack/react-query" | ||
import { | ||
type KernelSmartAccount, | ||
type KernelValidator, | ||
createKernelAccount | ||
} from "@zerodev/sdk" | ||
import { | ||
getSocialValidator, | ||
initiateLogin, | ||
isAuthorized | ||
} from "@zerodev/social-validator" | ||
import type { EntryPoint } from "permissionless/types" | ||
import { useCallback, useEffect, useState } from "react" | ||
import type { PublicClient } from "viem" | ||
import { useSocial } from "../providers/SocialContext" | ||
import { useZeroDevConfig } from "../providers/ZeroDevAppContext" | ||
import { useSetKernelAccount } from "../providers/ZeroDevValidatorContext" | ||
import type { KernelVersionType } from "../types" | ||
import { getEntryPointFromVersion } from "../utils/entryPoint" | ||
|
||
export type UseCreateKernelClientSocialParameters = { | ||
version: KernelVersionType | ||
oauthCallbackUrl?: string | ||
} | ||
|
||
export type UseCreateKernelClientSocialKey = { | ||
version: KernelVersionType | ||
oauthCallbackUrl?: string | ||
publicClient?: PublicClient | ||
appId?: string | ||
type?: "getSocialValidator" | ||
} | ||
|
||
export type CreateKernelClientSocialReturnType = { | ||
validator: KernelValidator<EntryPoint> | ||
kernelAccount: KernelSmartAccount<EntryPoint> | ||
entryPoint: EntryPoint | ||
} | ||
|
||
export type UseCreateKernelClientSocialReturnType = { | ||
login: (socialProvider: "google" | "facebook") => void | ||
} & Omit< | ||
UseMutationResult< | ||
CreateKernelClientSocialReturnType, | ||
unknown, | ||
UseCreateKernelClientSocialKey, | ||
unknown | ||
>, | ||
"mutate" | ||
> | ||
|
||
function mutationKey(config: UseCreateKernelClientSocialKey) { | ||
const { oauthCallbackUrl, publicClient, appId } = config | ||
|
||
return [ | ||
{ | ||
entity: "CreateKernelClient", | ||
oauthCallbackUrl, | ||
publicClient, | ||
appId | ||
} | ||
] as const | ||
} | ||
|
||
async function mutationFn( | ||
config: UseCreateKernelClientSocialKey | ||
): Promise<CreateKernelClientSocialReturnType> { | ||
const { publicClient, appId, version, type } = config | ||
|
||
if (!appId || !(await isAuthorized({ projectId: appId }))) { | ||
throw new Error("Not authorized") | ||
} | ||
|
||
if (!publicClient || !appId) { | ||
throw new Error("missing publicClient or appId") | ||
} | ||
let socialValidator: KernelValidator<EntryPoint> | ||
const entryPoint = getEntryPointFromVersion(version) | ||
|
||
if (type === "getSocialValidator") { | ||
socialValidator = await getSocialValidator(publicClient, { | ||
entryPoint, | ||
projectId: appId | ||
}) | ||
} else { | ||
throw new Error("invalid type") | ||
} | ||
|
||
const kernelAccount = await createKernelAccount(publicClient, { | ||
entryPoint: entryPoint, | ||
plugins: { | ||
sudo: socialValidator | ||
} | ||
}) | ||
|
||
return { validator: socialValidator, kernelAccount, entryPoint } | ||
} | ||
|
||
export function useCreateKernelClientSocial({ | ||
version, | ||
oauthCallbackUrl | ||
}: UseCreateKernelClientSocialParameters): UseCreateKernelClientSocialReturnType { | ||
const { | ||
setValidator, | ||
setKernelAccount, | ||
setEntryPoint, | ||
setKernelAccountClient | ||
} = useSetKernelAccount() | ||
const { appId, client } = useZeroDevConfig() | ||
const { | ||
setIsSocialPending, | ||
isSocialPending, | ||
login: loginSocial | ||
} = useSocial() | ||
|
||
const { data, mutate, ...result } = useMutation({ | ||
mutationKey: mutationKey({ | ||
appId: appId ?? undefined, | ||
publicClient: client ?? undefined, | ||
type: undefined, | ||
version, | ||
oauthCallbackUrl | ||
}), | ||
mutationFn, | ||
onSuccess: (data) => { | ||
setValidator(data.validator) | ||
setKernelAccount(data.kernelAccount) | ||
setEntryPoint(data.entryPoint) | ||
setKernelAccountClient(null) | ||
}, | ||
onMutate() { | ||
setIsSocialPending(true) | ||
}, | ||
onSettled() { | ||
setIsSocialPending(false) | ||
} | ||
}) | ||
|
||
const login = useCallback( | ||
(socialProvider: "google" | "facebook") => { | ||
loginSocial(socialProvider, oauthCallbackUrl) | ||
}, | ||
[oauthCallbackUrl, loginSocial] | ||
) | ||
|
||
useEffect(() => { | ||
const load = async () => { | ||
mutate({ | ||
appId: appId ?? undefined, | ||
publicClient: client ?? undefined, | ||
version, | ||
type: "getSocialValidator", | ||
oauthCallbackUrl | ||
}) | ||
} | ||
load() | ||
}, [appId, mutate, client, version, oauthCallbackUrl]) | ||
|
||
return { | ||
...result, | ||
data, | ||
isPending: isSocialPending, | ||
login | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { logout } from "@zerodev/social-validator" | ||
import { useMemo } from "react" | ||
import { useZeroDevConfig } from "../providers/ZeroDevAppContext" | ||
import { useKernelAccount } from "../providers/ZeroDevValidatorContext" | ||
|
||
export function useDisconnectSocial() { | ||
const { validator } = useKernelAccount() | ||
const { appId } = useZeroDevConfig() | ||
|
||
const logoutSocial = useMemo(() => { | ||
return async () => { | ||
if (validator?.source === "SocialValidator" && appId) { | ||
await logout({ projectId: appId }) | ||
} | ||
} | ||
}, [validator, appId]) | ||
|
||
return { logoutSocial } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
getSocialValidator, | ||
initiateLogin, | ||
isAuthorized | ||
} from "@zerodev/social-validator" | ||
import { | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState | ||
} from "react" | ||
import { useCreateKernelClientSocial } from "../hooks/useCreateKernelClientSocial" | ||
import { useZeroDevConfig } from "./ZeroDevAppContext" | ||
|
||
interface SocialContextValue { | ||
isSocialPending: boolean | ||
setIsSocialPending: (isPending: boolean) => void | ||
login: ( | ||
socialProvider: "google" | "facebook", | ||
oauthCallbackUrl?: string | ||
) => void | ||
} | ||
|
||
interface SocialProviderProps { | ||
children: React.ReactNode | ||
} | ||
|
||
export const SocialContext = createContext<SocialContextValue>({ | ||
isSocialPending: false, | ||
setIsSocialPending: () => {}, | ||
login: () => {} | ||
}) | ||
|
||
export function SocialProvider({ children }: SocialProviderProps) { | ||
const { appId } = useZeroDevConfig() | ||
const [isSocialPending, setIsSocialPending] = useState(false) | ||
|
||
const login = useCallback( | ||
(socialProvider: "google" | "facebook", oauthCallbackUrl?: string) => { | ||
if (!appId) { | ||
throw new Error("missing appId") | ||
} | ||
initiateLogin({ | ||
socialProvider, | ||
oauthCallbackUrl, | ||
projectId: appId | ||
}) | ||
}, | ||
[appId] | ||
) | ||
|
||
return ( | ||
<SocialContext.Provider | ||
value={useMemo( | ||
() => ({ | ||
isSocialPending, | ||
setIsSocialPending, | ||
login | ||
}), | ||
[isSocialPending, login] | ||
)} | ||
> | ||
{children} | ||
</SocialContext.Provider> | ||
) | ||
} | ||
|
||
export function useSocial() { | ||
const context = useContext(SocialContext) | ||
|
||
if (context === undefined) { | ||
throw new Error("useSocial must be used within a SocialProvider") | ||
} | ||
|
||
return context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters