Fails to redirect in server action
Answered
Polish posted this in #help-forum
PolishOP
This is my server action for the login
Every other thing works fine here, but when it comes to return redirect('/notes', RedirectType.push) it throws an error,
e.message says "NEXT_REDIRECT".Why is that ? need help
"use server"
import { createSession } from "@/app/lib/session"
import { authenticateUser } from "@/db/controller/usercontroller"
import { redirect, RedirectType } from "next/navigation"
export async function loginAction(
{ username, password }: { username: string, password: string }) {
try {
const { authorize, user } = await authenticateUser({ username: username, password: password })
if (authorize && user) {
await createSession(user)
return redirect('/notes', RedirectType.push)
}
else {
return { error: "Failed to authorize" }
}
}
catch (e) {
if (e instanceof Error) {
return { error: e.message }
}
return { error: "Failed to authorize" }
}
}
Every other thing works fine here, but when it comes to return redirect('/notes', RedirectType.push) it throws an error,
e.message says "NEXT_REDIRECT".Why is that ? need help
Answered by chisto
redirect internally throws an error so it should be called outside of try/catch blocks
5 Replies
show us how you call the function
PolishOP
'use client'
-----------------------------------------
async function onSubmit(data: { username: string, password: string }) {
setIsPending(true)
const result = await loginAction(data)
if (result) {
setIsPending(false)
}
result?.error && form.setError('password', { message: result?.error })
result?.error && form.setError('username', { message: '' })
}
--------------------------
<Button type="submit" className="w-full dark:bg-white bg-purple-700 hover:dark:bg-white hover:bg-purple-800 text-md font-semibold mt-2" disabled={isPending}>
{
isPending ?
<PendingDots />
:
<div>
log in
</div>
}
</Button>
redirect internally throws an error so it should be called outside of try/catch blocks
Answer
PolishOP
@chisto is there any better why way do this , this seems kind of odd
"use server"
import { createSession } from "@/app/lib/session"
import { authenticateUser } from "@/db/controller/usercontroller"
import { redirect, RedirectType } from "next/navigation"
export async function loginAction(
{ username, password }: { username: string, password: string }) {
let loggedIn = false
try {
const { authorize, user } = await authenticateUser({ username: username, password: password })
if (authorize && user) {
await createSession(user)
loggedIn = true
// throw redirect('/notes', RedirectType.push)
}
else {
return { error: "Failed to authorize" }
}
}
catch (e) {
if (e instanceof Error) {
return { error: e.message }
}
return { error: "Failed to authorize" }
}
if (loggedIn) {
return redirect('/notes')
}
else {
return { error: "Failed to authorize" }
}
}
I think you don't need the last else at the end
just redirect("/notes")
any error was already caught in the catch block
just redirect("/notes")
any error was already caught in the catch block