Redirect is not working in server. Error: NEXT_REDIRECT (v14)
Unanswered
Giant Angora posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
import { auth } from "@/firebase";
import { signInWithEmailAndPassword } from "firebase/auth";
import { NextApiResponse } from "next";
import { redirect } from "next/navigation";
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest, res: NextApiResponse) {
try {
const data = await req.json();
const { email, password } = data;
// // Start: Firebase action
// const userCredentials = await signInWithEmailAndPassword(
// auth,
// email,
// password
// );
// const user = userCredentials.user;
// console.log(user)
// End: Firebase action
redirect('/feed')
} catch (error: any) {
return NextResponse.json(
{
error: error.message,
},
{
status: 500,
}
);
}
}
import { signInWithEmailAndPassword } from "firebase/auth";
import { NextApiResponse } from "next";
import { redirect } from "next/navigation";
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest, res: NextApiResponse) {
try {
const data = await req.json();
const { email, password } = data;
// // Start: Firebase action
// const userCredentials = await signInWithEmailAndPassword(
// auth,
// email,
// password
// );
// const user = userCredentials.user;
// console.log(user)
// End: Firebase action
redirect('/feed')
} catch (error: any) {
return NextResponse.json(
{
error: error.message,
},
{
status: 500,
}
);
}
}
51 Replies
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/c78dd8e985ede1ad50c31ac682dd91a1.webp?size=256)
risky
redirect works by throwing special error, so you either have to rethrow it or not have it in try catch
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
can you code that plz
@risky
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
import { auth } from "@/firebase";
import { signInWithEmailAndPassword } from "firebase/auth";
import { NextApiResponse } from "next";
import { redirect } from "next/navigation";
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest, res: NextApiResponse) {
try {
const data = await req.json();
const { email, password } = data;
// // Start: Firebase action
// const userCredentials = await signInWithEmailAndPassword(
// auth,
// email,
// password
// );
// const user = userCredentials.user;
// console.log(user)
// End: Firebase action
redirect("/feed");
} catch (error) {
if (error instanceof Error) {
if (error.message === "NEXT_REDIRECT") throw error;
return NextResponse.json(
{
error: error.message,
},
{
status: 500,
}
);
} else if (typeof error === "string") {
return NextResponse.json(
{
error,
},
{
status: 500,
}
);
}
}
}
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
@Ray it should redirect to "/feed". Rather it is successfully logging in but not redirecting
![Image](https://cdn.discordapp.com/attachments/1189872406274265199/1189952104811479171/Screenshot_2023-12-28_at_8.54.23_PM.png?ex=65a00842&is=658d9342&hm=a3c5f33959ae0aab714de7953edc2123b8f171667a4f4d1189d5c2164bf1e72b&)
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
oh didn't notice thats a route handler
import { auth } from "@/firebase";
import { signInWithEmailAndPassword } from "firebase/auth";
import { NextApiResponse } from "next";
import { redirect } from "next/navigation";
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest, res: NextApiResponse) {
try {
const data = await req.json();
const { email, password } = data;
// // Start: Firebase action
// const userCredentials = await signInWithEmailAndPassword(
// auth,
// email,
// password
// );
// const user = userCredentials.user;
// console.log(user)
// End: Firebase action
return NextResponse.redirect("/feed", {status: 302});
} catch (error) {
if (error instanceof Error) {
return NextResponse.json(
{
error: error.message,
},
{
status: 500,
}
);
} else if (typeof error === "string") {
return NextResponse.json(
{
error,
},
{
status: 500,
}
);
}
}
}
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
@Ray
![Image](https://cdn.discordapp.com/attachments/1189872406274265199/1189955757635022950/Screenshot_2023-12-28_at_9.09.10_PM.png?ex=65a00ba8&is=658d96a8&hm=8d2d1ffa05dcc7bdb911108ac40237f44cbb209565ea8490957337147cf849b7&)
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
return NextResponse.redirect(new URL("/feed", req.nextUrl), {status: 302});
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
@Ray Still the same, you can see the netwrok tab, redirect to feed..... But in browser it is not redirecting
![Image](https://cdn.discordapp.com/attachments/1189872406274265199/1189956204080930896/Screenshot_2023-12-28_at_9.10.21_PM.png?ex=65a00c13&is=658d9713&hm=95e7fcad676a6f5c022d6bd69b6ea2e9739729558b1cfea32a5f6776ad24f0bb&)
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
use 302 instead of 303
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
same!
🥲
@Ray
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
you should handle it on client side
const res = await axios.post()
if (res.redirected) {
router.push("/feed")
}
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
but why it is doing this, as It is working fine
any idea?
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
fetch returns the data to be handled by JavaScript without causing the browser to navigate at all (although you don't do anything with the return value of fetch).
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
But I made a request in postman on the same API, it returns the /feed response as HTML
it is getting response
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
because postman not submitting with fetch
so what is your issue?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
it is getting /feed as response and logging in successfully, but not redirecting
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
because the endpoint return redirect to the response, so postman get the html
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
You can see in the browser network tab as send earlier
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
have you do this?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
I was doing the same before, it was redirect. But this is using client side. I have to redirect using server side
Basically, I refactored the code from client to server
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
so how does your code look like now?
how do you submit the form?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
const onSubmit: SubmitHandler<{
email: string;
password: string;
}> = async ({ email, password }) => {
try {
// show loader
showLoader();
// hit api request for login
await axios.post("/api/auth/login", {
email: email,
password: password,
});
// show success message
toast({
variant: "default",
description: "Logged in successfully!"
});
} catch (error: any) {
// show error message
toast({
variant: "destructive",
title: "Uh oh! Something went wrong.",
description: error.message,
});
} finally {
// hide loader
hideLoader();
}
};
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
you are submitting with axios(fetch)
so you need to do this
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
so if I use fetch() what do I need to do?
this is client side redirection
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
use server action instead of axios/fetch then
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
Please can you give me the starter code for that
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
![Image](https://cdn.discordapp.com/attachments/1189872406274265199/1189960724936663154/Screenshot_2023-12-28_at_9.28.54_PM.png?ex=65a01049&is=658d9b49&hm=724c874b377513cacf8d3b0cba54cf52e4b73ab9081f012633400052f68fe512&)
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
no, you can't just add 'use server'
btw, I don't think it would work with
import { signInWithEmailAndPassword } from "firebase/auth";
I think you got error with route handler too right?
so you have to comment it out
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
No, it is doing the same,
logging in but not redirecting
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
oh ok
use server action then
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant AngoraOP
I think it will take time to resolve until then I will use router.push
Anyways, thanks for helping. 🫂
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
yeah, what you need to do is
1. convert the route handler to server action
2. change onSubmit to action props on the form element
1. convert the route handler to server action
2. change onSubmit to action props on the form element