refactor(medusa): cleanup, fix, migrate baseService and therefore fix errors (#1701)

* refactor(medusa): cleanup, fix, migrate baseService and therefore fix errors for ts version > 4.5

* test(medusa): Fix discount tests

* fix(medusa): build

* feat(medusa): Remove unnecessary await
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2022-06-22 12:23:56 +02:00
1 parent 7302d76e12
commit 0e34800573
9 files changed
+460 -444

No files matched your search

+103 -72
View File
@@ -1,21 +1,32 @@
import Scrypt from "scrypt-kdf"
import { BaseService } from "medusa-interfaces"
import { AuthenticateResult } from "../types/auth"
import { User } from "../models/user"
import { Customer } from "../models/customer"
import { User, Customer } from "../models"
import { TransactionBaseService } from "../interfaces"
import UserService from "./user"
import CustomerService from "./customer"
import { EntityManager } from "typeorm"
type InjectedDependencies = {
manager: EntityManager
userService: UserService
customerService: CustomerService
}
/**
* Can authenticate a user based on email password combination
* @extends BaseService
*/
class AuthService extends BaseService {
constructor({ userService, customerService }) {
super()
class AuthService extends TransactionBaseService<AuthService> {
protected manager_: EntityManager
protected transactionManager_: EntityManager | undefined
protected readonly userService_: UserService
protected readonly customerService_: CustomerService
/** @private @const {UserService} */
constructor({ manager, userService, customerService }: InjectedDependencies) {
super({ manager, userService, customerService })
this.manager_ = manager
this.userService_ = userService
/** @private @const {CustomerService} */
this.customerService_ = customerService
}
@@ -25,7 +36,10 @@ class AuthService extends BaseService {
* @param {string} hash - the hash to compare against
* @return {bool} the result of the comparison
*/
async comparePassword_(password: string, hash: string): Promise<boolean> {
protected async comparePassword_(
password: string,
hash: string
): Promise<boolean> {
const buf = Buffer.from(hash, "base64")
return Scrypt.verify(buf, password)
}
@@ -39,30 +53,36 @@ class AuthService extends BaseService {
* error: a string with the error message
*/
async authenticateAPIToken(token: string): Promise<AuthenticateResult> {
if (process.env.NODE_ENV === "development") {
return await this.atomicPhase_(async (transactionManager) => {
if (process.env.NODE_ENV?.startsWith("dev")) {
try {
const user: User = await this.userService_
.withTransaction(transactionManager)
.retrieve(token)
return {
success: true,
user,
}
} catch (error) {
// ignore
}
}
try {
const user: User = await this.userService_.retrieve(token)
const user: User = await this.userService_
.withTransaction(transactionManager)
.retrieveByApiToken(token)
return {
success: true,
user,
}
} catch (error) {
// ignore
return {
success: false,
error: "Invalid API Token",
}
}
}
try {
const user: User = await this.userService_.retrieveByApiToken(token)
return {
success: true,
user,
}
} catch (error) {
return {
success: false,
error: "Invalid API Token",
}
}
})
}
/**
@@ -79,35 +99,39 @@ class AuthService extends BaseService {
email: string,
password: string
): Promise<AuthenticateResult> {
try {
const userPasswordHash: User = await this.userService_.retrieveByEmail(
email,
{
select: ["password_hash"],
}
)
return await this.atomicPhase_(async (transactionManager) => {
try {
const userPasswordHash: User = await this.userService_
.withTransaction(transactionManager)
.retrieveByEmail(email, {
select: ["password_hash"],
})
const passwordsMatch = await this.comparePassword_(
password,
userPasswordHash.password_hash
)
const passwordsMatch = await this.comparePassword_(
password,
userPasswordHash.password_hash
)
if (passwordsMatch) {
const user = await this.userService_.retrieveByEmail(email)
if (passwordsMatch) {
const user = await this.userService_
.withTransaction(transactionManager)
.retrieveByEmail(email)
return {
success: true,
user: user,
return {
success: true,
user: user,
}
}
} catch (error) {
console.log("error ->", error)
// ignore
}
} catch (error) {
// ignore
}
return {
success: false,
error: "Invalid email or password",
}
return {
success: false,
error: "Invalid email or password",
}
})
}
/**
@@ -124,32 +148,39 @@ class AuthService extends BaseService {
email: string,
password: string
): Promise<AuthenticateResult> {
try {
const customerPasswordHash: Customer =
await this.customerService_.retrieveByEmail(email, {
select: ["password_hash"],
})
if (customerPasswordHash.password_hash) {
const passwordsMatch = await this.comparePassword_(
password,
customerPasswordHash.password_hash
)
return await this.atomicPhase_(async (transactionManager) => {
try {
const customerPasswordHash: Customer = await this.customerService_
.withTransaction(transactionManager)
.retrieveByEmail(email, {
select: ["password_hash"],
})
if (customerPasswordHash.password_hash) {
const passwordsMatch = await this.comparePassword_(
password,
customerPasswordHash.password_hash
)
if (passwordsMatch) {
const customer = await this.customerService_.retrieveByEmail(email)
return {
success: true,
customer,
if (passwordsMatch) {
const customer = await this.customerService_
.withTransaction(transactionManager)
.retrieveByEmail(email)
return {
success: true,
customer,
}
}
}
} catch (error) {
// ignore
}
} catch (error) {
// ignore
}
return {
success: false,
error: "Invalid email or password",
}
return {
success: false,
error: "Invalid email or password",
}
})
}
}