-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(farcaster): init draft for supporting farcaster login
- Loading branch information
1 parent
868e6fc
commit 32a0f06
Showing
18 changed files
with
629 additions
and
6 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,5 @@ | ||
import { defineConfig } from 'father'; | ||
|
||
export default defineConfig({ | ||
extends: '../../.fatherrc.base.ts', | ||
}); |
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 @@ | ||
# TODO |
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,61 @@ | ||
{ | ||
"name": "@ant-design/web3-farcaster", | ||
"version": "1.0.0", | ||
"main": "dist/lib/index.js", | ||
"module": "dist/esm/index.js", | ||
"typings": "dist/esm/index.d.ts", | ||
"exports": { | ||
"import": "./dist/esm/index.js", | ||
"require": "./dist/lib/index.js", | ||
"types": "./dist/esm/index.d.ts" | ||
}, | ||
"sideEffects": false, | ||
"files": [ | ||
"dist", | ||
"CHANGELOG.md", | ||
"README.md" | ||
], | ||
"keywords": [ | ||
"ant", | ||
"component", | ||
"components", | ||
"design", | ||
"framework", | ||
"frontend", | ||
"react", | ||
"react-component", | ||
"ui", | ||
"web3", | ||
"farcaster" | ||
], | ||
"homepage": "https://web3.ant.design", | ||
"bugs": { | ||
"url": "https://github.com/ant-design/ant-design-web3/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ant-design/ant-design-web3" | ||
}, | ||
"scripts": { | ||
"dev": "father dev", | ||
"build": "father build" | ||
}, | ||
"dependencies": { | ||
"@farcaster/auth-client": "^0.1.1", | ||
"@farcaster/auth-kit": "^0.3.1" | ||
}, | ||
"devDependencies": { | ||
"father": "^4.4.4", | ||
"typescript": "^5.4.5" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org", | ||
"access": "public" | ||
}, | ||
"browserslist": [ | ||
"last 2 versions", | ||
"Firefox ESR", | ||
"> 1%", | ||
"ie >= 11" | ||
] | ||
} |
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,2 @@ | ||
export * from './provider'; | ||
export { QRCode } from '@farcaster/auth-kit'; |
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 type { FC } from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
type RenderResult = ReturnType<typeof render>; | ||
type RenderWithUtils = RenderResult & { | ||
selector: <T extends Element = Element>(selector: string) => T | null; | ||
selectors: <T extends Element = Element>(selector: string) => NodeListOf<T>; | ||
}; | ||
type XRender = (Comp: FC, options?: Parameters<typeof render>[1]) => RenderWithUtils; | ||
|
||
export const xrender: XRender = (Comp, options) => { | ||
const { baseElement, ...others } = render(<Comp />, options); | ||
return { | ||
baseElement, | ||
...others, | ||
selector: (selector) => baseElement.querySelector(selector), | ||
selectors: (selector) => baseElement.querySelectorAll(selector), | ||
}; | ||
}; |
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,72 @@ | ||
import React, { useCallback, useEffect } from 'react'; | ||
import type { Provider } from '@farcaster/auth-client'; | ||
import { AuthKitProvider, useSignIn, type UseSignInArgs } from '@farcaster/auth-kit'; | ||
|
||
import '@farcaster/auth-kit/styles.css'; | ||
|
||
// declares locally in '@farcaster/auth-kit', but it is not exported | ||
interface AuthKitConfig { | ||
relay?: string; | ||
domain?: string; | ||
siweUri?: string; | ||
rpcUrl?: string; | ||
redirectUrl?: string; | ||
version?: string; | ||
provider?: Provider; | ||
} | ||
|
||
interface IFarcasterContext extends Partial<ReturnType<typeof useSignIn>> { | ||
farcasterSupported: boolean; | ||
farcasterLogin: () => void; | ||
} | ||
|
||
const FarcasterContext = React.createContext<IFarcasterContext>({ | ||
farcasterSupported: false, | ||
farcasterLogin: () => {}, | ||
}); | ||
|
||
const FarcasterConfigProvider: React.FC<React.PropsWithChildren<UseSignInArgs>> = ({ | ||
children, | ||
...signInArgs | ||
}) => { | ||
const signInState = useSignIn(signInArgs); | ||
const { isError, reconnect, signIn, channelToken, connect } = signInState; | ||
|
||
const farcasterLogin = useCallback(() => { | ||
if (isError) { | ||
reconnect(); | ||
} | ||
signIn(); | ||
}, [isError, reconnect, signIn]); | ||
|
||
useEffect(() => { | ||
if (!channelToken) { | ||
connect(); | ||
} | ||
}, [channelToken, connect]); | ||
|
||
return ( | ||
<FarcasterContext.Provider value={{ farcasterSupported: true, farcasterLogin, ...signInState }}> | ||
{children} | ||
</FarcasterContext.Provider> | ||
); | ||
}; | ||
|
||
interface Web3FarcasterProviderProps extends UseSignInArgs { | ||
config?: AuthKitConfig; | ||
} | ||
|
||
export const useFarcaster = () => { | ||
const farcaster = React.useContext(FarcasterContext); | ||
return farcaster; | ||
}; | ||
|
||
export const FarcasterWeb3ConfigProvider: React.FC< | ||
React.PropsWithChildren<Web3FarcasterProviderProps> | ||
> = ({ children, config = {}, ...signInArgs }) => { | ||
return ( | ||
<AuthKitProvider config={config}> | ||
<FarcasterConfigProvider {...signInArgs}>{children}</FarcasterConfigProvider> | ||
</AuthKitProvider> | ||
); | ||
}; |
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 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": ["src", "global.d.ts"] | ||
} |
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
33 changes: 33 additions & 0 deletions
33
packages/web3/src/connect-modal/components/FarcasterCard.tsx
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,33 @@ | ||
import React from 'react'; | ||
import { QRCode, useFarcaster } from '@ant-design/web3-farcaster'; | ||
|
||
import { connectModalContext } from '../context'; | ||
import MainPanelHeader from './MainPanelHeader'; | ||
|
||
const FarcasterCard = () => { | ||
const { url, error, isError } = useFarcaster(); | ||
const { prefixCls } = React.useContext(connectModalContext); | ||
|
||
return ( | ||
<> | ||
<div className={`${prefixCls}-qr-code-container`}> | ||
<MainPanelHeader title="Sign in with Farcaster" /> | ||
<div className={`${prefixCls}-qr-code-box`}> | ||
{isError ? ( | ||
<> | ||
<div>Error</div> | ||
<div>{error?.message ?? 'Unknown error, please try again.'}</div> | ||
</> | ||
) : url ? ( | ||
<QRCode uri={url} size={300} logoSize={28} logoMargin={16} /> | ||
) : null} | ||
</div> | ||
<div className={`${prefixCls}-qr-code-tips`}> | ||
<div>Scan with your phone's camera to continue.</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default FarcasterCard; |
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
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
Oops, something went wrong.