Skip to content

Commit cd4518c

Browse files
authored
Merge pull request #102 from MeshJS/API
split auth signer
2 parents 89427fc + 0e2a3ad commit cd4518c

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

src/pages/api/v1/authSigner.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
2-
import { randomBytes } from "crypto";
32
import { db } from "@/server/db";
43
import { sign } from "jsonwebtoken";
54
import {
65
checkSignature,
76
DataSignature,
8-
deserializeAddress,
97
} from "@meshsdk/core";
108
import { cors } from "@/lib/cors";
119

@@ -18,33 +16,6 @@ export default async function handler(
1816
return res.status(200).end();
1917
}
2018

21-
if (req.method === "GET") {
22-
const { address } = req.query;
23-
if (typeof address !== "string") {
24-
return res.status(400).json({ error: "Invalid address" });
25-
}
26-
// Verify that the address exists in the User table
27-
const user = await db.user.findUnique({ where: { address } });
28-
if (!user) {
29-
return res.status(404).json({ error: "Address not found" });
30-
}
31-
32-
// Check if a nonce already exists for this address in the database
33-
const existing = await db.nonce.findFirst({ where: { address } });
34-
if (existing) {
35-
return res.status(200).json({ nonce: existing.value });
36-
}
37-
38-
const nonce = randomBytes(16).toString("hex");
39-
await db.nonce.create({
40-
data: {
41-
address,
42-
value: nonce,
43-
},
44-
});
45-
return res.status(200).json({ nonce });
46-
}
47-
4819
if (req.method === "POST") {
4920
const { address, signature, key } = req.body;
5021
if (

src/pages/api/v1/getNonce.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { NextApiRequest, NextApiResponse } from "next";
2+
import { randomBytes } from "crypto";
3+
import { db } from "@/server/db";
4+
import { cors } from "@/lib/cors";
5+
6+
export default async function handler(
7+
req: NextApiRequest,
8+
res: NextApiResponse,
9+
) {
10+
await cors(req, res);
11+
if (req.method === "OPTIONS") {
12+
return res.status(200).end();
13+
}
14+
15+
if (req.method === "GET") {
16+
const { address } = req.query;
17+
if (typeof address !== "string") {
18+
return res.status(400).json({ error: "Invalid address" });
19+
}
20+
// Verify that the address exists in the User table
21+
const user = await db.user.findUnique({ where: { address } });
22+
if (!user) {
23+
return res.status(404).json({ error: "Address not found" });
24+
}
25+
26+
// Check if a nonce already exists for this address in the database
27+
const existing = await db.nonce.findFirst({ where: { address } });
28+
if (existing) {
29+
return res.status(200).json({ nonce: existing.value });
30+
}
31+
32+
const nonce = randomBytes(16).toString("hex");
33+
await db.nonce.create({
34+
data: {
35+
address,
36+
value: nonce,
37+
},
38+
});
39+
return res.status(200).json({ nonce });
40+
}
41+
42+
res.status(405).end();
43+
}

src/utils/swagger.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ security: [
233233
}
234234
}
235235
},
236-
"/api/v1/authSigner": {
236+
"/api/v1/getNonce": {
237237
get: {
238238
tags: ["Auth"],
239239
summary: "Request nonce for address-based authentication",
@@ -262,8 +262,13 @@ security: [
262262
400: {
263263
description: "Invalid address",
264264
},
265+
404: {
266+
description: "Address not found",
267+
},
265268
},
266269
},
270+
},
271+
"/api/v1/authSigner": {
267272
post: {
268273
tags: ["Auth"],
269274
summary: "Verify signed nonce and return bearer token",

0 commit comments

Comments
 (0)