Next.js Discord

Discord Forum

how to remake this nuxt code to next.js?(CSR)

Unanswered
Mandarin fish posted this in #help-forum
Open in Discord
Mandarin fishOP
I need to redo this code from nuxt to next.js to send request with session, in nuxt I used the code provided below but in next there is no useRequestHeaders is there any altarnative for CSR?

Code:
headers: useRequestHeaders(["cookie"]), withCredentials: true

P.S: im using axios for requests

29 Replies

@Mandarin fish yes
with axios it should be like this:
import axios from 'axios';

axios({
  method: 'post',
  url: `${window.location.origin}/api`,
  data: formData,
  withCredentials: true, // this line
})
@Mini Lop but sessions on backend not saving
are you sure it hasn't been sent in the backend? try console.logging request.cookie
which backend are you using?
@Anay-208 which backend are you using?
Mini Lop
we using express.js on backend
i think you dont understand me
if I'm right, you're using withCredentials property, and credentials are not reaching in the backend, right?
@Anay-208 if I'm right, you're using withCredentials property, and credentials are not reaching in the backend, right?
Mini Lop
After authorization, a session is created on the backend. And I don't know how to make it so that when a second request is made to the backend, where this session is required, it is taken into account
I using translator and i can write wrong
in nuxt we used headers: useRequestHeaders(["cookie"]), withCredentials: true, but in next we dont know how save sessions?
@Anay-208 Can you send your back end code?
Mini Lop
we have no troubles with backend
if I login from browser in backend, all is normal
we cant use backend sessions from frontend
@Mini Lop we cant use backend sessions from frontend
That’s why I’m asking a small part of backend code, where sessions are being checked
@Mini Lop we have no troubles with backend
or answer my 1 question. in backend, are you checking session data in the header?
@Anay-208 That’s why I’m asking a small part of backend code, where sessions are being checked
Mini Lop
/api/get/sesssion
exports.getUserSession = async (req, res) => {
    try {
        if (req.session.user) {
            const session = req.session.user;

            return res.status(200).json({
                "session": session
            });
        } else {
            return res.status(200).json({
                "session": null
            });
        }
    } catch (error) {
        console.log(error.stack);
        return res.status(500).json({
            "error": "Ошибка сервера"
        })
    }
}
in /api/login/callback im using
      req.session.user = user;

for create session
When i visit this api page from browser and i have session, api return to me my session
If i visit this api page from browser and i have not session, api return to me {session:null}
but in frontend api always return {session:null}
Authorization works, and I'm 100 percent sure of that because I can authorize through the frontend
Mini Lop
Fixed.
@Mini Lop Fixed.
Can you send the solution, and mark that message as solution?