Search code examples
spartacus-storefront

How to manually authenticate user/client in spartacus?


right now I'm trying to create a proof of concept on spartacus. The concept is to manually check out a cart for an anonymous customer. I came to a point, where I was wondering whether it is possible to authenticate a client or user manually with spartacus.

So my question in general: What is the best practice to manually authenticate the client/user in spartacus? Some code examples would be great :)

Best regards and thanks in advance


Solution

  • There are a few things you have to do in case of a guest checkout.

    First thing is to enable guest checkout in the config with:

    checkout: { guest: true }
    

    Then the first step of the checkout should be /checkout-login page. There user needs to provide email address that is assigned to a specific cart.

    We recognize guest checkout by that assigned email to the cart. Otherwise, checkout components will behave like it is normal checkout. Because of that, there are calls for user addresses or payments.

    In case you want everything on one page (setting email address, delivery, payments etc.) you have to override default implementation for checkout components to avoid all these calls for the logged user.

    And calls for setting address and payment in case of guest checkout doesn't have to by authorized (you can see that on demo guest checkout these calls have undefined in Authorization header).

    And for client authorization, it is nicely described in https://stackoverflow.com/a/60821200/4666829