pevey / medusa-auth-cognito Goto Github PK
View Code? Open in Web Editor NEWUse AWS Cognito to store customer credentials instead of the Postgres database
License: MIT License
Use AWS Cognito to store customer credentials instead of the Postgres database
License: MIT License
Hey @pevey,
First of all, nice one ๐ช. Except the point that should be in the core (to match existing users during the update) I have a suggestion to simplify the customer service. What do you think of the following
import { CustomerService as MedusaCustomerService, Customer, CustomerGroup, setMetadata } from "@medusajs/medusa"
import { CreateCustomerInput, UpdateCustomerInput } from "@medusajs/medusa/dist/types/customers"
import { MedusaError } from "@medusajs/utils"
import { CustomerRepository } from "../repositories/customer"
export default class CustomerService extends MedusaCustomerService {
private cognitoService: any
protected readonly customerRepository_: typeof CustomerRepository
private emailsMapByPassword = new Map<string, { email: string; action: "create" | "update" }>()
constructor({ cognitoService, customerRepository }) {
// @ts-ignore
super(...arguments)
this.cognitoService = cognitoService
this.customerRepository_ = customerRepository
}
async create(customer: CreateCustomerInput): Promise<Customer> {
return await this.atomicPhase_(async (manager) => {
const { email, password } = customer
this.emailsMapByPassword.set(password, { email, action: "create" })
return await super.create(customer)
})
}
async update(
customerId: string,
update: UpdateCustomerInput
) {
return await this.atomicPhase_(async (manager) => {
const customer = await this.retrieve(customerId, { select: ["email"] })
const { email } = customer
if (update.password) {
this.emailsMapByPassword.set(update.password, { email, action: "update" })
}
return await super.update(customerId, update)
})
}
async hasPassword_(password: string): Promise<string> {
const { email, action } = this.emailsMapByPassword.get(password)
const hash = 'cognito'
try {
if (action === "create") {
const isCreated = await this.cognitoService.createCustomer(email, password)
if (!isCreated) {
this.emailsMapByPassword.delete(password)
throw new MedusaError(MedusaError.Types.UNEXPECTED_STATE, "Could not create customer in Cognito")
}
} else {
await this.cognitoService.setCustomerPassword(email, password).catch(() => {
throw new MedusaError(MedusaError.Types.UNEXPECTED_STATE, "Could not update customer password in Cognito")
})
}
return hash
} finally {
this.emailsMapByPassword.delete(password)
}
}
async delete(customerId: string): Promise<Customer | void> {
return await this.atomicPhase_(async (manager) => {
const removedCustomer = await super.delete(customerId)
await this.cognitoService.deleteCustomer(removedCustomer.email).catch(() => {
throw new MedusaError(MedusaError.Types.UNEXPECTED_STATE, "Could not delete customer in Cognito")
})
return removedCustomer
})
}
}
The main idea is to interfer as less as possible with the core methods to prevent version upgrade to break the plugin.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.