-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support User: Add Support User libraries #2368
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* This is a temporary file to assist development of the support user feature. | ||
*/ | ||
|
||
import config from 'config'; | ||
|
||
export default function( user ) { | ||
if ( config.isEnabled( 'support-user' ) ) { | ||
const callback = ( error ) => { | ||
if ( error ) { | ||
console.error( error ); | ||
} else { | ||
console.log( 'success' ); | ||
} | ||
}; | ||
|
||
window.supportUser = { | ||
login: ( username, password ) => user.changeUser( username, password, callback ), | ||
logout: () => user.restoreUser() | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,6 @@ | |
"logout_url", | ||
"siftscience_key", | ||
"facebook_api_key", | ||
"discover_blog_id" | ||
"discover_blog_id", | ||
"support-user" | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import qs from 'qs'; | ||
|
||
export default function wpcomSupport( wpcom ) { | ||
let supportUser = ''; | ||
let supportToken = ''; | ||
|
||
/** | ||
* Add the supportUser and supportToken to the query. | ||
* @param {Object} params The original request params object | ||
* @return {Object} The new query object with support data injected | ||
*/ | ||
const addSupportData = function( params ) { | ||
// Unwind the query string | ||
let query = qs.parse( params.query ); | ||
|
||
// Inject the credentials | ||
query.support_user = supportUser; | ||
query._support_token = supportToken | ||
|
||
return Object.assign( {}, params, { | ||
query: qs.stringify( query ) | ||
} ); | ||
}; | ||
|
||
const request = wpcom.request.bind( wpcom ); | ||
|
||
return Object.assign( wpcom, { | ||
changeUser: function( username, password, fn ) { | ||
return wpcom.req.post( | ||
{ | ||
apiVersion: '1.1', | ||
path: `/internal/support/${ username }/grant` | ||
}, | ||
{ | ||
password: password | ||
}, | ||
( error, response ) => { | ||
if ( ! error ) { | ||
supportUser = response.username; | ||
supportToken = response.token; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the future you may want to consider refactoring this so it doesn't need to depend on the closure, for easier testing, and for persistence through page reloads. I think this is fine for a first pass though. |
||
} | ||
|
||
fn( error, response ); | ||
} | ||
); | ||
}, | ||
restoreUser: function() { | ||
supportUser = ''; | ||
supportToken = ''; | ||
}, | ||
request: ( params, callback ) => { | ||
if ( supportUser && supportToken ) { | ||
return request( addSupportData( params ), callback ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Great improvement here. This is much more readable. |
||
} | ||
|
||
return request( params, callback ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call on moving one level up to wrap |
||
} ); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
qs.parse
instead of a hacked-in version is smart; if the params change, we won't have to rewrite parts ofaddSupportData
.