API Authentication not working

Hey, its me again. Okay so i followed the sapphire docs and made the authenticated precondition be
export const authenticated = () =>
createFunctionPrecondition(
(request: ApiRequest) => Boolean(request.auth?.token),
(_request: ApiRequest, response: ApiResponse) => response.error(HttpCodes.Unauthorized)
);
export const authenticated = () =>
createFunctionPrecondition(
(request: ApiRequest) => Boolean(request.auth?.token),
(_request: ApiRequest, response: ApiResponse) => response.error(HttpCodes.Unauthorized)
);
and i make the api call to refresh/fetch a user from the endpoint users/@me but i get { error: "Cannot read properties of null (reading 'id')" } when i log the response data returned here so that means auth is null? i have credentials: 'include' in the request tho
Solution:
@Oreo ™ Try using the fetch on the event object from the params. It's SvelteKit's special fetch that can include credentials and such. ```ts import type { Handle } from '@sveltejs/kit'; export const handle: Handle = async ({ event, resolve }) => {...
Jump to solution
17 Replies
Oreo ™
Oreo ™OP13mo ago
No description
No description
Oreo ™
Oreo ™OP13mo ago
I just struggle with oauth api stuff
Favna
Favna13mo ago
Wild shot while being half awake but can you ditch the class with statics and just export functions? If you want to group them still you can use a namespace namespace ApiClient { export function aaa() { } }
Favna
Favna13mo ago
and when you go to the devtools -> application -> cookies you can see your auth cookie there?
Favna
Favna13mo ago
also is the domain correct? if you use api.oreotm.xyz then Domain should be .oreotm.xyz. Mind that leading .
No description
Oreo ™
Oreo ™OP13mo ago
yup i see CARDINAL_AUTH and the domain is .oreotm.xyz
Oreo ™
Oreo ™OP13mo ago
No description
Oreo ™
Oreo ™OP13mo ago
No description
Oreo ™
Oreo ™OP13mo ago
https://stackoverflow.com/questions/34558264/fetch-api-with-cookie This said use 'same-origin' but nothing changed
Stack Overflow
Fetch API with Cookie
I am trying out the new Fetch API but is having trouble with Cookies. Specifically, after a successful login, there is a Cookie header in future requests, but Fetch seems to ignore that headers, an...
Oreo ™
Oreo ™OP13mo ago
No description
No description
Oreo ™
Oreo ™OP13mo ago
the endpoint works fine its just that the cookie isnt sent through ok so i found a "fix"
const user = await ApiClient.fetchUser(`CARDINAL_AUTH=${cookie}`);
const user = await ApiClient.fetchUser(`CARDINAL_AUTH=${cookie}`);
but aint no way this is acceptable why doesnt credentials: 'include' just work
Favna
Favna13mo ago
Didn't get back to you before on this. Is your bot open source? And does it work when running through 127.0.0.1 in dev?
Favna
Favna13mo ago
@Helpers btw if anyoen can help oreo
Solution
KB
KB13mo ago
@Oreo ™ Try using the fetch on the event object from the params. It's SvelteKit's special fetch that can include credentials and such.
import type { Handle } from '@sveltejs/kit';

export const handle: Handle = async ({ event, resolve }) => {
const { fetch } = event;

await fetch('https://example.com', {
method: 'GET'
});

return resolve(event);
};
import type { Handle } from '@sveltejs/kit';

export const handle: Handle = async ({ event, resolve }) => {
const { fetch } = event;

await fetch('https://example.com', {
method: 'GET'
});

return resolve(event);
};
KB
KB13mo ago
SvelteKit docs
Loading data • SvelteKit documentation
Want results from more Discord servers?
Add your server