Creating user accounts with remix-auth-form and remix-auth and prisma

I’m learning authentication for remix. I’m creating a signup sheet where the user creates an account with an email, username, and password. I can create the account on my database but the authentication fails. Here’s my code. I would appreciate any insight or help you can give me.

import React, { useState } from "react";
import type { MetaFunction } from "@remix-run/node";
import { ActionFunction, json, LoaderFunction } from "@remix-run/node";
import { useActionData, Link } from "@remix-run/react";
import { getMeta } from "~/utils/meta";
import { authenticator } from "~/.server/auth";
import { createUser } from "~/.server/user";
import Layout from "~/components/Layout";
import Textfield from "~/components/TextField";
import MaxWidthWrapper from "~/components/MaxWidthWrapper";

export const meta: MetaFunction = () => {
  const meta = getMeta({ title: "Pirate Radio Network" });
  return meta;
};

export const loader: LoaderFunction = async ({ request }) => {
  const user = await authenticator.isAuthenticated(request, {
    successRedirect: "/",
  });
  return { user };
};

export const action: ActionFunction = async ({ request }) => {
  const form = await request.formData();
  const action = form.get("_action");
  const email = form.get("email");
  const password = form.get("password");
  const username = form.get("username");

  if (
    typeof action !== "string" ||
    typeof email !== "string" ||
    typeof password !== "string" ||
    typeof username !== "string"
  ) {
    return json({ error: `Invalid Form Data`, form: action }, { status: 400 });
  }

  console.log(`Type of action: ${typeof action}`);
  console.log(`Type of email: ${typeof email}`);
  console.log(`Type of password: ${typeof password}`);
  console.log(`Type of username: ${typeof username}`);

  await createUser({ email, password, username })
    .then(() => console.log("User created successfully"))
    .catch((error) => console.error("User creation failed:", error));

  // Clone the request
  const clonedRequest = new Request(request, {
    body: form,
  });

  console.log(`Email: ${email}`);
  console.log(`Username: ${username}`);

  return await authenticator
    .authenticate("form", clonedRequest, {
      successRedirect: "/",
      failureRedirect: "/join",
      context: {formData: form},
    })
    .then(() => {
      console.log("Authentication succeeded");
      return json({ status: "success" }); // Add a return statement here
    })
    .catch((error) => {
      console.error("Authentication failed:", error);
      console.error("Request:", clonedRequest);
      return json({ error: "Authentication failed" }, { status: 400 }); // Add a return statement here
    });
};

export default function Signup() {
  const actionData = useActionData() as { context?: { formData: any } };
  const formData = actionData?.context?.formData || {};

  const [formState, setFormState] = useState({
    email: formData.email || "",
    password: formData.password || "",
    username: formData.username || "",
  });

  // Updates the form data when an input changes
  const handleInputChange = (
    event: React.ChangeEvent<HTMLInputElement>,
    field: string
  ) => {
    setFormState((form) => ({ ...form, [field]: event.target.value }));
  };

Here is my auth sever code.

import { Authenticator, AuthorizationError } from "remix-auth";
import { sessionStorage } from "./session";
import { FormStrategy } from "remix-auth-form";
import { prisma } from "./db";
import bcrypt from "bcryptjs";
import { Role } from "@prisma/client";

interface User {
  id: string;
  email: string;
  username: string;
  firstName: string;
  lastName: string;
  createdAt: Date;
  updatedAt: Date;
  role: Role;
  password: string;
}

const sessionSecret = process.env.SESSION_SECRET;
if (!sessionSecret) {
  throw new Error("SESSION_SECRET must be set");
}

const authenticator = new Authenticator<any>(sessionStorage);

const formStrategy = new FormStrategy(async ({form}) => {
  const email = form.get("email") as string;
  const password = form.get("password") as string;
  const username = form.get("username") as string;

  if (!email || !password || !username) {
    console.log("Form data is missing");
    throw new AuthorizationError();
  }

  const user = await prisma.user.findFirst({
    where: {
      OR: [
        { email: email },
        { username: username },
      ],
    },
    select: {
      id: true,
      email: true,
      username: true,
      firstName: true,
      lastName: true,
      createdAt: true,
      updatedAt: true,
      role: true,
      password: true,
    },
  }) as unknown as User;

  if (!user) {
    console.log("Invalid email or username");
    throw new AuthorizationError();
  }

  const passwordsMatch = await bcrypt.compare(password, user.password as string);

  if (!passwordsMatch) {
    throw new AuthorizationError();
  }

  return user;
});

authenticator.use(formStrategy, "form");

export {authenticator};

I expected the user account to authenticate upon creation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật