-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from MinaFoundation/feature/wallet-user-and-ac…
…count-linking Feature/wallet user and account linking
- Loading branch information
Showing
29 changed files
with
1,398 additions
and
277 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { NextResponse } from "next/server"; | ||
import { verifyToken } from "@/lib/auth/jwt"; | ||
import prisma from "@/lib/prisma"; | ||
import logger from "@/logging"; | ||
import { AppError } from "@/lib/errors"; | ||
import { ApiResponse } from "@/lib/api-response"; | ||
import { deriveUserId } from "@/lib/user/derive"; | ||
import { UserService } from "@/services/UserService"; | ||
|
||
const userService = new UserService(prisma); | ||
|
||
export const runtime = "nodejs"; | ||
|
||
interface LinkAccountRequest { | ||
walletToken: string; | ||
existingToken: string; | ||
} | ||
|
||
export async function POST(request: Request) { | ||
try { | ||
const body: LinkAccountRequest = await request.json(); | ||
const { walletToken, existingToken } = body; | ||
|
||
if (!walletToken || !existingToken) { | ||
throw new AppError("Missing required tokens", 400); | ||
} | ||
|
||
// Verify both tokens | ||
const [walletPayload, existingPayload] = await Promise.all([ | ||
verifyToken(walletToken), | ||
verifyToken(existingToken), | ||
]); | ||
|
||
if (walletPayload.authSource.type !== "wallet") { | ||
throw new AppError("Invalid wallet token", 400); | ||
} | ||
|
||
// Derive user IDs from auth sources | ||
const walletUserId = deriveUserId(walletPayload.authSource); | ||
const existingUserId = deriveUserId(existingPayload.authSource); | ||
|
||
// Check if linking is possible | ||
const canLink = await userService.canLink(walletUserId, existingUserId); | ||
if (!canLink) { | ||
throw new AppError("Cannot link these accounts", 400); | ||
} | ||
|
||
// Link the accounts | ||
const linked = await userService.linkAccounts(walletUserId, existingUserId); | ||
if (!linked) { | ||
throw new AppError("Failed to link accounts", 400); | ||
} | ||
|
||
return ApiResponse.success({ | ||
data: { message: "Accounts linked successfully" } | ||
}); | ||
|
||
} catch (error) { | ||
logger.error("Account linking error:", error); | ||
|
||
if (error instanceof AppError) { | ||
return ApiResponse.error(error); | ||
} | ||
|
||
return ApiResponse.error({ | ||
message: "Failed to link accounts", | ||
statusCode: 500 | ||
}); | ||
} | ||
} |
Oops, something went wrong.