From a0f10252a1c3074d41e5659ced1c96b82b771700 Mon Sep 17 00:00:00 2001 From: Godzil Date: Wed, 1 Aug 2018 20:37:50 +0100 Subject: [PATCH] Add back the old login method as fallback. --- src/my_request.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/my_request.ts b/src/my_request.ts index 9c33bf5..aa158af 100644 --- a/src/my_request.ts +++ b/src/my_request.ts @@ -311,8 +311,67 @@ function authenticate(config: IConfig, done: (err: Error) => void) } else { - log.error('This method of login is currently unsupported...\n'); - return done(AuthError('Unsupported login method')); + /* First get https://www.crunchyroll.com/login to get the login token */ + const options = + { + headers: defaultHeaders, + jar: j, + gzip: false, + method: 'GET', + url: 'https://www.crunchyroll.com/login' + }; + + cloudscraper.request(options, (err: Error, rep: string, body: string) => + { + if (err) return done(err); + + const $ = cheerio.load(body); + + /* Get the token from the login page */ + const token = $('input[name="login_form[_token]"]').attr('value'); + if (token === '') + { + return done(AuthError('Can\'t find token!')); + } + + /* Now call the page again with the token and credentials */ + const options = + { + headers: defaultHeaders, + form: + { + 'login_form[name]': config.user, + 'login_form[password]': config.pass, + 'login_form[redirect_url]': '/', + 'login_form[_token]': token + }, + jar: j, + gzip: false, + method: 'POST', + url: 'https://www.crunchyroll.com/login' + }; + + cloudscraper.request(options, (err: Error, rep: string, body: string) => + { + if (err) + { + return done(err); + } + + /* Now let's check if we are authentificated */ + checkIfUserIsAuth(config, (errCheckAuth2) => + { + if (isAuthenticated) + { + return done(null); + } + else + { + return done(errCheckAuth2); + } + }); + }); + }); } });