| 1 | const AuthPopup = require( './AuthPopup.js' ); |
| 2 | const config = require( './config.json' ); |
| 3 | |
| 4 | function checkLoggedIn() { |
| 5 | return ( new mw.Api() ).get( { |
| 6 | meta: 'userinfo' |
| 7 | } ).then( ( resp ) => { |
| 8 | const userinfo = resp.query.userinfo; |
| 9 | if ( userinfo.anon !== undefined || userinfo.temp !== undefined ) { |
| 10 | return null; |
| 11 | } |
| 12 | return userinfo; |
| 13 | } ); |
| 14 | } |
| 15 | |
| 16 | const loginTitle = mw.Title.makeTitle( -1, config.specialPageNames.UserLogin ); |
| 17 | const successTitle = mw.Title.makeTitle( -1, config.specialPageNames.AuthenticationPopupSuccess ); |
| 18 | |
| 19 | const loginPopupUrl = loginTitle.getUrl( { |
| 20 | display: 'popup', |
| 21 | returnto: successTitle.getPrefixedText(), |
| 22 | returntoquery: 'display=popup' |
| 23 | } ); |
| 24 | const loginFallbackUrl = loginTitle.getUrl( { |
| 25 | returnto: successTitle.getPrefixedText() |
| 26 | } ); |
| 27 | |
| 28 | /** |
| 29 | * `userinfo` object as returned by the |
| 30 | * {@link https://www.mediawiki.org/wiki/API:Userinfo action=query&meta=userinfo API module}. |
| 31 | * |
| 32 | * @typedef {Object} module:mediawiki.authenticationPopup~userinfo |
| 33 | * @property {string} name |
| 34 | * @property {number} id |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * Exposes an instance of {@link AuthPopup} configured to display a login dialog for the local |
| 39 | * instance of MediaWiki. |
| 40 | * |
| 41 | * The promises returned by `AuthPopup` methods will be resolved with a {@link userinfo} object. |
| 42 | * |
| 43 | * **This library is not stable yet (as of May 2024). We're still testing which of the |
| 44 | * methods work from the technical side, and which methods are understandable for users. |
| 45 | * Some methods or the whole library may be removed in the future.** |
| 46 | * |
| 47 | * @example |
| 48 | * const authPopup = require( 'mediawiki.authenticationPopup' ); |
| 49 | * authPopup.startPopupWindow() |
| 50 | * // or: authPopup.startNewTabOrWindow() |
| 51 | * // or: authPopup.startIframe() |
| 52 | * .then( function ( userinfo ) { |
| 53 | * if ( userinfo ) { |
| 54 | * // Logged in |
| 55 | * console.log( userinfo.name ); |
| 56 | * } else { |
| 57 | * // Cancelled by the user |
| 58 | * } |
| 59 | * }, function ( error ) { |
| 60 | * // Unexpected error stopped the login process |
| 61 | * } ); |
| 62 | * |
| 63 | * @example <caption>Example using `await` syntax</caption> |
| 64 | * const userinfo = await authPopup.startPopupWindow(); // etc. |
| 65 | * if ( userinfo ) { |
| 66 | * // Logged in |
| 67 | * } else { |
| 68 | * // Cancelled by the user |
| 69 | * } |
| 70 | * |
| 71 | * @module mediawiki.authenticationPopup |
| 72 | * @type {AuthPopup} |
| 73 | */ |
| 74 | module.exports = new AuthPopup( { |
| 75 | loginPopupUrl: loginPopupUrl, |
| 76 | loginFallbackUrl: loginFallbackUrl, |
| 77 | checkLoggedIn: checkLoggedIn, |
| 78 | message: () => $( '<div>' ).append( |
| 79 | $( '<p>' ).append( |
| 80 | mw.message( |
| 81 | 'userlogin-authpopup-loggingin-body', |
| 82 | $( '<a>' ).attr( 'href', loginFallbackUrl ).attr( 'target', '_blank' ) |
| 83 | ).parseDom() |
| 84 | ), |
| 85 | $.createSpinner( { |
| 86 | size: 'large', |
| 87 | type: 'block' |
| 88 | } ) |
| 89 | ) |
| 90 | } ); |