Untitled

 avatar
unknown
typescript
a year ago
996 B
12
Indexable
"use client";

import { z } from "zod";
import { userAuthSchema } from "./validations/auth";
import { signIn } from "next-auth/react";
import { AuthError } from "next-auth";

export const handleLogin = async (values: z.infer<typeof userAuthSchema>) => {
  const validatedFields = userAuthSchema.safeParse(values);

  if (!validatedFields.success) {
    return { error: "Invalid fields!" };
  }

  let { phone, pin } = validatedFields.data;

  const signInResponse = await signIn("credentials", {
    phone,
    pin,
    redirect: false
  });

  if (signInResponse?.error) {
    if (signInResponse.error.includes("Credential")) {
      return { error: "Invalid credentials!"}
    } else if (signInResponse.error.includes("Callback")) {
      return { error: "Invalid credentials"}
    } else if (signInResponse.error.includes("Configuration")) {
      return { error: "Invalid credentials" }
    } else {
      return { error: "Something went wrong!"}
    }
  }
};
Editor is loading...
Leave a Comment