Adds endpoints to manage customers (#54)

This commit is contained in:
Sebastian Rindom
2020-05-07 13:31:38 +02:00
committed by GitHub
parent 516bc7675d
commit 6a78df1ecd
24 changed files with 858 additions and 170 deletions
+49 -1
View File
@@ -6,10 +6,14 @@ import { BaseService } from "medusa-interfaces"
* @implements BaseService
*/
class AuthService extends BaseService {
constructor({ userService }) {
constructor({ userService, customerService }) {
super()
/** @private @const {UserService} */
this.userService_ = userService
/** @private @const {CustomerService} */
this.customerService_ = customerService
}
/**
@@ -38,6 +42,7 @@ class AuthService extends BaseService {
}
}
}
/**
* Authenticates a given user based on an email, password combination. Uses
* bcrypt to match password with hashed value.
@@ -70,6 +75,49 @@ class AuthService extends BaseService {
}
}
}
/**
* Authenticates a customer based on an email, password combination. Uses
* bcrypt to match password with hashed value.
* @param {string} email - the email of the user
* @param {string} password - the password of the user
* @return {{ success: (bool), user: (object | undefined) }}
* success: whether authentication succeeded
* user: the user document if authentication succeded
* error: a string with the error message
*/
async authenticateCustomer(email, password) {
try {
const customer = await this.customerService_.retrieveByEmail(email)
if (!customer.password_hash) {
return {
success: false,
error: "Invalid email or password",
}
}
const passwordsMatch = await bcrypt.compare(
password,
customer.password_hash
)
if (passwordsMatch) {
return {
success: true,
customer,
}
} else {
return {
success: false,
error: "Invalid email or password",
}
}
} catch (error) {
return {
success: false,
error: "Invalid email or password",
}
}
}
}
export default AuthService