feat: add login mutation
This commit is contained in:
30
packages/backend/src/graphql/mutations/login.ts
Normal file
30
packages/backend/src/graphql/mutations/login.ts
Normal 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;
|
@@ -9,6 +9,7 @@ import createFlow from './mutations/create-flow';
|
|||||||
import updateFlow from './mutations/update-flow';
|
import updateFlow from './mutations/update-flow';
|
||||||
import createStep from './mutations/create-step';
|
import createStep from './mutations/create-step';
|
||||||
import executeStep from './mutations/execute-step';
|
import executeStep from './mutations/execute-step';
|
||||||
|
import login from './mutations/login';
|
||||||
|
|
||||||
const rootMutation = new GraphQLObjectType({
|
const rootMutation = new GraphQLObjectType({
|
||||||
name: 'Mutation',
|
name: 'Mutation',
|
||||||
@@ -22,7 +23,8 @@ const rootMutation = new GraphQLObjectType({
|
|||||||
createFlow,
|
createFlow,
|
||||||
updateFlow,
|
updateFlow,
|
||||||
createStep,
|
createStep,
|
||||||
executeStep
|
executeStep,
|
||||||
|
login
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
13
packages/backend/src/graphql/types/user.ts
Normal file
13
packages/backend/src/graphql/types/user.ts
Normal 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;
|
@@ -32,6 +32,10 @@ class User extends Base {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
login(password: string) {
|
||||||
|
return bcrypt.compare(password, this.password);
|
||||||
|
}
|
||||||
|
|
||||||
async generateHash() {
|
async generateHash() {
|
||||||
this.password = await bcrypt.hash(this.password, 10);
|
this.password = await bcrypt.hash(this.password, 10);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user