feat: add login mutation

This commit is contained in:
Ali BARIN
2021-12-20 22:09:20 +01:00
parent 8b05489e40
commit e0f380026e
4 changed files with 50 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import User from '../../models/user';
import userType from '../types/user';
type Params = {
email: string,
password: string
}
const loginResolver = async (params: Params) => {
const user = await User.query().findOne({
email: params.email,
});
if (user && await user.login(params.password)) {
return user;
}
throw new Error('User could not be found.')
}
const login = {
type: userType,
args: {
email: { type: GraphQLNonNull(GraphQLString) },
password: { type: GraphQLNonNull(GraphQLString) }
},
resolve: (_: any, params: any) => loginResolver(params)
};
export default login;

View File

@@ -9,6 +9,7 @@ import createFlow from './mutations/create-flow';
import updateFlow from './mutations/update-flow';
import createStep from './mutations/create-step';
import executeStep from './mutations/execute-step';
import login from './mutations/login';
const rootMutation = new GraphQLObjectType({
name: 'Mutation',
@@ -22,7 +23,8 @@ const rootMutation = new GraphQLObjectType({
createFlow,
updateFlow,
createStep,
executeStep
executeStep,
login
}
});

View File

@@ -0,0 +1,13 @@
import { GraphQLObjectType, GraphQLString, GraphQLList, GraphQLInt } from 'graphql';
const userType = new GraphQLObjectType({
name: 'User',
fields: {
id: { type: GraphQLString },
email: { type: GraphQLString },
createdAt: { type: GraphQLString },
updatedAt: { type: GraphQLString }
}
})
export default userType;

View File

@@ -32,6 +32,10 @@ class User extends Base {
}
})
login(password: string) {
return bcrypt.compare(password, this.password);
}
async generateHash() {
this.password = await bcrypt.hash(this.password, 10);
}