diff --git a/client/constants.js b/client/constants.js index ffe8ba61..08b3f81b 100644 --- a/client/constants.js +++ b/client/constants.js @@ -19,7 +19,7 @@ export const AUTH_ERROR = 'AUTH_ERROR'; export const SETTINGS_UPDATED = 'SETTINGS_UPDATED'; -export const ADDED_API_KEY = 'ADDED_API_KEY'; +export const API_KEY_CREATED = 'API_KEY_CREATED'; export const REMOVED_API_KEY = 'REMOVED_API_KEY'; export const SET_PROJECT_NAME = 'SET_PROJECT_NAME'; diff --git a/client/modules/User/actions.js b/client/modules/User/actions.js index 0ba02f0d..cfd2664a 100644 --- a/client/modules/User/actions.js +++ b/client/modules/User/actions.js @@ -222,46 +222,22 @@ export function updateSettings(formValues) { .catch(response => Promise.reject(new Error(response.data.error))); } -export function addApiKey(label) { - return ((dispatch) => { - crypto.randomBytes(20, (err, buf) => { - const key = buf.toString('hex'); - const encodedKey = Buffer.from(key).toString('base64'); - axios.put(`${ROOT_URL}/account/api-keys`, { label, encodedKey }, { withCredentials: true }) - .then((response) => { - // window.alert(`Here is your key :\n${key}\nNote it somewhere, you won't be able to see it later !`); - const elt = React.createElement( - 'tr', { className: 'new-key' }, - React.createElement('td', {}, 'Here is your new key ;\ncopy it somewhere, you won\'t be able to see it later !'), - React.createElement( - 'td', {}, - React.createElement('input', { - id: 'key-to-copy', type: 'text', value: key, readOnly: true - }) - ), - React.createElement( - 'td', {}, - React.createElement('input', { - type: 'submit', - value: 'Copy to clipboard', - className: 'form__table-button-copy', - onClick: () => { - const inputKey = document.getElementById('key-to-copy'); - inputKey.select(); - document.execCommand('copy'); - } - }) - ) - ); - ReactDom.render(elt, document.getElementById('form__table_new_key')); - dispatch({ - type: ActionTypes.ADDED_API_KEY, - user: response.data - }); - }) - .catch(response => Promise.reject(new Error(response.data.error))); - }); - }); +export function createApiKeySuccess(token) { + return { + type: ActionTypes.API_KEY_CREATED, + token + }; +} + +export function createApiKey(label) { + return dispatch => + axios.post(`${ROOT_URL}/account/api-keys`, { label }, { withCredentials: true }) + .then((response) => { + const { token } = response.data; + dispatch(createApiKeySuccess(token)); + return token; + }) + .catch(response => Promise.reject(new Error(response.data.error))); } export function removeApiKey(keyId) { diff --git a/client/modules/User/components/APIKeyForm.jsx b/client/modules/User/components/APIKeyForm.jsx index d99005ac..3d8dcef6 100644 --- a/client/modules/User/components/APIKeyForm.jsx +++ b/client/modules/User/components/APIKeyForm.jsx @@ -12,7 +12,8 @@ class APIKeyForm extends React.Component { addKey(event) { event.preventDefault(); document.getElementById('addKeyForm').reset(); - this.props.addApiKey(this.state.keyLabel); + this.props.createApiKey(this.state.keyLabel) + .then(newToken => this.setState({ newToken })); this.state.keyLabel = ''; return false; } @@ -22,23 +23,35 @@ class APIKeyForm extends React.Component { } render() { + const { newToken } = this.state; + + const content = newToken ? + ( +
Here is your new key. Copy it somewhere, you won't be able to see it later !
+ + +