Skip to content

Commit

Permalink
Merge pull request #72 from cardano2vn/fix-login
Browse files Browse the repository at this point in the history
update
  • Loading branch information
tidvn authored Nov 6, 2024
2 parents 229a720 + 934aee0 commit e29144b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 62 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"lucide-react": "^0.447.0",
"next": "^15.0.2",
"next-auth": "^5.0.0-beta.25",
"node-cache": "^5.1.2",
"prism-react-renderer": "^2.4.0",
"react": "^19.0.0-rc-fb9a90fa48-20240614",
"react-copy-to-clipboard": "^5.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- CreateTable
CREATE TABLE "wallet_nonce" (
"id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"address" TEXT NOT NULL,
"nonce" TEXT NOT NULL,

CONSTRAINT "wallet_nonce_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "wallet_nonce_address_key" ON "wallet_nonce"("address");
10 changes: 10 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ model User {
@@map(name: "user")
}

model WalletNonce {
id String @id @default(cuid())
createdAt DateTime @default(now()) @map(name: "created_at")
updatedAt DateTime @updatedAt @map(name: "updated_at")
address String @unique @map(name: "address")
nonce String
@@map(name: "wallet_nonce")
}

model Collection {
id String @id @default(cuid())
createdAt DateTime @default(now()) @map(name: "created_at")
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/use-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export const useWallet = create<useWalletStore>((set, get) => ({
}

if (isNil(session)) {
const nonce = await getNonceByAddress(address);
if (isNil(nonce) || nonce === "") {
throw new Error("Cant get nonce");
const { data, result, message } = await getNonceByAddress(address);
if (!result || isNil(data)) {
throw new Error(message);
}
const signature = await browserWallet.signData(nonce);
const signature = await browserWallet.signData(data);
if (isNil(signature)) {
throw new Error("Cant get signature");
}
Expand Down
13 changes: 0 additions & 13 deletions src/instrumentation.ts

This file was deleted.

23 changes: 12 additions & 11 deletions src/lib/auth/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { checkSignature, DataSignature } from "@meshsdk/core";
import { checkSignature, DataSignature, generateNonce } from "@meshsdk/core";
import { NextAuthConfig } from "next-auth";
import CredentialProvider from "next-auth/providers/credentials";
import prisma from "@/lib/prisma";
Expand All @@ -25,11 +25,15 @@ const authConfig = {
if (isNil(wallet) || isNil(address) || isNil(signature)) {
throw new Error("Invalid credentials");
}
const walletNonce = await global.cacheUser.get(`nonce-${address}`);
if (isNil(walletNonce)) {
const walletNonce = await prisma.walletNonce.findFirst({
where: {
address,
},
});
if (isNil(walletNonce) || isNil(walletNonce.nonce)) {
throw new Error("Nonce not found");
}
const isSignatureValid = checkSignature(walletNonce, signature);
const isSignatureValid = checkSignature(walletNonce.nonce, signature);

if (!isSignatureValid) {
throw new Error("Invalid signature");
Expand All @@ -53,18 +57,15 @@ const authConfig = {
],
callbacks: {
async signIn({ user }: any) {
await global.cacheUser.del(`nonce-${user.address}`);
const result = await prisma.user.upsert({
await prisma.walletNonce.update({
where: {
address: user.address,
},
create: {
address: user.address,
data: {
nonce: generateNonce("signin to cip68 nft "),
},
update: {},
});

return !isNil(result);
return true;
},
async redirect() {
return "/dashboard";
Expand Down
64 changes: 36 additions & 28 deletions src/services/auth/get-nonce.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
"use server";

import prisma from "@/lib/prisma";
import { generateNonce } from "@meshsdk/core";
import { isNil } from "lodash";

export const getNonceByAddress = async (address: string) => {
if (isNil(address)) {
throw new Error("Stake address is required");
}
try {
if (isNil(address)) {
throw new Error("Stake address is required");
}

if (!/^[a-z0-9_]+$/.test(address)) {
throw new Error("Invalid address");
}
if (!/^[a-z0-9_]+$/.test(address)) {
throw new Error("Invalid address");
}

// const nonce = generateNonce("signin to cip68 nft");
// const walletNonce = await prisma.walletNonce.upsert({
// where: {
// address: address,
// },
// create: {
// address: address,
// nonce: nonce,
// },
// update: {
// nonce: nonce,
// },
// });
const nonce = generateNonce("signin to cip68 nft");
const walletNonce = await prisma.walletNonce.upsert({
where: {
address: address,
},
create: {
address: address,
nonce: nonce,
},
update: {
nonce: nonce,
},
});
if (!walletNonce) {
throw new Error("Cannot get the nonce");
}

const nonce = await global.cacheUser.get(`nonce-${address}`);
if (nonce) {
return nonce;
}
const newNonce = generateNonce("signin to cip68 nft");
const setCache = await global.cacheUser.set(`nonce-${address}`, newNonce);
if (!setCache) {
throw new Error("Cannot get the nonce");
return {
data: nonce,
result: true,
message: "Nonce generated successfully",
};
} catch (e) {
return {
data: null,
result: false,
message: e instanceof Error ? e.message : "unknown error",
};
}
return newNonce;
};
5 changes: 0 additions & 5 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/no-empty-object-type */
/* eslint-disable no-var */
import { Icons } from "@/components/common/icons";
import { BrowserWallet } from "@meshsdk/core";
import { StaticImageData } from "next/image";
Expand All @@ -11,10 +10,6 @@ declare module "next-auth" {
wallet?: string;
}
}
declare global {
var cacheConfigs: NodeCache;
var cacheUser: NodeCache;
}

export type JsonValue = string | number | boolean | JsonObject | JsonArray;
export interface JsonObject {
Expand Down

0 comments on commit e29144b

Please sign in to comment.