admin管理员组

文章数量:1122832

I am using Session from Express-session to got cookies for Login and using CORS in backend server and deploy on Railway, and deploy frontend on Netlify. I have many API but the problem was /Login and /Me, /Login for got email & password, and /Me for got cookies from Express-session. For /Login is got status OK(200) but for /Me got status Unauthorized(401), if I use localhost for Frontend it runs normally but if I use from Netlify it happens

This Backend :

app.use(
  session({
    secret: "any",
    resave: false,
    saveUninitialized: true,
    store: store,
    cookie: {
      expires: new Date(Date.now() + 86400000),
      maxAge: 30 * 24 * 60 * 60 * 1000,
      domain: "https://***lify.app",
      httpOnly: true,
      secure: true,
      sameSite: "none",
    },
  })
);
app.use(
  cors({
    credentials: true,
    origin: "https://***lify.app",
  })
);

and this frontend

axios.defaults.baseURL = "https://***.up.railway.app";
axios.defaults.withCredentials = true;

export const LoginUser = createAsyncThunk(
  "user/LoginUser",
  async (user, thunkAPI) => {
    try {
      const response = await axios.post(
        "https://***-production.up.railway.app/api/login",
        {
          email: user.email,
          password: user.password,
        },
        { withCredentials: true }
      );
      return response.data;
    } catch (error) {
      if (error.response) {
        const message = error.response.data.msg;
        return thunkAPI.rejectWithValue(message);
      }
    }
  }
);

export const getMe = createAsyncThunk("user/getMe", async (_, thunkAPI) => {
  try {
    const response = await axios.get(
      "https://***-production.up.railway.app/api/me",
      { withCredentials: true }
    );
    return response.data;
  } catch (error) {
    if (error.response) {
      const message = error.response.data.msg;
      return thunkAPI.rejectWithValue(message);
    }
  }
});

本文标签: reactjsCannot got cookies from Expresssession in different websiteStack Overflow