Web Developer
Hey everyone, I am trying to implement Magic URL into my NextJS project. I am having serious difficulty and I don't know what to do anymore.
In my api/ folder I have a function that looks like this:
export async function getMagicUrlToken(): Promise<Models.Token | Error> { try { return await account.createMagicURLToken( ID.unique(), 'alexappleget2014@gmail.com', 'http://localhost:3000/auth/magicUrl', ); } catch (error) { console.error(error); throw new Error('Error creating token'); }}This function is then imported into my App/Login folder inside the page.tsx file. For simple testing i made a simple button:
import {getMagicUrlToken} from '@/api/apiFunctions';
<button onClick={getMagicUrlToken}>Test Magic Url</button>Up to this point it works fine. I click the button and get the email with the link.
Since in my api function i put 'http://localhost:3000/auth/magicUrl' I went into my App/Auth/ folder and made another magicUrl/ folder with a file named route.ts. This is to handle the email link. Here is my code:
export async function GET(request: Request): Promise<Response> { try { const requestUrl = new URL(request.url);
const userId = requestUrl.searchParams.get('userId'); const secret = requestUrl.searchParams.get('secret');
if (!userId || !secret) { return new NextResponse('Invalid URL parameters', { status: 400 }); }
const session = await account.updateMagicURLSession(userId, secret);
return NextResponse.redirect(requestUrl.origin); } catch (error) { console.error('Error processing request:', error); return new NextResponse( JSON.stringify({ error: 'Internal Server Error.' }), { status: 500 }, ); }}