New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix SDK usage of Auth (JSON vs Cookie, Static vs Dynamic) #7112
Comments
Seeing that that example starts with Groetjes! |
@rijkvanzanten The |
@rijkvanzanten this was exactly what I was also trying to achieve, so I guess this is the answer to your question above :) |
Bump @rijkvanzanten :) Of in mooi Nederlands: vergeet-me-nietje! |
This it what I use in a svelte project. However you need to make sure that the import { Directus, Auth, LocalStorage, AxiosTransport } from '@directus/sdk'
let sdk = {}
const url = ''
const storage = new LocalStorage() // : new MemoryStorage()
// Transport used to communicate with the server.
const transport = new AxiosTransport(url, storage, async () => {
await auth.refresh() // This is how axios checks for refresh
})
const options = {
mode: 'json',
refresh: { auto: true, time: 3000}
}
// Auth is how authentication is handled, stored, and refreshed.
const auth = new Auth(transport, storage, options)
sdk = new Directus(url, { auth, storage, transport })
export { sdk } |
Thanks for this! However, the problem with this example (and the one in the docs) is that your line which says "// This is how axios checks for refresh" gives me "compile" errors because "auth" is not available there. |
Not sure how to reach you best @rijkvanzanten :) |
You say my name twice holding a lit candle, and I'll appear in your shadow The current implementation of the SDK is clearly not intuitive enough for this sort of use case. I think the best approach is to tweak the SDK a bit to make the switch between doing things "automagically" under the hood, and doing things manual with the tokens in JSON more user friendly. The same thing applies to using the SDK with a static token, having to override the auth class and whatnot just to have it rely on a static token is a bit overkill. Lets use this thread and issue as a way to discuss the ideal way this should work. I believe the changes that would be required to make this intuitive are minimal (most if not all the methods for data are fine, it's mostly the configuration/usage of authentication that's a bit finicky), so hopefully we can figure it out and work it in before the next release 👍🏻 |
I would love to talk you through my usecase and see what we can come up with. I have a working auth implementation for Vue Storefront, but it seems a little bloated. Perhaps schedule a 15 minute call somewhere next week? |
Totally! Are you on our Discord yet? That's a little easier to schedule 🙂 Just shoot me a DM there, my user is |
Hello all, working on fixing the auth for the SDK, and thought I'd outline the scenarios for which we want it to work, so we can better triage how it will work.
Does this seem like it covers everything? Are there any customizations people may want to make that this wouldn't allow for? |
I believe these three would cover all the use cases, yeah
I agree with that. We can just make axios an internal dependency. No need to overcomplicate that. |
Hello all,
So, in the end, my suggestion is to just to handle this with something like (main...joselcvarela:issues/7112): sdk.auth.mode = 'json' Let me hear your thoughts @rijkvanzanten . |
@joselcvarela I think the usage outlined above by @j3n57h0m45 is way too cumbersome for what it is: import { Directus, Auth, LocalStorage, AxiosTransport } from '@directus/sdk'
let sdk = {}
const url = ''
const storage = new LocalStorage() // : new MemoryStorage()
// Transport used to communicate with the server.
const transport = new AxiosTransport(url, storage, async () => {
await auth.refresh() // This is how axios checks for refresh
})
const options = {
mode: 'json',
refresh: { auto: true, time: 3000}
}
// Auth is how authentication is handled, stored, and refreshed.
const auth = new Auth(transport, storage, options)
sdk = new Directus(url, { auth, storage, transport })
export { sdk } Ideally, I'd like that to become as simple as possible, with ways to override certain internals, for example: import { DirectusSDK } from '@directus/sdk';
const sdk = new DirectusSDK({
url: 'https://example.com',
token: 'my-static-secret-token'
}); for static tokens, or import { DirectusSDK } from '@directus/sdk';
const sdk = new DirectusSDK({
url: 'https://example.com',
auth: {
autoRefresh: true
}
}); where it "just works". Then for cases where you need a specific storage override, you can do something like import { DirectusSDK } from '@directus/sdk';
import localForage from 'localforage';
const sdk = new DirectusSDK({
url: 'https://example.com',
auth: {
autoRefresh: true,
storage: localForage.createInstance({ name: 'session-storage' })
}
}); |
@rijkvanzanten yeah I think that too. const sdk = new Directus('http://localhost:8055', 'static_token')
// OR
// const sdk = new Directus('http://localhost:8055')
// await sdk.auth.login({email, password})
await sdk.items('articles').readMany(); But for a more configurable way, we could provide this: const sdk = new Directus('http://localhost:8055')
sdk.auth.mode('json').autoRefresh(true).deltaTime(3000)
sdk.auth.storage.prefix('_my_app_') |
const sdk = new Directus('http://localhost:8055')
sdk.auth.mode('json').autoRefresh(true).deltaTime(3000)
sdk.auth.storage.prefix('_my_app_') That way of relying on method calls for an initial setup feels a little strange to me. Any reason why that can't be part of a config object in the constructor of that Directus class? |
Since we need to declare this function calls anyway, it is more clean. But you've got a point, the well known packages uses a configuration, so maybe we should go that way. PS: I like more the chainable way 😅 |
There's no reason why we couldn't have both tho! I do believe however that for the 80/20 case it's easier to have a single config object to initialize with, and then have the methods available to adjust later on if you ever have the use case to change the values after the fact 👍🏻 |
Preflight Checklist
Describe the Bug
Not sure if it is supposed to work but when I copy paste the advanced example from the SDK docs here it fails to run and errors out at await auth.refresh()
To Reproduce
Copy paste the advanced solution and try to run it.
What version of Directus are you using?
RC86
What version of Node.js are you using?
Na
What database are you using?
Na
What browser are you using?
All
What operating system are you using?
Na
How are you deploying Directus?
Na
The text was updated successfully, but these errors were encountered: