sdy

add isAuthenticated middleware

1 -import { prisma } from "../../../utils"; 1 +import { prisma, isAuthenticated } from "../../../utils";
2 import bcrypt from "bcryptjs"; 2 import bcrypt from "bcryptjs";
3 3
4 export default { 4 export default {
5 Mutation: { 5 Mutation: {
6 resetPassword: async (_, args) => { 6 resetPassword: async (_, args) => {
7 - const { secret, email, passwordOne, passwordTwo } = args; 7 + if (isAuthenticated) {
8 - const user = await prisma.user.findOne({ 8 + const { secret, email, passwordOne, passwordTwo } = args;
9 - where: { 9 + const user = await prisma.user.findOne({
10 - email, 10 + where: {
11 - }, 11 + email,
12 - }); 12 + },
13 - const encryptSecret = await bcrypt.hash(user.emailSecret, 10); 13 + });
14 - if (encryptSecret !== secret) { 14 + const encryptSecret = await bcrypt.hash(user.emailSecret, 10);
15 - throw new Error( 15 + if (encryptSecret !== secret) {
16 - "not vaild secret value!, input another value or resend email" 16 + throw new Error(
17 - ); 17 + "not vaild secret value!, input another value or resend email"
18 - } else { 18 + );
19 - if (passwordOne !== passwordTwo) {
20 - // For check new password is right, the two things must be same.
21 - throw new Error("the two password don't match each other, try again");
22 } else { 19 } else {
23 - await prisma.user.update({ 20 + if (passwordOne !== passwordTwo) {
24 - where: { 21 + // For check new password is right, the two things must be same.
25 - email, 22 + throw new Error(
26 - }, 23 + "the two password don't match each other, try again"
27 - data: { 24 + );
28 - emailSecret: "", 25 + } else {
29 - password: passwordOne, 26 + await prisma.user.update({
30 - }, 27 + where: {
31 - }); 28 + email,
29 + },
30 + data: {
31 + emailSecret: "",
32 + password: passwordOne,
33 + },
34 + });
35 + }
36 + return user;
32 } 37 }
33 - return user; 38 + } else {
39 + throw new Error("You need to login first");
34 } 40 }
35 }, 41 },
36 }, 42 },
......