DEV Community

RJ-1998
RJ-1998

Posted on

4 1

Login/Logout functionality with Reactjs and Graphql

I'm trying to build a login/logout functionality using Graphql and Reactjs.
Here's my schema file

const graphql = require('graphql');
const User = require('../models/user');
const Post = require('../models/post');
const Login = require('../models/login')
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull
} = graphql;
const loginSchema = new GraphQLObjectType({
name: 'Login',
fields: () =>({
email: {type: GraphQLString},
password: {type: GraphQLString},
ok: {type: GraphQLString}
})
});
const postTags = new GraphQLObjectType({
name: 'postTags',
fields: () =>({
tag1: {type: GraphQLString},
tag2: {type: GraphQLString},
tag3: {type: GraphQLString},
tag4: {type: GraphQLString},
tag5: {type: GraphQLString}
})
});
const userSocialURL = new GraphQLObjectType({
name: 'SocialURL',
fields: () =>({
facebookurl: {type: GraphQLString},
githuburl: {type: GraphQLString},
twitterurl: {type: GraphQLString}
})
});
const userProfile = new GraphQLObjectType({
name: 'Profile',
fields: () =>({
age: {type: GraphQLInt},
bio: {type: GraphQLString},
uname: {type: GraphQLString},
occupation: {type: GraphQLString},
socialurl: {type: userSocialURL}
})
});
const PostType = new GraphQLObjectType({
name: 'Post',
fields: () =>({
id: {type: GraphQLID},
title: {type: GraphQLString},
body: {type: GraphQLString},
loves: {type: GraphQLInt},
date: {type: GraphQLString},
tags: {type: postTags},
user: {
type: UserType,
resolve(parent,args){
return User.findById(parent.userId);
}
}
})
});
const UserType = new GraphQLObjectType({
name: 'User',
fields: () =>({
id: {type: GraphQLID},
fname: {type: GraphQLString},
lname: {type: GraphQLString},
email: {type: GraphQLString},
password: {type: GraphQLString},
phone: {type: GraphQLString},
profile: {type: userProfile},
posts: {
type: new GraphQLList(PostType),
resolve(parent,args){
return Post.find({userId: parent.id});
}
}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
post: {
type: PostType,
args: {id: {type: GraphQLID}},
resolve(parent,args){ // to get data from DATABASE
return Post.findById(args.id); // to find a particular post by id
}
},
user: {
type: UserType,
args: {id: {type: GraphQLID}},
resolve(parent,args){
return User.findById(args.id); // to find a particular user
}
},
posts: {
type: new GraphQLList(PostType),
resolve(parent,args){
return Post.find({}); // to find all posts
}
},
users: {
type: new GraphQLList(UserType),
resolve(parent,args){
return User.find({}); // to find all users
}
}
}
});
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addUser: {
type: UserType,
args: {
fname: {type: new GraphQLNonNull(GraphQLString)},
lname: {type: new GraphQLNonNull(GraphQLString)},
email: {type: new GraphQLNonNull(GraphQLString)},
password: {type: new GraphQLNonNull(GraphQLString)},
phone: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parent,args){
let user = new User({
fname: args.fname,
lname: args.lname,
email: args.email,
password: args.password,
phone: args.phone
});
return user.save();
}
},
addPost: {
type: PostType,
args: {
title: {type: new GraphQLNonNull(GraphQLString)},
body: {type: new GraphQLNonNull(GraphQLString)},
date: {type: new GraphQLNonNull(GraphQLString)}
//userId: {type: new GraphQLNonNull(GraphQLString)},
//tags: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parent,args){
let post = new Post({
title: args.title,
body: args.body,
userId: args.userId,
date: args.date
//tags: args.tags,
});
return post.save();
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
});
view raw schema.js hosted with ❤ by GitHub

I want to create a login mutation inside which I want to check if the user with certain email-id exist or not and if exists then password matches or not. This logic should be inside the resolve method but I am unable to figure out how to do it.
Here's my Login React Component
import React, { Component } from 'react'
import { AUTH_TOKEN } from '../../../constants'
import { compose, graphql } from 'react-apollo'
import { loginMutation } from '../../../queries/queries'
export class Login extends Component {
state = {
login: true, // switch between Login and SignUp
email: '',
password: '',
}
handleSubmit = (e) =>{
e.preventDefault();
this.props.loginMutation({
variables: {
email: this.state.email,
password: this.state.password,
}
});
document.getElementById('email').value="";
document.getElementById('password').value="";
}
handleChange = (e) =>{
this.setState({
[e.target.id]: e.target.value
})
}
render() {
return (
<div className="container-fluid">
<div className="form row">
<div className="col-lg-3"></div>
<form className="col-lg-6" onSubmit={this.handleSubmit} onCompleted = {data => this._confirm(data)}>
<div>
<h1 className="text-white"><center>Login to Your Account</center></h1>
</div>
<div className="form-group">
<label htmlFor="email">Email</label>
<input id="email" type="text" className="form-control" onChange={this.handleChange}/>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input id="password" type="text" className="form-control" onChange={this.handleChange}/>
</div>
<div>
<button className="btn btn-primary submitbutton btn-lg btn-block" type="submit">Login
</button>
</div>
</form>
</div>
</div>
)
}
_confirm = async data => {
const { token } = this.state.login ? data.login : data.login
this._saveUserData(token)
this.props.history.push(`/`)
}
_saveUserData = token => {
localStorage.setItem(AUTH_TOKEN, token)
}
}
export default compose(
graphql(loginMutation,{name: 'loginMutation'})
)(Login);
view raw Login.js hosted with ❤ by GitHub

I also want to keep track of the token so that the user remains logged in if he or she refresh the page.
If someone has the solution please let me know 🙏

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay