Rainbow logo
RainbowKit
2.2.10

사용자 정의 인증

사용자 정의 인증

자신의 인증 백엔드에 연결

RainbowKit은 Ethereum 및 NextAuth와의 첫 번째 클래스 지원을 제공하지만, 맞춤형 백엔드 및 메시지 형식과 통합할 수도 있습니다.

먼저 인증 어댑터를 만듭니다. 이것은 RainbowKit이 메시지를 생성/준비하고 백엔드와 통신할 수 있게 해줍니다.

예를 들어, Sign-In with Ethereum을 커스텀 API 엔드포인트와 연동하는 인증 어댑터를 만들 수 있습니다. 예: iron-session.

import { createAuthenticationAdapter } from '@rainbow-me/rainbowkit';
import { createSiweMessage } from 'viem/siwe';
const authenticationAdapter = createAuthenticationAdapter({
getNonce: async () => {
const response = await fetch('/api/nonce');
return await response.text();
},
createMessage: ({ nonce, address, chainId }) => {
return createSiweMessage({
domain: window.location.host,
address,
statement: 'Sign in with Ethereum to the app.',
uri: window.location.origin,
version: '1',
chainId,
nonce,
});
},
verify: async ({ message, signature }) => {
const verifyRes = await fetch('/api/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message, signature }),
});
return Boolean(verifyRes.ok);
},
signOut: async () => {
await fetch('/api/logout');
},
});

귀하의 애플리케이션이 이미 인증 라이프사이클을 관리하고 있다고 가정할 때, 현재 인증 상태를 사용자의 맞춤 어댑터와 함께 RainbowKitAuthenticationProvider에 전달하여 기존의 RainbowKitProvider를 래핑할 수 있습니다.

import { createAuthenticationAdapter, RainbowKitAuthenticationProvider, RainbowKitProvider, } from '@rainbow-me/rainbowkit';
import { AppProps } from 'next/app';
import { WagmiProvider } from 'wagmi';
import { QueryClientProvider, QueryClient, } from "@tanstack/react-query";
const authenticationAdapter = createAuthenticationAdapter({
/* See above... */
});
const queryClient = new QueryClient();
export default function App({ Component, pageProps }: AppProps) {
// You'll need to resolve AUTHENTICATION_STATUS here
// using your application's authentication system.
// It needs to be either 'loading' (during initial load),
// 'unauthenticated' or 'authenticated'.
return (
<WagmiProvider {...etc}>
<QueryClientProvider client={queryClient}>
<RainbowKitAuthenticationProvider adapter={authenticationAdapter} status={AUTHENTICATION_STATUS} >
<RainbowKitProvider {...etc}>
<Component {...pageProps} />
</RainbowKitProvider>
</RainbowKitAuthenticationProvider>
</QueryClientProvider>
</WagmiProvider>
);
}

여기까지 진행하시고 기존의 오픈 소스 인증 라이브러리를 위한 어댑터를 만드셨다면, 다른 사람들도 사용할 수 있도록 패키지를 만들어 보세요!