N
Nuxtā€¢2y ago
salomon_.

Correct way to get cookie from the rest api

Hello, I am wondering what's the correct place to get csrftoken from backend rest api and send to the user? I use ssr, so I want to make it on server side. So in my attempt I created server/api/csrftoken.ts where I get the token from backend, then created server/middleware/auth.ts with the following content:
export default defineEventHandler(async (event) => {
const { csrftoken } = await $fetch('/api/csrftoken');

setCookie(event, 'csrftoken', csrftoken);
});
export default defineEventHandler(async (event) => {
const { csrftoken } = await $fetch('/api/csrftoken');

setCookie(event, 'csrftoken', csrftoken);
});
But it doesn't work, I got 500 error from the Nitro. Maybe there is a correct way?
7 Replies
salomon_.
salomon_.OPā€¢2y ago
@Zampa I don't think this is an answer to my initial question. Your message showing how to get cookie on server side, but I am asking how to get cookie from backend rest api and send it to the user. Thank you anyway. Still looking for a solutionšŸ„¹
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
salomon_.
salomon_.OPā€¢2y ago
@rohrig The CSRF token is generated on backend side (Django) so I need to get it from there to be able to work with the rest api. Thanks for the links, I will check it out. @rohrig where should I get csrftoken in this case?
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
salomon_.
salomon_.OPā€¢2y ago
@rohrig Let me explain a bit. So, now I have a dashboard built with django as a rest api and vue 3 (spa) on frontend side. I have a static auth pages, login, signup, etc served by django so it generates and pass token and session to the client. The index.html of spa app is also served by django so I always have the csrftoken to do a rest api like post etc. So I have a kinda hybrid app where we use static django pages for login and then redirect to the spa. But now I need to ged rid of the static pages. So looking for a way to do an auth flow
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server