Remove duplicate code

- there was the same methods in user.js and user_management.js except
that the login was not filtering out deleted users, so it was failing
one test. Reverted changes back to the user.js from the master branch...
added back our changes.

Co-authored-by: Tirokk <wolle.huss@pjannto.com>
Co-authored-by: ogerly <fridolin@tutanota.com>
This commit is contained in:
mattwr18 2019-09-04 19:31:43 +02:00
parent c92ba35db6
commit b04649e1ee

View File

@ -1,10 +1,7 @@
import encode from '../../jwt/encode'
import bcrypt from 'bcryptjs'
import { neo4jgraphql } from 'neo4j-graphql-js'
import fileUpload from './fileUpload'
import { neode } from '../../bootstrap/neo4j'
import { AuthenticationError, UserInputError, ForbiddenError } from 'apollo-server'
import { UserInputError, ForbiddenError } from 'apollo-server'
import Resolver from './helpers/Resolver'
const instance = neode()
@ -58,66 +55,8 @@ export default {
}
return neo4jgraphql(object, args, context, resolveInfo, false)
},
isLoggedIn: (_, args, { driver, user }) => {
return Boolean(user && user.id)
},
currentUser: async (object, params, ctx, resolveInfo) => {
const { user } = ctx
if (!user) return null
return neo4jgraphql(object, { id: user.id }, ctx, resolveInfo, false)
},
},
Mutation: {
login: async (_, { email, password }, { driver, req, user }) => {
// if (user && user.id) {
// throw new Error('Already logged in.')
// }
const session = driver.session()
const result = await session.run(
'MATCH (user:User)-[:PRIMARY_EMAIL]->(e:EmailAddress {email: $userEmail})' +
'RETURN user {.id, .slug, .name, .avatar, .encryptedPassword, .role, .disabled, email:e.email} as user LIMIT 1',
{
userEmail: email,
},
)
session.close()
const [currentUser] = await result.records.map(record => {
return record.get('user')
})
if (
currentUser &&
(await bcrypt.compareSync(password, currentUser.encryptedPassword)) &&
!currentUser.disabled
) {
delete currentUser.encryptedPassword
return encode(currentUser)
} else if (currentUser && currentUser.disabled) {
throw new AuthenticationError('Your account has been disabled.')
} else {
throw new AuthenticationError('Incorrect email address or password.')
}
},
changePassword: async (_, { oldPassword, newPassword }, { driver, user }) => {
const currentUser = await instance.find('User', user.id)
const encryptedPassword = currentUser.get('encryptedPassword')
if (!(await bcrypt.compareSync(oldPassword, encryptedPassword))) {
throw new AuthenticationError('Old password is not correct')
}
if (await bcrypt.compareSync(newPassword, encryptedPassword)) {
throw new AuthenticationError('Old password and new password should be different')
}
const newEncryptedPassword = await bcrypt.hashSync(newPassword, 10)
await currentUser.update({
encryptedPassword: newEncryptedPassword,
updatedAt: new Date().toISOString(),
})
return encode(await currentUser.toJson())
},
block: async (object, args, context, resolveInfo) => {
const { user: currentUser } = context
if (currentUser.id === args.id) return null
@ -156,7 +95,6 @@ export default {
throw new ForbiddenError('Invalid version format!')
}
}
args = await fileUpload(args, { file: 'avatarUpload', url: 'avatar' })
try {
const user = await instance.find('User', args.id)
@ -217,6 +155,14 @@ export default {
},
},
User: {
email: async (parent, params, context, resolveInfo) => {
if (typeof parent.email !== 'undefined') return parent.email
const { id } = parent
const statement = `MATCH(u:User {id: {id}})-[:PRIMARY_EMAIL]->(e:EmailAddress) RETURN e`
const result = await instance.cypher(statement, { id })
const [{ email }] = result.records.map(r => r.get('e').properties)
return email
},
...Resolver('User', {
undefinedToNull: [
'actorId',
@ -264,13 +210,5 @@ export default {
badges: '<-[:REWARDED]-(related:Badge)',
},
}),
email: async (parent, params, context, resolveInfo) => {
if (typeof parent.email !== 'undefined') return parent.email
const { id } = parent
const statement = `MATCH(u:User {id: {id}})-[:PRIMARY_EMAIL]->(e:EmailAddress) RETURN e`
const result = await instance.cypher(statement, { id })
const [{ email }] = result.records.map(r => r.get('e').properties)
return email
},
},
}