fix(auth): pass userId through signup verification flow
Signup redirect now includes userId in the query string so the verify-email endpoint receives it. After successful verification, the endpoint creates a local DB user via ensureUserExists() so the account is ready for login.
This commit is contained in:
parent
feb7dc2643
commit
4fc952cddd
@ -1,6 +1,7 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server"
|
import { NextRequest, NextResponse } from "next/server"
|
||||||
import { getWorkOS } from "@workos-inc/authkit-nextjs"
|
import { getWorkOS } from "@workos-inc/authkit-nextjs"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
import { ensureUserExists } from "@/lib/auth"
|
||||||
|
|
||||||
const verifyEmailSchema = z.object({
|
const verifyEmailSchema = z.object({
|
||||||
code: z.string().min(1, "Verification code is required"),
|
code: z.string().min(1, "Verification code is required"),
|
||||||
@ -51,6 +52,15 @@ export async function POST(request: NextRequest) {
|
|||||||
const workos = getWorkOS()
|
const workos = getWorkOS()
|
||||||
await workos.userManagement.verifyEmail({ userId, code })
|
await workos.userManagement.verifyEmail({ userId, code })
|
||||||
|
|
||||||
|
const user = await workos.userManagement.getUser(userId)
|
||||||
|
await ensureUserExists({
|
||||||
|
id: user.id,
|
||||||
|
email: user.email,
|
||||||
|
firstName: user.firstName,
|
||||||
|
lastName: user.lastName,
|
||||||
|
profilePictureUrl: user.profilePictureUrl,
|
||||||
|
})
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: "Email verified successfully",
|
message: "Email verified successfully",
|
||||||
|
|||||||
@ -45,11 +45,14 @@ export function SignupForm() {
|
|||||||
success: boolean
|
success: boolean
|
||||||
message?: string
|
message?: string
|
||||||
error?: string
|
error?: string
|
||||||
|
userId?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
toast.success(result.message || "Account created!")
|
toast.success(result.message || "Account created!")
|
||||||
router.push("/verify-email?email=" + encodeURIComponent(data.email))
|
router.push(
|
||||||
|
`/verify-email?email=${encodeURIComponent(data.email)}&userId=${encodeURIComponent(result.userId ?? "")}`
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
toast.error(result.error || "Signup failed")
|
toast.error(result.error || "Signup failed")
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user