nextjs redirect of route handling not working

23 Views Asked by At

hi guys I'm trying to redirect the user after loging in but nothing is happening

import { NextRequest, NextResponse } from 'next/server';
import jwt from 'jsonwebtoken';

export async function POST(request: NextRequest) {
  const incomingRequest = await request.json();

  if (incomingRequest.email != '[email protected]')
    return NextResponse.json(
      { message: 'email ou senha invalidos' },
      { status: 404 }
    );
  const redirectTo = request.cookies.get('redirectTo')?.value;

  const redirectURL = redirectTo ?? new URL('/login/agents', request.url);
  const token = jwt.sign({ name: 'usuario teste' }, 'dshnfdsçfjdsalf');

  const cookieExpiresInSeconds = 60 * 2;

  return NextResponse.redirect(redirectURL, {
    headers: {
      'Set-Cookie': `token=${token}; Path=/; max-age=${cookieExpiresInSeconds};`,
    },
  });
}

I also tried with redirect but nothing works, I have no idea why it's not working I took this code from a previus project I did a while ago

0

There are 0 best solutions below