diff --git a/src/jest/helpers.js b/src/jest/helpers.js index ff6a535e2..0d358ed40 100644 --- a/src/jest/helpers.js +++ b/src/jest/helpers.js @@ -7,12 +7,10 @@ export const host = 'http://127.0.0.1:4123' export async function login ({ email, password }) { const mutation = ` mutation { - login(email:"${email}", password:"${password}"){ - token - } + login(email:"${email}", password:"${password}") }` const response = await request(host, mutation) return { - authorization: `Bearer ${response.login.token}` + authorization: `Bearer ${response.login}` } } diff --git a/src/resolvers/user_management.js b/src/resolvers/user_management.js index b0c9d3df5..552f6201a 100644 --- a/src/resolvers/user_management.js +++ b/src/resolvers/user_management.js @@ -1,16 +1,18 @@ import encode from '../jwt/encode' -import { fixUrl } from '../middleware/fixImageUrlsMiddleware' import bcrypt from 'bcryptjs' import { AuthenticationError } from 'apollo-server' +import { neo4jgraphql } from 'neo4j-graphql-js' export default { Query: { isLoggedIn: (parent, args, { driver, user }) => { return Boolean(user && user.id) }, - currentUser: (parent, args, { user }) => { - return user - } + currentUser: async (object, params, ctx, resolveInfo) => { + const { user} = ctx + if(!user) return null + return neo4jgraphql(object, {id: user.id}, ctx, resolveInfo, false) + }, }, Mutation: { signup: async (parent, { email, password }, { req }) => { @@ -41,10 +43,7 @@ export default { if (currentUser && await bcrypt.compareSync(password, currentUser.password)) { delete currentUser.password - currentUser.avatar = fixUrl(currentUser.avatar) - return Object.assign(currentUser, { - token: encode(currentUser) - }) + return encode(currentUser) } else throw new AuthenticationError('Incorrect email address or password.') }) } diff --git a/src/resolvers/user_management.spec.js b/src/resolvers/user_management.spec.js index cb12efb2d..a3bf6fdd0 100644 --- a/src/resolvers/user_management.spec.js +++ b/src/resolvers/user_management.spec.js @@ -82,7 +82,6 @@ describe('currentUser', () => { avatar email role - token } }` @@ -122,8 +121,7 @@ describe('currentUser', () => { id: 'acb2d923-f3af-479e-9f00-61b12e864666', name: 'Matilde Hermiston', slug: 'matilde-hermiston', - role: 'user', - token: headers.authorization.replace('Bearer ', '') + role: 'user' } } await expect(client.request(query)).resolves.toEqual(expected) @@ -137,9 +135,7 @@ describe('login', () => { const { email, password } = params return ` mutation { - login(email:"${email}", password:"${password}"){ - token - } + login(email:"${email}", password:"${password}") }` } @@ -150,7 +146,7 @@ describe('login', () => { email: 'test@example.org', password: '1234' })) - const { token } = data.login + const token = data.login jwt.verify(token, process.env.JWT_SECRET, (err, data) => { expect(data.email).toEqual('test@example.org') expect(err).toBeNull() diff --git a/src/schema.graphql b/src/schema.graphql index 4413e1deb..1f9bcb477 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -1,24 +1,14 @@ type Query { isLoggedIn: Boolean! - currentUser: LoggedInUser + currentUser: User statistics: Statistics! } type Mutation { - login(email: String!, password: String!): LoggedInUser + login(email: String!, password: String!): String! signup(email: String!, password: String!): Boolean! report(resource: Resource!, description: String): Report } -type LoggedInUser { - id: ID! - slug: String! - name: String! - avatar:String! - email: String! - role: String! - token: String! -} - type Statistics { countUsers: Int! countPosts: Int! diff --git a/src/seed/factories/index.js b/src/seed/factories/index.js index d9bbd700c..ed35d2c3b 100644 --- a/src/seed/factories/index.js +++ b/src/seed/factories/index.js @@ -15,13 +15,11 @@ export const seedServerHost = 'http://127.0.0.1:4001' const authenticatedHeaders = async ({ email, password }, host) => { const mutation = ` mutation { - login(email:"${email}", password:"${password}"){ - token - } + login(email:"${email}", password:"${password}") }` const response = await request(host, mutation) return { - authorization: `Bearer ${response.login.token}` + authorization: `Bearer ${response.login}` } } const factories = {