signInAuth.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { bagistoFetch, /*restApiFetch*/} from "@/utils/bagisto";
  2. import { CUSTOMER_LOGIN } from "@/graphql/customer/mutations";
  3. type LoginFormInputs = {
  4. username: string;
  5. password: string;
  6. };
  7. export default async function signInAuth(loginData:LoginFormInputs) {
  8. const input = {
  9. email: loginData.username,
  10. password: loginData.password,
  11. };
  12. const res = await bagistoFetch<any>({
  13. query: CUSTOMER_LOGIN,
  14. variables: { input },
  15. cache: "no-store",
  16. });
  17. const data = res?.body?.data?.createCustomerLogin?.customerLogin;
  18. if (!data || !data.success || !data.token) {
  19. throw new Error(data?.message || "Invalid credentials.");
  20. }
  21. return {
  22. id: data.id, // required by NextAuth
  23. email: loginData.username,
  24. name: loginData.username, // Using email as name since firstName/lastName are missing in response
  25. apiToken: data.apiToken,
  26. accessToken: data.token, // Sanctum token
  27. role: "customer",
  28. };
  29. }