I am trying to create a login function with GraphQL and node. I have gotten the sign up to work but when querying the login function it says the password is not defined.
the AuthType
const AuthType = new GraphQLObjectType({
name: 'Auth',
fields: () => ({
userId: {type: GraphQLString},
username: {type: GraphQLString},
email: {type: GraphQLString},
})
});
This holds data I am expecting back.
const RootQuery = new GraphQLObectType({
login: {
type: AuthType,
args: {
password: {
type: GraphQLString
},
email: {
type: GraphQLString
}
},
resolve(parent, args) {
return User.findOne({
email: args.email
})
.then(user => {
const isEqual = new Promise(bcrypt.compare(password, args.password));
if (!isEqual) {
throw new Error('Password is incorrect!');
}
}).then(result => {
return {
userId: result.id,
username: result.username
};
}).catch(err => {
throw err
});
}
}
});
This is the logic to check data, thanks.
schema.js
const graphql = require('graphql');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const {GraphQLObjectType,
GraphQLInt,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLList,
GraphQLNonNull } = graphql;
const User = require('../models/user');
const Event = require('../models/event');
The User type defines what data from the user we would like to store.
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
firstname: {type: GraphQLString},
lastname: {type: GraphQLString},
username: {type: GraphQLString},
email: {type: GraphQLString},
password: {type: GraphQLString},
location: {type: GraphQLString},
about: {type: GraphQLString},
gender: {type: GraphQLString},
yob: {type: GraphQLString}, //Year of Birth;
events: {
type: new GraphQLList(EventType),
resolve(parent, args){
// return _.filter(events, {userId: parent.id});
return Event.find({creator: parent.id});
}
}
})
});
The Login function still doesn't recognize the password input.
You are not defining the password field in your AuthType
, I guess you should do something like:
const AuthType = new GraphQLObjectType({
name: 'Auth',
fields: () => ({
userId: {type: GraphQLString},
username: {type: GraphQLString},
email: {type: GraphQLString},
password: {type: GraphQLString},
})
});
Also, you have a spelling mistake in this line:
const RootQuery = new GraphQLObectType({
It should be GraphQLObjectType
instead of GraphQLObectType
Also, in this line:
const isEqual = new Promise(bcrypt.compare(password, args.password));
Probably you are getting the error there, as password
is not defined in the code. You probably want to do user.password
?
Instead of using args in the resolve function, I used {email, password}.
const RootQuery = new GraphQLObectType({
login: {
type: AuthType,
args: {
password: {
type: GraphQLString
},
email: {
type: GraphQLString
}
},
resolve(parent, {email, password }) {
return User.findOne({
email: email
})
.then(user => {
const isEqual = bcrypt. compare(
password, user.password));
if (!isEqual) {
throw new Error('Password is incorrect!');
}
}).then(result => {
return {
userId: result.id,
username: result.username
};
}).catch(err => {
throw err
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.