login.js 656 Bytes
import { prisma } from "../../../utils";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";

export default {
  Mutation: {
    login: async (_, args, context) => {
      const { email, password } = args;
      const user = await prisma.user.findOne({
        where: {
          email,
        },
      });
      if (!user) {
        throw new Error("There is no such user");
      }

      const vaild = await bcrypt.compare(password, user.password);
      if (!vaild) {
        throw new Error("Invaild Password!");
      }

      const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET);

      return { token, user };
    },
  },
};