Merge "Whitelist the <wbr> element."
[mediawiki.git] / resources / mediawiki.api / mediawiki.api.login.js
blobccbae06c791b22a6a96d3dabbb6f79545415d1cf
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, request,
18                                 deferred = $.Deferred(),
19                                 api = this;
21                         params = {
22                                 action: 'login',
23                                 lgname: username,
24                                 lgpassword: password
25                         };
27                         request = api.post( params );
28                         request.fail( deferred.reject );
29                         request.done( function ( data ) {
30                                 params.lgtoken = data.login.token;
31                                 api.post( params )
32                                         .fail( deferred.reject )
33                                         .done( function ( data ) {
34                                                 var code;
35                                                 if ( data.login && data.login.result === 'Success' ) {
36                                                         deferred.resolve( data );
37                                                 } else {
38                                                         // Set proper error code whenever possible
39                                                         code = data.error && data.error.code || 'unknown';
40                                                         deferred.reject( code, data );
41                                                 }
42                                         } );
43                         } );
45                         return deferred.promise( { abort: request.abort } );
46                 }
47         } );
49         /**
50          * @class mw.Api
51          * @mixins mw.Api.plugin.login
52          */
54 }( mediaWiki, jQuery ) );