"use server"

import { AuthError } from "next-auth"

import { signIn } from "@/lib/auth"
import { SignInFormType } from "@/lib/schemas/auth.schemas"

export async function signInAction(values: SignInFormType) {
  const { username, password } = values

  try {
    await signIn("credentials", {
      username,
      password,
      redirect: false,
    })
    return {
      success: true,
    }
  } catch (error) {
    if (error instanceof AuthError) {
      return { error: error.cause?.err?.message }
    }
    return null
  }
}
