I am using directus sdk in my react app.When using mode "jwt" my token get expires after some time. So i tried to use mode "cookie". But getting error after login.
I tried with directus sdk 6.0.1 and 6.1.1
Configuration code for the same:
const client = new DirectusSDK({
url: baserUrl,
project: projectName,
storage: localStorage.getItem('authToken')
tokenExpirationTime: 86400000
});
As per the documentation for using cookie mode,
client.login({
url: "https://demo.directus.io",
email: "[email protected]",
password: "password",
mode: "cookie"
});
and this didn't worked for me. and I altered like this.
function login(data) {
return client.login({
email: data.email,
password: data.password,
persist: true
},{mode:'cookie'}).then((response) => response).catch((error) => {
const str = JSON.stringify(error);
throw JSON.parse(str);
});
}
and then the request executed for login only. the calls after that returned a 401 (Unauthorized request).
Can you check the developer console if you are on Chrome? There were some issues with Chrome blocking cookies without SameSite attribute.
But the localStorage/sessionStorage should work, it does on a couple of my projects
check directus/directus-8-legacy#2339
Also, the Directus app itself uses the SDK with Cookie mode, so it surely works there 馃檪
@Kematia I tried on firefox and chrome. The issue exists on both case.
Did you check the developer console? Firefox should not block cookies right now. Can you send us the details of the XHR request?
@Kematia
Please see the XHR request detail here. https://gist.github.com/UnniKrishnaPanicker/afb43f836bc9ce19df3147d6243ae071
It's just a mistake in the documentation. login() expects credentials and options and mode is not part of ILoginCredentials, but rather part of ILoginOptions. So documentation needs an update.
Thanks @dennis-f! Would it be possible to submit a quick PR for the change to correct this?
Is it really that hard to update docs?
It's still wrong in there.
And while on it: Why options anyway? And when you really want to split that info, why is 'persit' still "allowed" (looked up) in ILoginCredentials too? And then it's put in the ILoginBody anyway ;)
Btw: cookie can't be read/shown by js, okay, but shouldn't i see it in the browsers dev-tools?
I have no persistance neither jwt nor cookie mode. I don't understand how this absolute basic stuff is not working ...
/SDK-6.3.0 + Directus 8.8.1
@michapixel this is all free and open source software. If you see something that needs fixing, please fix it yourself or open an issue and hope that somebody else will fix it for you out of the kindness of their heart.
Most helpful comment
It's just a mistake in the documentation.
login()expects credentials and options andmodeis not part ofILoginCredentials, but rather part ofILoginOptions. So documentation needs an update.