3

I tried to established a connection between my app and Drupal services. When I try to login to remote Drupal site using Mozilla add-on Poster everything is working... POST request return OK 200 and I'm logged into the site.

When I make the POST request from my app, I always get 200 repose OK, but when I visit the site in browser I'm not logged in. I suppose that Cookie header is not correctly structured in POST request...but I don't know how to fix it.

Does anyone have idea how can I solve this?

1 Answer 1

1

I had the same problem in Drupal 8. The AJAX POST isn't retrieving the cookie from user/login as Drupal does a redirect right away to another page (user usually) which is where you get your 200. And there is nothing (nothing) you can do to interrupt that redirect as its wired into the browser. Postman in Chrome (and I presume Poster in Firefox) don't have those restrictions.

In Drupal 7 the Services module provides a login endpoint. After that you can retrieve the session ID and create the cookie by hand. See https://groups.drupal.org/node/358308

In Drupal 8 that endpoint doesn't exist (and there is debate as to whether it should). What I do is:

  1. In the app I create a regular login form with a return destination to my app <form action="/user/login?destination=/app">
  2. Once that's done I retrieve a CSRF token as by then the cookie is set. In Drupal 8 you get that at rest/session/token. With Drupal 7 and the Services module you retrieve it at a different path based on your end point.
  3. After that every request goes back with the X-CSRF-Token header and the cookie isn't used anymore.
5
  • Hi, I use Drupal 7. I get the session cookie with the POST request from my app, but when I load the remote site in browser I'm not logged in.
    – Vesna
    Commented Mar 17, 2015 at 12:38
  • Like I say. The cookie isn't set and stored in the browser. You have to pass it in on every POST with custom headers. Commented Mar 17, 2015 at 12:47
  • Hi thank you. I don't need to make any authorized post request after I'm logged in. I just need to redirect to remote site and be logged in there. I tried to make a redirect with cUrl to the remote site with the cookie .. and it was working .. I was redirected to remote site as logged in user, but if I click on another location I'm not logged in anymore.It seems that only the first redirect is authorized. Any idea?
    – Vesna
    Commented Mar 18, 2015 at 9:16
  • You can't set a cookie for a domain that you're not on. So what you're trying to do won't work. Try creating a regular form who's action points at the foreign domain as I mentioned in the answer. That should set a cookie in the correct domain. Commented Mar 18, 2015 at 12:00
  • Thank you. It's much clear now. But how will I combine the services endpoint and the form's action url? I must first post request to endpoint and after that redirect to foreign site? How I'll combine that two? Can you post some simple code example? Thank you.
    – Vesna
    Commented Mar 18, 2015 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.