File tree Expand file tree Collapse file tree 3 files changed +49
-30
lines changed
Expand file tree Collapse file tree 3 files changed +49
-30
lines changed Original file line number Diff line number Diff line change 11import type { NextApiRequest , NextApiResponse } from "next" ;
2- import { randomBytes } from "crypto" ;
32import { db } from "@/server/db" ;
43import { sign } from "jsonwebtoken" ;
54import {
65 checkSignature ,
76 DataSignature ,
8- deserializeAddress ,
97} from "@meshsdk/core" ;
108import { 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 (
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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" ,
You can’t perform that action at this time.
0 commit comments