Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / resources / src / mediawiki / api / login.js
blob2b709aae7bca023f27239acf3d01167c119142c3
1 /**
2  * Make the two-step login easier.
3  *
4  * @author Niklas Laxström
5  * @class mw.Api.plugin.login
6  * @since 1.22
7  */
8 ( function ( mw, $ ) {
9         'use strict';
11         $.extend( mw.Api.prototype, {
12                 /**
13                  * @param {string} username
14                  * @param {string} password
15                  * @return {jQuery.Promise} See mw.Api#post
16                  */
17                 login: function ( username, password ) {
18                         var params, apiPromise, innerPromise,
19                                 api = this;
21                         params = {
22                                 action: 'login',
23                                 lgname: username,
24                                 lgpassword: password
25                         };
27                         apiPromise = api.post( params );
29                         return apiPromise
30                                 .then( function ( data ) {
31                                         params.lgtoken = data.login.token;
32                                         innerPromise = api.post( params )
33                                                 .then( function ( data ) {
34                                                         var code;
35                                                         if ( data.login.result !== 'Success' ) {
36                                                                 // Set proper error code whenever possible
37                                                                 code = data.error && data.error.code || 'unknown';
38                                                                 return $.Deferred().reject( code, data );
39                                                         }
40                                                         return data;
41                                                 } );
42                                         return innerPromise;
43                                 } )
44                                 .promise( {
45                                         abort: function () {
46                                                 apiPromise.abort();
47                                                 if ( innerPromise ) {
48                                                         innerPromise.abort();
49                                                 }
50                                         }
51                                 } );
52                 }
53         } );
55         /**
56          * @class mw.Api
57          * @mixins mw.Api.plugin.login
58          */
60 }( mediaWiki, jQuery ) );