mediawiki.api.login: Include second HTTP request in abort()
[mediawiki.git] / resources / src / mediawiki.api / mediawiki.api.login.js
blob25257927b4019a84501c7d9746b7e7f13d40fb02
1 /**
2 * Make the two-step login easier.
3 * @author Niklas Laxström
4 * @class mw.Api.plugin.login
5 * @since 1.22
6 */
7 ( function ( mw, $ ) {
8 'use strict';
10 $.extend( mw.Api.prototype, {
11 /**
12 * @param {string} username
13 * @param {string} password
14 * @return {jQuery.Promise} See mw.Api#post
16 login: function ( username, password ) {
17 var params, apiPromise, innerPromise,
18 api = this;
20 params = {
21 action: 'login',
22 lgname: username,
23 lgpassword: password
26 apiPromise = api.post( params );
28 return apiPromise
29 .then( function ( data ) {
30 params.lgtoken = data.login.token;
31 innerPromise = api.post( params )
32 .then( function ( data ) {
33 var code;
34 if ( data.login.result !== 'Success' ) {
35 // Set proper error code whenever possible
36 code = data.error && data.error.code || 'unknown';
37 return $.Deferred().reject( code, data );
39 return data;
40 } );
41 return innerPromise;
42 } )
43 .promise( {
44 abort: function () {
45 apiPromise.abort();
46 if ( innerPromise ) {
47 innerPromise.abort();
50 } );
52 } );
54 /**
55 * @class mw.Api
56 * @mixins mw.Api.plugin.login
59 }( mediaWiki, jQuery ) );