I am implementing a login functionality with mongoose and graphql for user, but I am getting null from the login resolver somehow. here is the model:
const mongoose = require("mongoose")
const Schema = mongoose.Schema
const userSchema = new Schema({
username: { type: String, unique: true, required: true, trim: true },
email: { type: String, unique: true, required: true, trim: true },
password: { type: String, required: true },
phone: { type: String, unique:true, required: true, trim: true },
emailVerified: { type: Boolean, default: false },
phoneVerified: { type: Boolean, default: false }
})
module.exports = mongoose.model('User', userSchema)
I am using email and password for the login, I have tried to register the user and it works.
on the graphql end of things, I have 2 schemas, UserType and ActiveUserType, the ActiveUserType returns the jwt token for the user logging in:
const ActiveUserType = new GraphQLObjectType({
name: 'ActiveUser',
fields: () => ({
success: { type: GraphQLBoolean },
message: { type: GraphQLString },
token: { type: GraphQLString },
username: { type: GraphQLString }
})
})
and then I am creating a login field in the rootquery:
login: {
type: ActiveUserType,
args: { email: { type: new GraphQLNonNull(GraphQLString) }, password: { type: new GraphQLNonNull(GraphQLString) } },
resolve: function(parent, args) {
return User.findOne({ email: args.email, password: args.password }, function(err, user) {
if(err) {
return {
success: false,
message: "an error occurred",
token: null,
username: null
}
}
if(!user) {
return {
success: false,
message: "user not found"
token: null,
username: null
}
}
return {
success: true,
token: "something",
username: user.username
}
})
}
},
however, when I request for the login field on graphiql frontend with the email and password that surely exists in the collection, I am getting output:
{
"data": {
"login": null
}
}
this is my very first attempt with mongoose as well as graphql, hence cannot deduce what is going wrong here, however have already looked into several threads prior to posting this question, but not finding any solution. Your help is highly appreciated.