Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lj/siwe #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"@fontsource/courier-prime": "^4.5.0",
"@fontsource/lato": "^4.5.0",
"@loadable/component": "^5.15.0",
"@rainbow-me/rainbowkit": "^0.4.6",
"@rainbow-me/rainbowkit": "^0.5.1",
"@rainbow-me/rainbowkit-siwe-next-auth": "^0.1.1",
"@react-three/drei": "^8.6.4",
"@react-three/fiber": "^7.0.25",
"@slack/web-api": "^6.5.1",
Expand All @@ -34,12 +35,13 @@
"datadog-winston": "^1.5.1",
"detect-gpu": "^4.0.2",
"dotenv": "^16.0.1",
"ethers": "^5.5.1",
"ethers": "^5.7.0",
"evm-translator": "^0.3.6",
"framer-motion": "^4",
"ioredis": "^4.27.9",
"ipfs-http-client": "50.1.2",
"next": "11.1.2",
"next-auth": "^4.10.3",
"node-fetch": "^3.1.0",
"node-fetch-retry": "^2.0.0",
"onoma": "^0.1.1",
Expand All @@ -48,10 +50,11 @@
"react": "17.0.2",
"react-countdown": "^2.3.2",
"react-dom": "17.0.2",
"siwe": "^1.1.6",
"styled-components": "^5.3.5",
"ua-parser-js": "^1.0.2",
"urlbox": "^1.3.3",
"wagmi": "^0.6.2",
"wagmi": "^0.6.5",
"web3": "^1.6.0",
"web3modal": "^1.9.4",
"winston": "^3.3.3"
Expand Down
62 changes: 34 additions & 28 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ChakraProvider, extendTheme, Flex } from '@chakra-ui/react';
import '@fontsource/arimo';
import { lightTheme, RainbowKitProvider } from '@rainbow-me/rainbowkit';
import { RainbowKitSiweNextAuthProvider } from '@rainbow-me/rainbowkit-siwe-next-auth';
import '@rainbow-me/rainbowkit/styles.css';
import { SessionProvider } from 'next-auth/react';
import type { AppProps } from 'next/app';
import dynamic from 'next/dynamic';
import { useRouter } from 'next/router';
Expand Down Expand Up @@ -30,35 +32,39 @@ function App({ Component, pageProps }: AppProps): JSX.Element {
return (
<ChakraProvider theme={theme}>
<WagmiConfig client={wagmiClient}>
<RainbowKitProvider
chains={chains}
theme={lightTheme({
accentColor: '#FDFFFF',
accentColorForeground: '#151515',
})}>
<Flex
// backgroundImage={leftBg.src}
// bgBlendMode="overlay"
// bgPosition={'left 0px top -70px'}
// bgSize={bgSize}
width="100%"
// bgRepeat="no-repeat repeat"
>
<Flex
// backgroundImage={rightBg.src}
width="100%"
// bgPosition={'right 0px top -70px'}
// bgSize={bgSize}
// bgRepeat="no-repeat repeat"
>
<Flex bgColor="brand.800" width="100%">
<Layout>
<Component {...pageProps} />
</Layout>
<SessionProvider session={pageProps.session} refetchInterval={0}>
<RainbowKitSiweNextAuthProvider>
<RainbowKitProvider
chains={chains}
theme={lightTheme({
accentColor: '#FDFFFF',
accentColorForeground: '#151515',
})}>
<Flex
// backgroundImage={leftBg.src}
// bgBlendMode="overlay"
// bgPosition={'left 0px top -70px'}
// bgSize={bgSize}
width="100%"
// bgRepeat="no-repeat repeat"
>
<Flex
// backgroundImage={rightBg.src}
width="100%"
// bgPosition={'right 0px top -70px'}
// bgSize={bgSize}
// bgRepeat="no-repeat repeat"
>
<Flex bgColor="brand.800" width="100%">
<Layout>
<Component {...pageProps} />
</Layout>
</Flex>
</Flex>
</Flex>
</Flex>
</Flex>
</RainbowKitProvider>
</RainbowKitProvider>
</RainbowKitSiweNextAuthProvider>
</SessionProvider>
</WagmiConfig>
</ChakraProvider>
);
Expand Down
94 changes: 94 additions & 0 deletions pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import axios from 'axios';
import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import { getCsrfToken } from 'next-auth/react';
import { SiweMessage } from 'siwe';

import { METABOT_BASE_API_URL } from '@utils/constants';
import { frontendSignature, signupSignature } from '@utils/index';

export default async function auth(req, res) {
const providers = [
CredentialsProvider({
name: 'Ethereum',
credentials: {
message: {
label: 'Message',
type: 'text',
placeholder: '0x0',
},
signature: {
label: 'Signature',
type: 'text',
placeholder: '0x0',
},
},
async authorize(credentials) {
try {
const siwe = new SiweMessage(JSON.parse(credentials?.message || '{}'));

const nextAuthUrl =
process.env.NEXTAUTH_URL ||
(process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : null);

if (!nextAuthUrl) {
return null;
}

const nextAuthHost = new URL(nextAuthUrl).host;
if (siwe.domain !== nextAuthHost) {
return null;
}

if (siwe.nonce !== (await getCsrfToken({ req }))) {
return null;
}

await siwe.validate(credentials?.signature || '');
return {
id: siwe.address,
};
} catch (e) {
return null;
}
},
}),
];

const isDefaultSigninPage = req.method === 'GET' && req.query.nextauth.includes('signin');

if (isDefaultSigninPage) {
providers.pop();
}

return await NextAuth(req, res, {
providers,
session: {
strategy: 'jwt',
},
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async session({ session, token }) {
const address = token.sub;
session.address = token.sub;

try {
const { data: user } = await axios.get(
`${METABOT_BASE_API_URL}user/${address}`,
{
headers: {
'x-signup-signature': signupSignature(address),
'x-frontend-signature': frontendSignature(address),
},
},
);
session.user = user;
} catch (err) {
res.status(500).json(err);
} finally {
return session;
}
},
},
});
}
Loading