|
1 | | -import { prisma } from './generated/prisma-client' |
2 | | -import { ContextParameters } from 'graphql-yoga/dist/types' |
| 1 | +import { prisma, } from '../src/generated/prisma-client' |
| 2 | +import { ContextParameters, } from 'graphql-yoga/dist/types' |
3 | 3 | import * as jwt from 'jsonwebtoken' |
4 | | -import { arg, stringArg, idArg, enumType, objectType } from 'nexus' |
| 4 | +import { arg, stringArg, idArg, enumType, objectType, } from 'nexus' |
5 | 5 | import * as bcrypt from 'bcrypt' |
6 | 6 |
|
7 | 7 | export const getUser = async (req: ContextParameters) => { |
8 | | - const auth = req.request.get('Authorization') |
| 8 | + const auth = req.request.get('Authorization') |
9 | 9 |
|
10 | | - if (auth) { |
11 | | - const token = auth.replace('Bearer ', '') |
12 | | - try { |
13 | | - const { userId } = jwt.verify(token, 'APP_SECRET') |
14 | | - // TO FIX: prisma.user ne retourne pas .role |
15 | | - return prisma.user({ id: userId }) |
16 | | - } catch (error) { |
17 | | - console.log('getUser error', error) |
18 | | - return null |
19 | | - } |
20 | | - } else { |
21 | | - return null |
22 | | - } |
| 10 | + if (auth) { |
| 11 | + const token = auth.replace('Bearer ', '') |
| 12 | + try { |
| 13 | + const { userId, } = jwt.verify(token, 'APP_SECRET') |
| 14 | + // TO FIX: prisma.user ne retourne pas .role |
| 15 | + return prisma.user({ id: userId, }) |
| 16 | + } catch (error) { |
| 17 | + console.log('getUser error', error) |
| 18 | + return null |
| 19 | + } |
| 20 | + } else { |
| 21 | + return null |
| 22 | + } |
23 | 23 | } |
24 | 24 |
|
25 | 25 | export const signup = { |
26 | | - type: 'AuthPayload', |
27 | | - args: { |
28 | | - name: stringArg({ nullable: false }), |
29 | | - email: stringArg({ nullable: false }), |
30 | | - password: stringArg({ nullable: false }) |
31 | | - }, |
32 | | - resolve: async (_, { name, email, password }, ctx) => { |
33 | | - const password2 = await bcrypt.hash(password, 10) |
| 26 | + type: 'AuthPayload', |
| 27 | + args: { |
| 28 | + name: stringArg({ nullable: false, }), |
| 29 | + email: stringArg({ nullable: false, }), |
| 30 | + password: stringArg({ nullable: false, }), |
| 31 | + }, |
| 32 | + resolve: async (_, { name, email, password, }, ctx) => { |
| 33 | + const password2 = await bcrypt.hash(password, 10) |
34 | 34 |
|
35 | | - const user = await ctx.prisma.createUser({ |
36 | | - name, |
37 | | - email, |
38 | | - password: password2, |
39 | | - role: 'USER' |
40 | | - }) |
| 35 | + const user = await ctx.prisma.createUser({ |
| 36 | + name, |
| 37 | + email, |
| 38 | + password: password2, |
| 39 | + role: 'USER', |
| 40 | + }) |
41 | 41 |
|
42 | | - console.log({ user }) |
| 42 | + console.log({ user, }) |
43 | 43 |
|
44 | | - return { |
45 | | - token: jwt.sign({ userId: user.id }, 'APP_SECRET'), |
46 | | - user |
47 | | - } |
48 | | - } |
| 44 | + return { |
| 45 | + token: jwt.sign({ userId: user.id, }, 'APP_SECRET'), |
| 46 | + user, |
| 47 | + } |
| 48 | + }, |
49 | 49 | } |
50 | 50 |
|
51 | 51 | export const login = { |
52 | | - type: 'AuthPayload', |
53 | | - args: { |
54 | | - email: stringArg({ nullable: false }), |
55 | | - password: stringArg({ nullable: false }) |
56 | | - }, |
57 | | - resolve: async (_, { email, password }, ctx) => { |
58 | | - const user = await ctx.prisma.user({ email }) |
59 | | - console.log({ user }) |
60 | | - if (!user) throw new Error('User not in db.') |
| 52 | + type: 'AuthPayload', |
| 53 | + args: { |
| 54 | + email: stringArg({ nullable: false, }), |
| 55 | + password: stringArg({ nullable: false, }), |
| 56 | + }, |
| 57 | + resolve: async (_, { email, password, }, ctx) => { |
| 58 | + const user = await ctx.prisma.user({ email, }) |
| 59 | + console.log({ user, }) |
| 60 | + if (!user) throw new Error('User not in db.') |
61 | 61 |
|
62 | | - const valid = await bcrypt.compare(password, user.password) |
63 | | - console.log({ valid }) |
64 | | - if (!valid) throw new Error('Wrong password.') |
| 62 | + const valid = await bcrypt.compare(password, user.password) |
| 63 | + console.log({ valid, }) |
| 64 | + if (!valid) throw new Error('Wrong password.') |
65 | 65 |
|
66 | | - return { |
67 | | - token: jwt.sign({ userId: user.id }, 'APP_SECRET'), |
68 | | - user |
69 | | - } |
70 | | - } |
| 66 | + return { |
| 67 | + token: jwt.sign({ userId: user.id, }, 'APP_SECRET'), |
| 68 | + user, |
| 69 | + } |
| 70 | + }, |
71 | 71 | } |
0 commit comments