Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 85e2979

Browse files
committed
Clear user cache if seesion expired
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent f7a4bb7 commit 85e2979

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/core.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ core.getProblems = function(cb) {
1010
if (cached) return cb(null, cached);
1111

1212
client.getProblems(function(e, problems) {
13+
if (e) return cb(e);
14+
1315
cache.set('all', problems);
14-
return cb(e, problems);
16+
return cb(null, problems);
1517
});
1618
};
1719

@@ -61,19 +63,24 @@ core.updateProblem = function(problem, kv) {
6163
};
6264

6365
core.login = function(user, cb) {
66+
var self = this;
6467
client.login(user, function(e, user) {
6568
if (e) return cb(e);
6669

67-
// NOTE: need invalidate any user related cache
68-
cache.del('all');
70+
self.logout();
71+
6972
cache.set('.user', user);
7073
return cb(null, user);
7174
});
7275
};
7376

7477
core.logout = function(user) {
7578
user = this.getUser();
76-
if (user) cache.del('.user');
79+
if (user) {
80+
// NOTE: need invalidate any user related cache
81+
cache.del('.user');
82+
cache.del('all');
83+
}
7784
return user;
7885
};
7986

lib/leetcode_client.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ function checkError(e, resp, expectedStatus, msg) {
2020
if (e) return e;
2121

2222
if (resp && resp.statusCode !== expectedStatus) {
23-
if (resp.statusCode === 403)
23+
if (resp.statusCode === 403) {
2424
msg = msg || 'session expired, please login again';
2525

26+
var core = require('./core');
27+
core.logout();
28+
}
29+
2630
return {
2731
msg: msg || 'http error',
2832
statusCode: resp.statusCode
@@ -37,7 +41,14 @@ leetcodeClient.getProblems = function(cb) {
3741
e = checkError(e, resp, 200);
3842
if (e) return cb(e);
3943

40-
var problems = JSON.parse(body).stat_status_pairs.map(function(p) {
44+
var json = JSON.parse(body);
45+
46+
// leetcode permits anonymous access to the problem list
47+
// while we require login first to make a better experience.
48+
if (json.user_name.length === 0)
49+
return cb('session expired, please login again');
50+
51+
var problems = json.stat_status_pairs.map(function(p) {
4152
return {
4253
state: p.status || 'None',
4354
id: p.stat.question_id,

0 commit comments

Comments
 (0)