I'm trying out Directus as a faster way to deliver my API's. But I'm having trouble to fetch data with a static token authentication.
Below is my client setup:
import { createDirectus, staticToken,rest, withToken} from "@directus/sdk";
const directus = createDirectus(process.env.NEXT_PUBLIC_API_URL as string).with(rest());
const client = directus.with(staticToken(process.env.ADMIN_TOKEN as string))
export default client;
and here is my fetch posts implementation
const getAllPosts = async () => {
try {
const response = await client.request(readItems("posts", { fields: ["id", "title"] }));
const posts = response || [];
return posts;
} catch (error) {
console.log('error', error);
}
};
The response is the following:
response: Response {
[Symbol(realm)]: null,
[Symbol(state)]: {
aborted: false,
rangeRequested: false,
timingAllowPassed: true,
requestIncludesCredentials: true,
type: 'default',
status: 401,
timingInfo: [Object],
cacheState: '',
statusText: 'Unauthorized',
headersList: [HeadersList],
urlList: [Array],
body: [Object]
},
[Symbol(headers)]: HeadersList {
cookies: null,
[Symbol(headers map)]: [Map],
[Symbol(headers map sorted)]: null
}
}
}
error {
errors: [ { message: 'Invalid user credentials.', extensions: [Object] } ],
response: Response {
[Symbol(realm)]: null,
[Symbol(state)]: {
aborted: false,
rangeRequested: false,
timingAllowPassed: true,
requestIncludesCredentials: true,
type: 'default',
status: 401,
timingInfo: [Object],
cacheState: '',
statusText: 'Unauthorized',
headersList: [HeadersList],
urlList: [Array],
body: [Object]
},
[Symbol(headers)]: HeadersList {
cookies: null,
[Symbol(headers map)]: [Map],
[Symbol(headers map sorted)]: null
}
}
}
in a 2022 directus thread thread the auth process for static tokes looked like this:
const { Directus } = require("@directus/sdk");
(async () => {
const directus = new Directus("...", { auth: { staticToken: "your_static_token" } } )
const playlist = await directus.items("playlist").readOne(5)
console.log(playlist)
})()
Though with the latest SDK its no longer working...
It looks like I am not authorized to access the data even though my secret token (ADMIN_TOKEN) is set to an admin user (which has access to all the tables). In my opinion the documentation deserved improvements because this is not clear at all. Anyone can help me to find out the problem?