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