I’m building my first React / Ionic app and I’m having trouble saving the session cookie. My backend server is Drupal 8; I want to use the session cookie instead of OAuth because I want users to be able to log in to my app and then automatically be logged in to the Drupal website as well if they open up a website link inside the app.
Drupal session cookies are returned as HttpOnly
and Secure
.
So, after reading this thread on cookie-based auth, I installed the capacitor-community/http plugin and started testing it on an Android device.
I don’t understand cookies very well so I may be missing something obvious, but I can’t figure out how to “save” the cookie when logging in.
When you log in to Drupal in the browser, the session cookie is saved automatically.
I have successfully logged into my Drupal site via Postman like this:
https://www.example.com/user/login?_format=json
Headers: Content-Type: application/json
Body: raw
{"mail": "test@example.com","pass": "password"}
This also returns the session cookie in Postman’s “Cookies” area.
So in my React app, I tried to log in like this:
export const basicHeaders: HeadersInit = new Headers({
Accept: 'application/json',
});
async function login(email: string, password: string): Promise<void> {
const loginPost = {
mail: email,
pass: password,
};
const { Http } = Plugins;
await Http.request(`${baseUrl}/user/login?_format=json`, {
method: 'POST',
headers: basicHeaders,
data: JSON.stringify(loginPost),
});
However, with this code, the cookie isn’t saved.
The http plugin documentation has some examples for setCookie
and getCookies
, but no example of how to do it when you need to log in to get the cookie.
Any advice about how to save a cookie on login would be much appreciated.