adding login GraphQL resolver
This commit is contained in:
@@ -1,36 +1,26 @@
|
||||
function loginUserResolver() {
|
||||
return {
|
||||
Subscription: {
|
||||
loginUser: {
|
||||
subscribe: async ({ email, password }: { email: string; password: string; }, { pubsub }: { pubsub }: { EventEmitter }) => {
|
||||
loginUser({ email, password });
|
||||
return pubsub.asyncIterator('LOGIN_SUCCESS' || 'LOGIN_FAILURE');
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
type userLoginInput = {
|
||||
email: String!
|
||||
password: String!
|
||||
}
|
||||
|
||||
function logoutUserResolver() {
|
||||
return {
|
||||
Subscription: {
|
||||
logoutUser: {
|
||||
subscribe: async ({ email }: { email: string }, { pubsub }: { pubsub }: { EventEmitter }) => {
|
||||
logoutUser({ email });
|
||||
return pubsub.asyncIterator('LOGOUT_SUCCESS' || 'LOGOUT_FAILURE');
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function createUserResolver() {
|
||||
return {
|
||||
Mutation: {
|
||||
createUser: async ({ email, password }: { email: string; password: string; }) => {
|
||||
const user = await createUser({ email, password });
|
||||
return user ? "USER_CREATED_SUCCESS" : "USER_CREATION_FAILED";
|
||||
const resolvers = {
|
||||
Mutation: {
|
||||
login: async (_parent, { input: input }, context) => {
|
||||
const { email, password } = input;
|
||||
// Db connection and authentication logic here
|
||||
const user = await context.db.users.findOne({ email });
|
||||
if (!user || user.password !== password || !user.email) {
|
||||
return { userLoginSuccessStatus: false };
|
||||
else if (user && user.password === password && user.email) {
|
||||
return { userLoginSuccessStatus: true };
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
else {
|
||||
return { userLoginSuccessStatus: false };
|
||||
console.log("User not found or unknown error");
|
||||
}
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,30 @@
|
||||
type Login {
|
||||
type userLogin {
|
||||
email: String!
|
||||
password: String!
|
||||
}
|
||||
type LoginResponse {
|
||||
loginStatus: Boolean!
|
||||
type userLoginResponse {
|
||||
userLoginSuccessStatus: Boolean!
|
||||
}
|
||||
|
||||
type Subscription {
|
||||
login: Login!
|
||||
},
|
||||
required: ["email", "password"]
|
||||
|
||||
subscription login($email: String!, $password: String!) {
|
||||
login(email: $email, password: $password) {
|
||||
id
|
||||
email
|
||||
loginStatus
|
||||
type userLogout {
|
||||
email: String!
|
||||
}
|
||||
}
|
||||
type userLogoutResponse {
|
||||
userLoggedOutSuccessStatus: Boolean!
|
||||
}
|
||||
type CreateUser {
|
||||
firstName: String!
|
||||
lastName: String!
|
||||
email: String!
|
||||
password: String!
|
||||
dateOfBirth: String!
|
||||
}
|
||||
type CreateUserResponse {
|
||||
userCreatedStatus: Boolean!
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
login(input: userLogin!): userLoginResponse!
|
||||
logout(input: userLogout!): user LogoutResponse!
|
||||
createUser(input: CreateUser!): CreateUserResponse!
|
||||
}
|
||||
Reference in New Issue
Block a user