Showing
1 changed file
with
20 additions
and
1 deletions
1 | import { prisma } from "../../../utils"; | 1 | import { prisma } from "../../../utils"; |
2 | +import bcrypt from "bcryptjs"; | ||
3 | +import jwt from "jsonwebtoken"; | ||
2 | 4 | ||
3 | export default { | 5 | export default { |
4 | Mutation: { | 6 | Mutation: { |
5 | - login: async (_, args) => { | 7 | + login: async (_, args, context) => { |
6 | const { email, password } = args; | 8 | const { email, password } = args; |
9 | + const user = await prisma.user.findOne({ | ||
10 | + where: { | ||
11 | + email, | ||
12 | + }, | ||
13 | + }); | ||
14 | + if (!user) { | ||
15 | + throw new Error("There is no such user"); | ||
16 | + } | ||
17 | + | ||
18 | + const vaild = await bcrypt.compare(password, user.password); | ||
19 | + if (!vaild) { | ||
20 | + throw new Error("Invaild Password!"); | ||
21 | + } | ||
22 | + | ||
23 | + const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET); | ||
24 | + | ||
25 | + return { token, user }; | ||
7 | }, | 26 | }, |
8 | }, | 27 | }, |
9 | }; | 28 | }; | ... | ... |
-
Please register or login to post a comment