We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
router
Hello!
I use cookies to store authentication token. The fetch function in load does not use original request headers.
fetch
load
Given the following code:
import type { PageServerLoad } from '@analogjs/router'; export const load = async ({ fetch, req }: PageServerLoad) => { console.log('req.url', req.url); console.log('req.headers', req.headers); return { principal: await fetch<{ name: string }>('/api/v1/users/me'), }; };
It logs:
req.url /_analog/pages/-index- An error occurred: FetchError: (401 Unauthorized (/api/v1/users/me)) req.headers { 'content-length': '0', 'user-agent': 'Mozilla/5.0 (Linux x64) node.js/18.15.0 v8/10.2.154.26-node.25', host: 'localhost:5173', connection: 'close', accept: 'application/json, text/plain, */*', 'x-forwarded-for': '127.0.0.1', 'x-forwarded-port': '49226', 'x-forwarded-proto': 'IPv4' }
Is there any ways of using cookies in load function ?
Make API calls from client.
The text was updated successfully, but these errors were encountered:
Yes, it's possible to do, but we will need to add the event to the PageServerLoad object
PageServerLoad
Something like below:
import type { PageServerLoad } from '@analogjs/router'; export const load = async ({ fetch, req, event }: PageServerLoad) => { const cookies = parseCookies(event); console.log('req.url', req.url); console.log('req.headers', req.headers); return { principal: await fetch<{ name: string }>('/api/v1/users/me'), }; };
OR
import type { PageServerLoad } from '@analogjs/router'; export const load = async ({ fetch, req, cookies, event }: PageServerLoad) => { console.log('req.url', req.url); console.log('req.headers', req.headers); return { principal: await fetch<{ name: string }>('/api/v1/users/me'), }; };
Sorry, something went wrong.
Oh! It is that easy 🤦 Thank you for the quick reply and keep the good job on Analog!
feat(vite-plugin-nitro): add event to load function and types
9c2178d
Closes #606
Successfully merging a pull request may close this issue.
Which scope/s are relevant/related to the feature request?
router
Information
Hello!
I use cookies to store authentication token. The
fetch
function inload
does not use original request headers.Given the following code:
It logs:
Is there any ways of using cookies in load function ?
Describe any alternatives/workarounds you're currently using
Make API calls from client.
I would be willing to submit a PR to fix this issue
The text was updated successfully, but these errors were encountered: