Giter VIP home page Giter VIP logo

medusa-auth-cognito's People

Contributors

pevey avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

appsensus

medusa-auth-cognito's Issues

CustomerService suggestion

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.