Skip to content

Commit

Permalink
feat: eip-4361
Browse files Browse the repository at this point in the history
close #56
  • Loading branch information
pociej committed May 14, 2024
1 parent 85c5ee2 commit e463638
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 59 deletions.
23 changes: 17 additions & 6 deletions frontend/src/components/providers/blockchainProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const wagmiConfig = defaultWagmiConfig({
});

function createMessage({ nonce, address, chainId }: any) {
console.log("createMessage", nonce, address, chainId);
console.log("goubg to call constructor");
const message = new SiweMessage({
version: "1",
domain: window.location.host,
Expand All @@ -38,9 +36,6 @@ function createMessage({ nonce, address, chainId }: any) {
statement: "Sign in with ethereum",
});

console.log("after constructor");

console.log("message", message);
return message.prepareMessage();
}

Expand All @@ -63,7 +58,6 @@ const siweConfig = createSIWEConfig({
}
const responseData = await response.json();

console.log("nonce response", responseData.nonce.toString());
return responseData.nonce.toString();
},
getSession: async () => {
Expand All @@ -73,6 +67,23 @@ const siweConfig = createSIWEConfig({
};
},
verifyMessage: async ({ message, signature }) => {
const res = await fetch(`${import.meta.env.VITE_BACKEND_URL}/login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message, signature }),
});

if (!res.ok) {
throw new Error(`Error logging in: ${res.statusText}`);
}

const data = await res.json();

localStorage.setItem("accessToken", data.accessToken);
localStorage.setItem("refreshToken", data.refreshToken);

return true;
},
signOut: async () => {
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/components/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ export const Register = () => {
variants={variants}
transition={{ duration: 0.5 }}
>
{user.isConnected() && address && (
<RegisterButton
isVisible={!user.isRegistered() && user.isConnected()}
/>
)}
{user.isRegistered() && (
<RegisterSummary isVisible={user.isRegistered()} />
)}
Expand Down
77 changes: 29 additions & 48 deletions loginWithCrypto/src/fastify/routes/login.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,57 @@
import { RouteOptions } from "fastify";
import { verifyMessage } from "../../utils/verifyMessage.js";
import config from "../../utils/ensureEnv.js";

import { recoverMessageAddress } from "viem";
export const login: RouteOptions = {
method: "POST",
url: "/login",
schema: {
body: {
type: "object",
properties: {
walletAddress: {
type: "string",
pattern: "^0x[a-fA-F0-9]{40}$",
},
messageSignature: {
signature: {
type: "string",
},
message: {
type: "string",
},
},
required: ["walletAddress", "messageSignature", "message"],
required: ["signature", "message"],
},
},
handler: async (req, res) => {
//@ts-ignore
const { walletAddress, messageSignature, message } = req.body;
const { signature, message } = req.body;
// @ts-ignore temporary
const { userService } = req.routeOptions.config;
const isOk = await verifyMessage({
address: walletAddress,
signature: messageSignature,
message: message,
const walletAddress = await recoverMessageAddress({
message,
signature,
});

console.log("is ok", isOk);
if (isOk) {
const user = await userService.findByWalletAddress(walletAddress);
if (!user) {
res.status(401).send({ message: "Invalid signature" });
return;
}
userService.regenerateNonce(user.id);

console.log("user", user);
console.log("walletAddress", walletAddress);
const user = await userService.findByWalletAddress(walletAddress);

console.log(
"config",
config.JWT_SECRET,
config.JWT_TOKEN_EXPIRATION,
config.JWT_REFRESH_TOKEN_EXPIRATION
);

const tokens = {
accessToken: await res.jwtSign(
{ _id: user._id },
{
expiresIn: config.JWT_TOKEN_EXPIRATION,
}
),
refreshToken: await res.jwtSign(
{ _id: user._id },
{
expiresIn: config.JWT_REFRESH_TOKEN_EXPIRATION,
}
),
};
console.log("tokens", tokens);

res.send(tokens);
} else {
if (!user) {
res.status(401).send({ message: "Invalid signature" });
return;
}

userService.regenerateNonce(user.id);

const tokens = {
accessToken: await res.jwtSign(
{ _id: user._id },
{
expiresIn: config.JWT_TOKEN_EXPIRATION,
}
),
refreshToken: await res.jwtSign(
{ _id: user._id },
{
expiresIn: config.JWT_REFRESH_TOKEN_EXPIRATION,
}
),
};
res.send(tokens);
},
};

0 comments on commit e463638

Please sign in to comment.