|
@@ -1,19 +1,9 @@
|
|
-// const { forwardTo } = require('prisma-binding')
|
|
|
|
-const bcrypt = require('bcryptjs')
|
|
|
|
-const randombytes = require('randombytes')
|
|
|
|
-const { promisify } = require('util')
|
|
|
|
-const jwt = require('jsonwebtoken')
|
|
|
|
-const { transport, emailTemplate } = require('./mail')
|
|
|
|
|
|
+// const { transport, emailTemplate } = require('./mail')
|
|
|
|
|
|
const LoginError = new Error('You must be logged in.')
|
|
const LoginError = new Error('You must be logged in.')
|
|
-const PermissionError = new Error('Insufficient permissions.')
|
|
|
|
|
|
+// const PermissionError = new Error('Insufficient permissions.')
|
|
|
|
|
|
const Query = {
|
|
const Query = {
|
|
- users: async (parent, args, context, info) => {
|
|
|
|
- if (!context.request.userId) throw LoginError
|
|
|
|
- if (!context.request.user.permissions.find(permission => permission === 'ADMIN')) throw PermissionError
|
|
|
|
- return context.db.query.users(args, info)
|
|
|
|
- },
|
|
|
|
training: async (parent, args, context, info) => {
|
|
training: async (parent, args, context, info) => {
|
|
if (!context.request.userId) throw LoginError
|
|
if (!context.request.userId) throw LoginError
|
|
return context.db.query.training({ data: args }, info)
|
|
return context.db.query.training({ data: args }, info)
|
|
@@ -29,137 +19,10 @@ const Query = {
|
|
blocks: async (parent, args, context, info) => {
|
|
blocks: async (parent, args, context, info) => {
|
|
if (!context.request.userId) throw LoginError
|
|
if (!context.request.userId) throw LoginError
|
|
return context.db.query.trainingTypes()
|
|
return context.db.query.trainingTypes()
|
|
- },
|
|
|
|
- me: (parent, args, context, info) => {
|
|
|
|
- if (!context.request.userId) throw LoginError
|
|
|
|
- return context.db.query.user({ where: { id: context.request.userId } }, info)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
const Mutation = {
|
|
const Mutation = {
|
|
- createUser: async (parent, { data }, context, info) => {
|
|
|
|
- if (!context.request.userId) throw LoginError
|
|
|
|
- if (!context.request.user.permissions.find(permission => permission === 'ADMIN')) throw PermissionError
|
|
|
|
-
|
|
|
|
- const email = data.email.toLowerCase()
|
|
|
|
- const password = await bcrypt.hash(data.password, 10)
|
|
|
|
- return context.db.mutation.createUser({
|
|
|
|
- data: {
|
|
|
|
- ...data,
|
|
|
|
- email,
|
|
|
|
- password
|
|
|
|
- }
|
|
|
|
- }, info)
|
|
|
|
- },
|
|
|
|
- updateUser: async (parent, { data, where }, context, info) => {
|
|
|
|
- if (!context.request.userId) throw LoginError
|
|
|
|
- if (!context.request.user.permissions.find(permission => permission === 'ADMIN')) throw PermissionError
|
|
|
|
-
|
|
|
|
- const updateData = { ...data }
|
|
|
|
- if (data.email) updateData.email = data.email.toLowerCase()
|
|
|
|
- if (data.password) updateData.password = await bcrypt.hash(data.password, 10)
|
|
|
|
- return context.db.mutation.updateUser({
|
|
|
|
- data: updateData,
|
|
|
|
- where
|
|
|
|
- }, info)
|
|
|
|
- },
|
|
|
|
- deleteUser: (parent, { where }, context, info) => {
|
|
|
|
- if (!context.request.userId) throw LoginError
|
|
|
|
- if (!context.request.user.permissions.find(permission => permission === 'ADMIN')) throw PermissionError
|
|
|
|
-
|
|
|
|
- return context.db.mutation.deleteUser({ where })
|
|
|
|
- },
|
|
|
|
- signup: async (parent, args, ctx, info) => {
|
|
|
|
- const email = args.email.toLowerCase()
|
|
|
|
- const password = await bcrypt.hash(args.password, 10)
|
|
|
|
- const user = await ctx.db.mutation.createUser(
|
|
|
|
- {
|
|
|
|
- data: {
|
|
|
|
- ...args,
|
|
|
|
- email,
|
|
|
|
- password
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- info
|
|
|
|
- )
|
|
|
|
- const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET)
|
|
|
|
- ctx.response.cookie('token', token, {
|
|
|
|
- httpOnly: true,
|
|
|
|
- maxAge: 24 * 60 * 60 * 1000
|
|
|
|
- })
|
|
|
|
- return user
|
|
|
|
- },
|
|
|
|
- login: async (parent, args, context, info) => {
|
|
|
|
- const { email, password } = args
|
|
|
|
- const user = await context.db.query.user({ where: { email } })
|
|
|
|
- if (!user) throw new Error('User not found')
|
|
|
|
- const valid = await bcrypt.compare(password, user.password)
|
|
|
|
- if (!valid) throw new Error('Invalid password')
|
|
|
|
- const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET)
|
|
|
|
- context.response.cookie(
|
|
|
|
- 'token',
|
|
|
|
- token,
|
|
|
|
- {
|
|
|
|
- httpOnly: true,
|
|
|
|
- maxAge: 7 * 24 * 3600 * 1000
|
|
|
|
- },
|
|
|
|
- info
|
|
|
|
- )
|
|
|
|
- return user
|
|
|
|
- },
|
|
|
|
- logout: async (parent, args, context, info) => {
|
|
|
|
- context.response.clearCookie('token')
|
|
|
|
- return 'Logged out.'
|
|
|
|
- },
|
|
|
|
- requestReset: async (parent, { email }, context, info) => {
|
|
|
|
- const user = await context.db.query.user({ where: { email } })
|
|
|
|
- if (!user) {
|
|
|
|
- return 'Success.'
|
|
|
|
- }
|
|
|
|
- const randombytesPromisified = promisify(randombytes)
|
|
|
|
- const resetToken = (await randombytesPromisified(20)).toString('hex')
|
|
|
|
- const resetTokenExpiry = Date.now() + 3600000 // 1 hour from now
|
|
|
|
- await context.db.mutation.updateUser({
|
|
|
|
- where: { email },
|
|
|
|
- data: { resetToken, resetTokenExpiry }
|
|
|
|
- })
|
|
|
|
- /* await transport.sendMail({
|
|
|
|
- from: 'wes@wesbos.com',
|
|
|
|
- to: user.email,
|
|
|
|
- subject: 'Your Password Reset Token',
|
|
|
|
- html: emailTemplate(`Your Password Reset Token is here!
|
|
|
|
- \n\n
|
|
|
|
- <a href="${process.env
|
|
|
|
- .FRONTEND_URL}/reset?resetToken=${resetToken}">Click Here to Reset</a>`)
|
|
|
|
- }) */
|
|
|
|
- return 'Success.'
|
|
|
|
- },
|
|
|
|
- resetPassword: async (parent, args, context, info) => {
|
|
|
|
- const [user] = await context.db.query.users({
|
|
|
|
- where: {
|
|
|
|
- resetToken: args.token,
|
|
|
|
- resetTokenExpiry_gte: Date.now() - 3600000
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- if (!user) {
|
|
|
|
- throw Error('Token invalid or expired.')
|
|
|
|
- }
|
|
|
|
- const password = await bcrypt.hash(args.password, 10)
|
|
|
|
- const updatedUser = await context.db.mutation.updateUser({
|
|
|
|
- where: { email: user.email },
|
|
|
|
- data: {
|
|
|
|
- password,
|
|
|
|
- resetToken: null,
|
|
|
|
- resetTokenExpiry: null
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- const token = jwt.sign({ userId: updatedUser.id }, process.env.APP_SECRET)
|
|
|
|
- context.response.cookie('token', token, {
|
|
|
|
- httpOnly: true,
|
|
|
|
- maxAge: 1000 * 60 * 60 * 24 * 365
|
|
|
|
- })
|
|
|
|
- return updatedUser
|
|
|
|
- },
|
|
|
|
createTraining: async (parent, args, context, info) => {
|
|
createTraining: async (parent, args, context, info) => {
|
|
if (!context.request.userId) throw LoginError
|
|
if (!context.request.userId) throw LoginError
|
|
const training = await context.db.mutation.createTraining(
|
|
const training = await context.db.mutation.createTraining(
|