Browse Source

Fix login will expire

pull/6/head
Menci 8 years ago
parent
commit
9d487101c3
  1. 37
      app.js
  2. 7
      modules/api.js
  3. 1
      modules/user.js

37
app.js

@ -121,19 +121,48 @@ global.syzoj = {
app.use((req, res, next) => { app.use((req, res, next) => {
// req.session.user_id = 1; // req.session.user_id = 1;
let User = syzoj.model('user');
if (req.session.user_id) { if (req.session.user_id) {
let User = syzoj.model('user');
User.fromID(req.session.user_id).then((user) => { User.fromID(req.session.user_id).then((user) => {
res.locals.user = user; res.locals.user = user;
next(); next();
}).catch((err) => { }).catch((err) => {
this.log(err); this.log(err);
res.locals.user = req.session.user_id = null; res.locals.user = null;
req.session.user_id = null;
next(); next();
}) })
} else { } else {
res.locals.user = req.session.user_id = null; if (req.cookies.login) {
next(); let obj;
try {
obj = JSON.parse(req.cookies.login);
User.findOne({
where: {
username: obj[0],
password: obj[1]
}
}).then(user => {
if (!user) throw null;
res.locals.user = user;
req.session.user_id = user.id;
next();
}).catch(err => {
console.log(err);
res.locals.user = null;
req.session.user_id = null;
next();
});
} catch (e) {
res.locals.user = null;
req.session.user_id = null;
next();
}
} else {
res.locals.user = null;
req.session.user_id = null;
next();
}
} }
}); });

7
modules/api.js

@ -25,6 +25,10 @@ let WaitingJudge = syzoj.model('waiting_judge');
let JudgeState = syzoj.model('judge_state'); let JudgeState = syzoj.model('judge_state');
let TestData = syzoj.model('testdata'); let TestData = syzoj.model('testdata');
function setLoginCookie(username, password, res) {
res.cookie('login', JSON.stringify([username, password]));
}
// Login // Login
app.post('/api/login', async (req, res) => { app.post('/api/login', async (req, res) => {
try { try {
@ -35,6 +39,7 @@ app.post('/api/login', async (req, res) => {
else if (user.password !== req.body.password) res.send({ error_code: 1002 }); else if (user.password !== req.body.password) res.send({ error_code: 1002 });
else { else {
req.session.user_id = user.id; req.session.user_id = user.id;
setLoginCookie(user.username, user.password, res);
res.send({ error_code: 1 }); res.send({ error_code: 1 });
} }
} catch (e) { } catch (e) {
@ -65,6 +70,8 @@ app.post('/api/sign_up', async (req, res) => {
await user.save(); await user.save();
req.session.user_id = user.id; req.session.user_id = user.id;
setLoginCookie(user.username, user.password, res);
res.send(JSON.stringify({ error_code: 1 })); res.send(JSON.stringify({ error_code: 1 }));
} catch (e) { } catch (e) {
syzoj.log(e); syzoj.log(e);

1
modules/user.js

@ -78,6 +78,7 @@ app.get('/sign_up', async (req, res) => {
// Logout // Logout
app.get('/logout', async (req, res) => { app.get('/logout', async (req, res) => {
req.session.user_id = null; req.session.user_id = null;
res.clearCookie('login');
res.redirect(req.query.url || '/'); res.redirect(req.query.url || '/');
}); });

Loading…
Cancel
Save