Next.js Discord

Discord Forum

Next-auth credentials -> sign in

Unanswered
Rex posted this in #help-forum
Open in Discord
RexOP
Hi, I creaed this function to sign-in with credentials, when I am successfully signed in, it redirects me to the home page, but my session is not automaticly updated, I still have the button Sing In instead of My Account but when I refresh the page, everything is good

export const signInWithCredentials = async (
  state: AuthResponse,
  data: FormData
): Promise<AuthResponse> => {
  try {
    const result = signInSchema.safeParse({
      email: data.get("email"),
      password: data.get("password"),
    });

    if (!result.success)
      return {
        success: false,
        errors: result.error.formErrors.fieldErrors,
      };

    const { email, password } = result.data;

    await signIn("credentials", { email, password });

    return {
      success: true,
    };
  } catch (error) {
    let errorMsg = "";
    if (error instanceof Error && error.message === "NEXT_REDIRECT") {
      redirect("/");
    } else if (error instanceof AuthError) {
      errorMsg = error.message;
    } else {
      errorMsg = (error as any).message;
    }
    return { error: errorMsg, success: false };
  }
};

0 Replies