fair-rose
fair-rose15mo ago

cookies

hi all I use useTable useForm and another hooks to get the data from the backend in the backend (nodejs, mongo) I created a session to save the login status I found that every time I move from page to another I get a new session in my DB I think its maybe because everytime I get a new cookie ID from the front end anyone know if there is another reason or can explain the cookies in refine ? thank you
7 Replies
kapa.ai
kapa.ai15mo ago
Hey @Deleted User! I'm the kapa.ai support bot. I've searched through the docs to try to answer your question. :wave:
kapa.ai
kapa.ai15mo ago
---------------------- Hmm, I don't know enough to give you a confident answer yet about cookies and sessions in refine. However, you can refer to the refine documentation for more information on hooks and other features: https://refine.dev/docs ----------------------
Overview | refine
What is refine?
kapa.ai
kapa.ai15mo ago
<@1073419683014922323> kapa.ai is still learning and improving, please let us how it did by reacting below
eastern-cyan
eastern-cyan15mo ago
cookies in refine not different from any react/javascript application. example of cookie usage https://github.com/refinedev/refine/blob/next/examples/with-remix-headless/app/authProvider.ts#L24 this is remix example but not different from nextjs or cra appliation. thats the beautify of refine's authProvider abstraction. explanation: 1. on authProvider.login sets cookie 2. on authProvider.check checks user has cookie, if not redirecting to login page. 3. on authProvider.onError if API returns 401, redirecting to login page are you using refine@3 or refine@4 ? 3 or 4 logics are the same but returning values are changed with refine@4
fair-rose
fair-rose15mo ago
I think 4
eastern-cyan
eastern-cyan15mo ago
okay. this example uses 4 too
fair-rose
fair-rose15mo ago
I think the problem from server side I will take a look for both thanks is anyone know in mern stack and check if the issue from the backend ?
const express = require('express')
const dotenv = require('dotenv');
const cors = require('cors');

const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);

const connectDB = require('./mongodb/connect.js');

const customerRouter = require('./routes/customers.route.js');
const eventsRouter = require('./routes/events.route.js');
const ordersRouter = require('./routes/orders.route.js');
const productRouter = require('./routes/product.route.js');
const userRouter = require('./routes/users.route.js');

const MAX_AGE = 1000 * 60 * 60 * 2;

dotenv.config();
const app = express();
app.use(session({
secret: 'secret-key',
name: 'session_id',
store: new MongoDBStore({
uri: process.env.MONGODB_URL,
collection: 'sessions',
}),
cookie: {
maxAge: MAX_AGE,
sameSite: 'none',
secure: true,
},
resave: false,
saveUninitialized: true,

}));
app.set('trust proxy', 1)
app.use(cors());
app.use(express.json({ limit: '50mb' }));

app.get('/', (req, res) => {
res.send({ message: 'hello world' });
})

app.use('/Users', userRouter)
app.use('/Products', productRouter);
app.use('/Customers', customerRouter);
app.use('/Orders_History', ordersRouter);
app.use('/Events', eventsRouter);

const startServer = async () => {
try {
connectDB(process.env.MONGODB_URL);
app.listen(8080, () => console.log("server running on port: 8080"));
} catch (error) {}
}

startServer();
const express = require('express')
const dotenv = require('dotenv');
const cors = require('cors');

const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);

const connectDB = require('./mongodb/connect.js');

const customerRouter = require('./routes/customers.route.js');
const eventsRouter = require('./routes/events.route.js');
const ordersRouter = require('./routes/orders.route.js');
const productRouter = require('./routes/product.route.js');
const userRouter = require('./routes/users.route.js');

const MAX_AGE = 1000 * 60 * 60 * 2;

dotenv.config();
const app = express();
app.use(session({
secret: 'secret-key',
name: 'session_id',
store: new MongoDBStore({
uri: process.env.MONGODB_URL,
collection: 'sessions',
}),
cookie: {
maxAge: MAX_AGE,
sameSite: 'none',
secure: true,
},
resave: false,
saveUninitialized: true,

}));
app.set('trust proxy', 1)
app.use(cors());
app.use(express.json({ limit: '50mb' }));

app.get('/', (req, res) => {
res.send({ message: 'hello world' });
})

app.use('/Users', userRouter)
app.use('/Products', productRouter);
app.use('/Customers', customerRouter);
app.use('/Orders_History', ordersRouter);
app.use('/Events', eventsRouter);

const startServer = async () => {
try {
connectDB(process.env.MONGODB_URL);
app.listen(8080, () => console.log("server running on port: 8080"));
} catch (error) {}
}

startServer();