diff --git a/README.md b/README.md index 13eabbb7..56bf57d8 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ # leetcode-cli > Note: This repository is forked from [leetcode-cli](https://github.com/skygragon/leetcode-cli) for temporary usage. +> Note: Copy cookie from webbrowser and Using **leetcode user -c** can temporary fix can't [login problem](https://github.com/jdneo/vscode-leetcode/issues/478). @@ -39,6 +40,7 @@ Great thanks to leetcode.com, a really awesome website! Read help first $ leetcode help Login with your leetcode account $ leetcode user -l + Cookie login with cookie $ leetcode user -c Browse all questions $ leetcode list Choose one question $ leetcode show 1 -g -l cpp Coding it! diff --git a/lib/commands/user.js b/lib/commands/user.js index 4cd903cd..f5ae86b7 100644 --- a/lib/commands/user.js +++ b/lib/commands/user.js @@ -21,6 +21,12 @@ const cmd = { default: false, describe: 'Login' }) + .option('c', { + alias: 'cookie', + type: 'boolean', + default: false, + describe: 'cookieLogin' + }) .option('L', { alias: 'logout', type: 'boolean', @@ -29,6 +35,7 @@ const cmd = { }) .example(chalk.yellow('leetcode user'), 'Show current user') .example(chalk.yellow('leetcode user -l'), 'User login') + .example(chalk.yellow('leetcode user -c'), 'User Cookie login') .example(chalk.yellow('leetcode user -L'), 'User logout'); } }; @@ -59,7 +66,22 @@ cmd.handler = function(argv) { log.info('Successfully logout as', chalk.yellow(user.name)); else log.fail('You are not login yet?'); - } else { + } else if (argv.cookie) { + // session + prompt.colors = false; + prompt.message = ''; + prompt.start(); + prompt.get([ + {name: 'login', required: true}, + {name: 'cookie', required: true} + ], function(e, user) { + if (e) return log.fail(e) + core.cookieLogin(user, function(e, user) { + if (e) return log.fail(e); + log.info('Successfully cookie login as', chalk.yellow(user.name)); + }); + }); + } else { // show current user user = session.getUser(); if (user) { diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 880c9fba..924b40d7 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -538,4 +538,19 @@ plugin.login = function(user, cb) { }); }; +plugin.cookieLogin = function(user, cb) { + // re pattern for cookie chrome or firefox + const SessionPattern = /LEETCODE_SESSION=(.+?)(;|$)/; + const csrfPattern = /csrftoken=(.+?)(;|$)/; + const reSessionResult = SessionPattern.exec(user.cookie); + const reCsrfResult = csrfPattern.exec(user.cookie); + if (reSessionResult === null || reCsrfResult === null) { + return cb('invalid cookie?') + } + user.sessionId = reSessionResult[1]; + user.sessionCSRF = reCsrfResult[1]; + session.saveUser(user); + plugin.getUser(user, cb); +} + module.exports = plugin;