Merge "DatabaseMssql: Don't duplicate body of makeList()"
[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
15                  */
16                 login: function ( username, password ) {
17                         var params, apiPromise, innerPromise,
18                                 api = this;
20                         params = {
21                                 action: 'login',
22                                 lgname: username,
23                                 lgpassword: password
24                         };
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 );
38                                                         }
39                                                         return data;
40                                                 } );
41                                         return innerPromise;
42                                 } )
43                                 .promise( {
44                                         abort: function () {
45                                                 apiPromise.abort();
46                                                 if ( innerPromise ) {
47                                                         innerPromise.abort();
48                                                 }
49                                         }
50                                 } );
51                 }
52         } );
54         /**
55          * @class mw.Api
56          * @mixins mw.Api.plugin.login
57          */
59 }( mediaWiki, jQuery ) );