Next.js Discord

Discord Forum

Fails to redirect in server action

Answered
Polish posted this in #help-forum
Open in Discord
Avatar
PolishOP
This is my server action for the login
"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
View full answer

5 Replies

Avatar
show us how you call the function
Avatar
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>
Avatar
redirect internally throws an error so it should be called outside of try/catch blocks
Answer
Avatar
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" }

  }
}
Avatar
I think you don't need the last else at the end
just redirect("/notes")
any error was already caught in the catch block