import NextAuth from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import dbConnect from '../../../utils/helpers/dbHelpers';
const User = require('../../../models/user');

// @ts-ignore
export default NextAuth({
  session: {
    // @ts-ignore
    jwt: true,
  },
  callbacks: {
    async jwt({ token, user }) {
      return { ...token, ...user };
    },
    // @ts-ignore
    async session({ token }) {
      return token;
    },
  },
  providers: [
    Credentials({
      // @ts-ignore
      async authorize(credentials) {
        await dbConnect();

        // @ts-ignore
        const userData = await User.findByCredentials(
          // @ts-ignore
          credentials.username,
          // @ts-ignore
          credentials.password
        );
        return { user: userData };
      },
    }),
  ],
});
