diff --git a/lib/plugins/leetcode.cn.js b/lib/plugins/leetcode.cn.js index 07c78f98..60e620eb 100644 --- a/lib/plugins/leetcode.cn.js +++ b/lib/plugins/leetcode.cn.js @@ -1,4 +1,4 @@ -'use strict' +'use strict'; var request = require('request'); var config = require('../config'); diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 12d7ccbe..cf2231db 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -3,6 +3,7 @@ var util = require('util'); var _ = require('underscore'); var request = require('request'); +var prompt = require('prompt'); var config = require('../config'); var h = require('../helper'); @@ -612,30 +613,39 @@ plugin.githubLogin = function(user, cb) { if (resp.request.uri.href !== urls.github_tf_redirect) { return requestLeetcodeAndSave(_request, leetcodeUrl, user, cb); } - // read two-factor code must be sync. - const twoFactorcode = require('prompt-sync')()('Please enter your two-factor code: '); - const authenticityTokenTwoFactor = body.match(/name="authenticity_token" value="(.*?)"/); - if (authenticityTokenTwoFactor === null) { - return cb('Get GitHub two-factor token failed'); - } - const optionsTwoFactor = { - url: urls.github_tf_session_request, - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, - followAllRedirects: true, - form: { - 'otp': twoFactorcode, - 'authenticity_token': authenticityTokenTwoFactor[1], - 'utf8': encodeURIComponent('✓'), - }, - }; - _request(optionsTwoFactor, function(e, resp, body) { - if (resp.request.uri.href === urls.github_tf_session_request) { - return cb('Invalid two-factor code please check'); + prompt.colors = false; + prompt.message = ''; + prompt.start(); + prompt.get([ + { + name: 'twoFactorCode', + required: true + } + ], function(e, result) { + if (e) return log.fail(e); + const authenticityTokenTwoFactor = body.match(/name="authenticity_token" value="(.*?)"/); + if (authenticityTokenTwoFactor === null) { + return cb('Get GitHub two-factor token failed'); } - requestLeetcodeAndSave(_request, leetcodeUrl, user, cb); + const optionsTwoFactor = { + url: urls.github_tf_session_request, + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + followAllRedirects: true, + form: { + 'otp': result.twoFactorCode, + 'authenticity_token': authenticityTokenTwoFactor[1], + 'utf8': encodeURIComponent('✓'), + }, + }; + _request(optionsTwoFactor, function(e, resp, body) { + if (resp.request.uri.href === urls.github_tf_session_request) { + return cb('Invalid two-factor code please check'); + } + requestLeetcodeAndSave(_request, leetcodeUrl, user, cb); + }); }); }); }); diff --git a/package.json b/package.json index 6d8ca383..6323fd8e 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,6 @@ "nock": "10.0.2", "nyc": "^13.3.0", "pkg": "^4.3.4", - "prompt-sync": "^4.2.0", "rewire": "4.0.1" } }