Skip to content

Commit 1e183fc

Browse files
committed
Refactor: break down login function.
Signed-off-by: Eric Wang <[email protected]>
1 parent babd4b8 commit 1e183fc

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

lib/plugins/leetcode.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ plugin.getFavorites = function(cb) {
364364
});
365365
};
366366

367-
plugin.login = function(user, cb) {
368-
log.debug('running leetcode.login');
367+
plugin.signin = function(user, cb) {
368+
log.debug('running leetcode.signin');
369369
request(config.sys.urls.login, function(e, resp, body) {
370370
e = checkError(e, resp, 200);
371371
if (e) return cb(e);
@@ -393,19 +393,27 @@ plugin.login = function(user, cb) {
393393
user.sessionId = h.getSetCookieValue(resp, 'LEETCODE_SESSION');
394394
user.name = h.getSetCookieValue(resp, 'messages')
395395
.match('Successfully signed in as ([^.]*)')[1];
396-
session.saveUser(user);
396+
return cb(null, user);
397+
});
398+
});
399+
};
397400

398-
plugin.getFavorites(function(e, favorites) {
399-
if (e) return cb(e);
401+
plugin.login = function(user, cb) {
402+
log.debug('running leetcode.login');
403+
plugin.signin(user, function(e, user) {
404+
if (e) return cb(e);
405+
session.saveUser(user);
400406

401-
// TODO: pick other useful values from favorites
402-
var favorite = _.find(favorites.favorites.private_favorites, function(f) {
403-
return f.name === 'Favorite';
404-
});
405-
user.hash = favorite.id_hash;
407+
plugin.getFavorites(function(e, favorites) {
408+
if (e) return cb(e);
406409

407-
return cb(null, user);
410+
// TODO: pick other useful values from favorites
411+
var favorite = _.find(favorites.favorites.private_favorites, function(f) {
412+
return f.name === 'Favorite';
408413
});
414+
user.hash = favorite.id_hash;
415+
416+
return cb(null, user);
409417
});
410418
});
411419
};

0 commit comments

Comments
 (0)