# Session Handling Headless to Native

**URL:** <https://forum.shopware.com/t/session-handling-headless-to-native/67597>\
**Category:** Programming (EN)\
**Created:** [11. Juni 2020 um 07:18 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597 "2020-06-11T07:18:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![edinhobl](https://avatars.discourse-cdn.com/v4/letter/e/e8c25b/32.png) [@edinhobl](https://forum.shopware.com/u/edinhobl)\
**Post date:** [11. Juni 2020 um 07:18 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/1 "2020-06-11T07:18:23Z")

</div>

I am tryin to implement a custom template based webshop I bought from the net. Currently I am accessing the API via Ajax. My template now handles the adding of products into the cart, removeing them and so on. But now when I reload the page the sw-context-token is gone. **How to handle that reload with the token?** In the shopware frontend it works after reloading. Also: **How is it possible after the cart is completed by the user to redirect the user to the payment process in the shopware storefront so that a can do a „session handover“?**

---

<div class="post-metadata">

**Author:** ![mit3233](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mit3233](https://forum.shopware.com/u/mit3233)\
**Post date:** [29. Juni 2020 um 12:59 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/2 "2020-06-29T12:59:49Z")

</div>

\*push\* I have the same problem. Would like to login a user via API and switch to the Shopware Frontend to process the checkout.

---

<div class="post-metadata">

**Author:** ![edinhobl](https://avatars.discourse-cdn.com/v4/letter/e/e8c25b/32.png) [@edinhobl](https://forum.shopware.com/u/edinhobl)\
**Post date:** [4. Juli 2020 um 15:48 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/3 "2020-07-04T15:48:11Z")

</div>

Any solutions to this?

---

<div class="post-metadata">

**Author:** ![mit3233](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mit3233](https://forum.shopware.com/u/mit3233)\
**Post date:** [6. Juli 2020 um 06:29 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/4 "2020-07-06T06:29:07Z")

</div>

I have no solution for the moment. It seems that Shopware doens’t support this function. You can use the REST Api to log in a user but you can’t switch to the Frontend and take the Session to proceed…

---

<div class="post-metadata">

**Author:** ![Kassor](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@Kassor](https://forum.shopware.com/u/Kassor)\
**Post date:** [21. Juli 2020 um 11:02 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/5 "2020-07-21T11:02:02Z")

</div>

Are there new information?

---

<div class="post-metadata">

**Author:** ![saibotd](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@saibotd](https://forum.shopware.com/u/saibotd)\
**Post date:** [9. September 2020 um 07:46 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/6 "2020-09-09T07:46:48Z")

</div>

I can help you with the first question: You have to save&nbsp;sw-context-token by yourself and attach it to any subsequent request.  
For example, you could store it in localStorage (localstorage.setItem(‚sw-context-token‘, res.data[‚sw-context-token‘])) or in a cookie.

---

<div class="post-metadata">

**Author:** ![saibotd](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@saibotd](https://forum.shopware.com/u/saibotd)\
**Post date:** [9. September 2020 um 07:59 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/7 "2020-09-09T07:59:08Z")

</div>

For the second question I’ve opened an issue on GitHub:&nbsp;[Question/Request: Session handover from API to native sessions · Issue #1314 · shopware/platform · GitHub](https://github.com/shopware/platform/issues/1314). Hopefully someone from the team can point us to the right direction.

---

<div class="post-metadata">

**Author:** ![mit3233](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@mit3233](https://forum.shopware.com/u/mit3233)\
**Post date:** [9. September 2020 um 19:03 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/8 "2020-09-09T19:03:30Z")

</div>

@saibotd‍&nbsp; For second question: I also saved the context token to cookie and than i plan to read it out via shopware plugin and give the basket opend via api the token from the shopware frontend and override it in the Cookie. So Shopware and the website where i opend the cart, will have the same context token. Thats my solution for now…

---

<div class="post-metadata">

**Author:** ![saibotd](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@saibotd](https://forum.shopware.com/u/saibotd)\
**Post date:** [30. September 2020 um 09:24 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/9 "2020-09-30T09:24:09Z")

</div>

@mit3233‍ This is also what I"m doing now - it actually works rather well. For anyone looking for a quick solution:

```
     /**
     * @Route("/set/{cartId}", name="frontend.cart-transfer.set", methods={"GET"}, defaults={"XmlHttpRequest"=true})
     */
    public function setCart($cartId, Request $request, SalesChannelContext $context): Response
    {
        $swContextToken = $this->get('session')->get('sw-context-token');
        if ($swContextToken != $cartId)
            $this->get('session')->set('sw-context-token', $cartId);
        return $this->redirectToRoute("frontend.checkout.cart.page");
    }

```

$cartId is the `sw-context-token` from the API.

---

<div class="post-metadata">

**Author:** ![FelixWienberg](https://avatars.discourse-cdn.com/v4/letter/f/a87d85/32.png) [@FelixWienberg](https://forum.shopware.com/u/FelixWienberg)\
**Post date:** [10. Dezember 2020 um 14:41 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/10 "2020-12-10T14:41:44Z")

</div>

@saibotd‍ &nbsp;Thanks for this solution. It works really well for synching the cart, but do you have any Idea if it would be possible to do something similiar with the login status of the user?

I’d relly like to prevent that a&nbsp;user who&nbsp;already has logged in through the API on our website to have to log in a second time for the checkout process.

---

<div class="post-metadata">

**Author:** ![FelixWienberg](https://avatars.discourse-cdn.com/v4/letter/f/a87d85/32.png) [@FelixWienberg](https://forum.shopware.com/u/FelixWienberg)\
**Post date:** [11. Dezember 2020 um 16:17 UTC](https://forum.shopware.com/t/session-handling-headless-to-native/67597/11 "2020-12-11T16:17:38Z")

</div>

Please ignore my last comment!

The problem of the lost login status of the user was caused by having different sales-channels for the headless and the native enviroment.&nbsp;

&nbsp;
