Back

Email Verification Confirmation Flow

  • 0
  • Auth
  • Web
olalexy1
14 Jun, 2024, 07:00

Please, I need help creating a session in this route so that my app can redirect to the dashboard after a successful verification confirmation process. Code below

``

import { type NextRequest, NextResponse } from "next/server"; import { createUserEmailVerificationConfirmation } from "@/lib/actions/user.actions"; import { cookies } from "next/headers";

// Creating a handler to a GET request to route /auth/verify-email export async function GET(request: NextRequest) { const { searchParams } = new URL(request.url); const userId = searchParams.get("userId"); const secret = searchParams.get("secret"); const next = "/dashboard";

// Create redirect link without the secret token const redirectTo = request.nextUrl.clone(); redirectTo.pathname = next; redirectTo.searchParams.delete("userId"); redirectTo.searchParams.delete("secret");

if (userId && secret) { const response = await createUserEmailVerificationConfirmation({ userId, secret, });

TypeScript
console.log(response, "see response");

if (response) {
  redirectTo.searchParams.delete("next");

  // Create a NextResponse object for setting cookies
  const redirectResponse = NextResponse.redirect(redirectTo);

  //Intended to create a session here

  redirectResponse.cookies.set("session", `session value here`, {
    path: "/",
    httpOnly: true,
    sameSite: "strict",
    secure: true,
  });

  return redirectResponse;
}

}

// return the user to an error page with some instructions redirectTo.pathname = "/error"; return NextResponse.redirect(redirectTo); }

``

After signing up, a verification link is sent, which works fine; it takes the user to the route above. However, I need a session to redirect to the protected dashboard. What is the appropriate flow to handle this as I don't have the email or password in this route?

TL;DR
Developers are looking to set up a session for redirecting to the dashboard after email verification. To achieve this, after successful verification, create a session using NextResponse to set a cookie with session details. Ensure the cookie has these parameters: path: "/", httpOnly: true, sameSite: "strict", secure: true.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more