1- import {
2- BadRequestException ,
3- Body ,
4- Controller ,
5- Get ,
6- Post ,
7- Req ,
8- Res ,
9- } from '@nestjs/common' ;
10- import { MiniAppWalletAuthSuccessPayload } from '@worldcoin/minikit-js' ;
1+ import { Body , Controller , Get , Post , Req , Res } from '@nestjs/common' ;
112import { Request , Response } from 'express' ;
123import { AuthService } from './auth.service' ;
13-
14- interface IRequestPayload {
15- payload : MiniAppWalletAuthSuccessPayload ;
16- }
17-
18- type RequestWithCookies = Request & {
19- cookies : {
20- siwe ?: string ;
21- } ;
22- } ;
4+ import { JwtService } from './jwt.service' ;
5+ import { Public } from './jwt-auth.guard' ;
6+ import { VerifyWorldIdDto } from './auth.dto' ;
7+ import { SignatureVerificationFailureException } from 'src/common/exceptions' ;
8+ import { BadRequestException } from '@nestjs/common' ;
239
2410function isHttps ( req : Request ) {
2511 return (
@@ -29,8 +15,12 @@ function isHttps(req: Request) {
2915
3016@Controller ( 'auth' )
3117export class AuthController {
32- constructor ( private readonly authService : AuthService ) { }
18+ constructor (
19+ private readonly authService : AuthService ,
20+ private readonly jwtService : JwtService ,
21+ ) { }
3322
23+ @Public ( )
3424 @Get ( 'nonce' )
3525 generateNonce ( @Req ( ) req : Request , @Res ( ) res : Response ) {
3626 const nonce = this . authService . generateNonce ( ) ;
@@ -41,20 +31,45 @@ export class AuthController {
4131 maxAge : 2 * 60 * 1000 , //2 minutes
4232 } ) ;
4333
44- return { nonce } ;
34+ return res . json ( { nonce } ) ;
4535 }
4636
47- @Post ( 'verifyPayload' )
48- async verifyPayload (
49- @Req ( ) req : RequestWithCookies ,
50- @Body ( ) body : IRequestPayload ,
37+ @Public ( )
38+ @Post ( 'verifyWorldId' )
39+ async verifyWorldId (
40+ @Req ( ) _req : Request ,
41+ @Body ( ) body : VerifyWorldIdDto ,
42+ @Res ( ) res : Response ,
5143 ) {
52- const { payload } = body ;
53- const storedNonce = req . cookies . siwe ;
54- if ( ! storedNonce ) {
55- throw new BadRequestException ( 'No nonce found in cookies' ) ;
44+ const { walletPayload, worldIdProof, nonce } = body ;
45+
46+ try {
47+ const isValid = await this . authService . verifyPayload (
48+ walletPayload ,
49+ nonce ,
50+ ) ;
51+
52+ if ( ! isValid ) {
53+ throw new SignatureVerificationFailureException ( ) ;
54+ }
55+
56+ const worldID = worldIdProof ?. nullifier_hash ;
57+ const walletAddress = walletPayload ?. address ;
58+
59+ const user = await this . authService . createUser ( worldID , '' ) ;
60+
61+ const token = this . jwtService . sign ( {
62+ userId : user . id ,
63+ worldID,
64+ address : walletAddress ,
65+ } ) ;
66+
67+ return res . status ( 200 ) . json ( { isValid : true , token } ) ;
68+ } catch ( error ) {
69+ console . error ( error ) ;
70+ throw new BadRequestException (
71+ error instanceof Error ? error . message : 'Unknown error' ,
72+ ) ;
5673 }
57- const isValid = await this . authService . verifyPayload ( payload , storedNonce ) ;
58- return { isValid } ;
5974 }
6075}
0 commit comments