Get rid of LoggedInUser graphql type

This was causing a lot of headache on the frontend. Now, there a single
source of truth. If you query `currentUser` you can overwrite your local
copy of the user and update the UI.
This commit is contained in:
Robert Schäfer 2019-02-27 10:42:55 +01:00
parent ec185f13d4
commit 5fd44230dd
5 changed files with 16 additions and 35 deletions

View File

@ -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}`
}
}

View File

@ -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.')
})
}

View File

@ -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()

View File

@ -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!

View File

@ -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 = {