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