Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add leetcode user -c that user can login with cookie #31

Merged
merged 3 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

<img src="https://github.com/skygragon/leetcode-cli/raw/master/docs/logo.png" align="right">

Expand Down Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Align the indention?

Browse all questions $ leetcode list
Choose one question $ leetcode show 1 -g -l cpp
Coding it!
Expand Down
24 changes: 23 additions & 1 deletion lib/commands/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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');
}
};
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions lib/plugins/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;