This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
auth-app.js
67 lines (55 loc) · 1.77 KB
/
auth-app.js
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
61
62
63
64
65
66
67
/* global authApp */
( function( $, authApp ) {
var $appNameField = $( '#app_name' ),
$approveBtn = $( '#approve' ),
$rejectBtn = $( '#reject' ),
$form = $appNameField.closest( 'form' );
$approveBtn.click( function( e ) {
var name = $appNameField.val();
e.preventDefault();
if ( 0 === name.length ) {
$appNameField.focus();
return;
}
$appNameField.prop( 'disabled', true );
$approveBtn.prop( 'disabled', true );
$.ajax( {
url: authApp.root + authApp.namespace + '/application-passwords/' + authApp.user_id + '/add',
method: 'POST',
beforeSend: function( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', authApp.nonce );
},
data: {
name: name
}
} ).done( function( response ) {
var raw = authApp.success,
url, $display;
if ( raw ) {
url = raw + ( -1 === raw.indexOf( '?' ) ? '?' : '&' ) +
'user_login=' + encodeURIComponent( authApp.user_login ) +
'&password=' + encodeURIComponent( response.password );
window.location = url;
} else {
// Should we maybe just reuse the js template modal from the profile page?
$form.replaceWith( '<p class="js-password-display">' +
authApp.strings.new_pass
.replace( '%1$s', '<strong></strong>' )
.replace( '%2$s', '<kbd></kbd>' ) +
'</p>' );
$display = $( '.js-password-display' );
// We're using .text() to write the variables to avoid any chance of XSS.
$display.find( 'strong' ).text( name );
$display.find( 'kbd' ).text( response.password );
}
} );
} );
$rejectBtn.click( function( e ) {
e.preventDefault();
// @todo: Make a better way to do this so it feels like less of a semi-open redirect.
window.location = authApp.reject;
} );
$form.on( 'submit', function( e ) {
e.preventDefault();
} );
}( jQuery, authApp ) );