Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fe for api-key fixed commit

kruzhambusa9c95cd716cc
diff --git a/src/api/index.js b/src/api/index.js
index 94f6dfdf4..57de8c1c3 100644
--- a/src/api/index.js
+++ b/src/api/index.js
@@ -380,3 +380,25 @@ export const submit2FA = (twoFactorAuthenticationCode) =>
? error.response.data
: error.response.data.message
})
+
+export const generateApiKey = () =>
+ api
+ .post('user/api-key')
+ .then((response) => response.data)
+ .catch((error) => {
+ debug('%s', error)
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message
+ })
+
+export const deleteApiKey = () =>
+ api
+ .delete('user/api-key')
+ .then((response) => response.data)
+ .catch((error) => {
+ debug('%s', error)
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message
+ })
diff --git a/src/pages/UserSettings/UserSettings.container.jsx b/src/pages/UserSettings/UserSettings.container.jsx
index 8cdfe1790..1f8adeaf1 100644
--- a/src/pages/UserSettings/UserSettings.container.jsx
+++ b/src/pages/UserSettings/UserSettings.container.jsx
@@ -17,7 +17,9 @@ import { alertsActions } from 'redux/actions/alerts'
import UIActions from 'redux/actions/ui'
import { getCookie, setCookie } from 'utils/cookie'
import { trackCustom } from 'utils/analytics'
-import { confirmEmail, exportUserData } from 'api'
+import {
+ confirmEmail, exportUserData, generateApiKey, deleteApiKey,
+} from 'api'
import routes from 'routes'
import UserSettings from './UserSettings'
@@ -138,6 +140,26 @@ const UserSettingsContainer = () => {
dispatch(authActions.setUserShareData(data, id))
}
+ const onApiKeyGenerate = async () => {
+ try {
+ const res = await generateApiKey()
+ console.log(res)
+ dispatch(authActions.setApiKey(res.apiKey))
+ } catch (e) {
+ dispatch(errorsActions.updateProfileFailed(e))
+ }
+ }
+
+ const onApiKeyDelete = async () => {
+ try {
+ const res = await deleteApiKey()
+ console.log(res)
+ dispatch(authActions.setApiKey(null))
+ } catch (e) {
+ dispatch(errorsActions.updateProfileFailed(e))
+ }
+ }
+
return (
<UserSettings
t={t}
@@ -156,6 +178,8 @@ const UserSettingsContainer = () => {
updateUserData={updateUserData}
login={login}
genericError={genericError}
+ onApiKeyDelete={onApiKeyDelete}
+ onApiKeyGenerate={onApiKeyGenerate}
/>
)
}
diff --git a/src/pages/UserSettings/UserSettings.jsx b/src/pages/UserSettings/UserSettings.jsx
index 0598ad1d0..a2ea6fc4e 100644
--- a/src/pages/UserSettings/UserSettings.jsx
+++ b/src/pages/UserSettings/UserSettings.jsx
@@ -356,7 +356,7 @@ const TwoFA = ({
const UserSettings = ({
onDelete, onExport, onSubmit, onEmailConfirm, onDeleteProjectCache, t,
removeProject, removeShareProject, setUserShareData, setProjectsShareData, language,
- userSharedUpdate, sharedProjectError, updateUserData, login, genericError,
+ userSharedUpdate, sharedProjectError, updateUserData, login, genericError, onApiKeyGenerate, onApiKeyDelete,
}) => {
const { user, dontRemember } = useSelector(state => state.auth)
@@ -536,6 +536,36 @@ const UserSettings = ({
{t('common.save')}
</Button>
<hr className='mt-5' />
+ <h3 className='flex items-center mt-2 text-lg font-bold text-gray-900 dark:text-gray-50'>
+ Api-key
+ <div className='ml-5'>
+ <Beta />
+ </div>
+ </h3>
+ <div className='grid grid-cols-1 gap-y-6 gap-x-4 lg:grid-cols-2 mt-4'>
+ <div>
+ <Input
+ name='apiKey'
+ id='apiKey'
+ type='text'
+ label='Generated Api-key'
+ value={user.apiKey || ''}
+ className='mt-4'
+ onChange={handleInput}
+ disabled
+ />
+ </div>
+ </div>
+ {user.apiKey ? (
+ <Button className='mt-4' onClick={onApiKeyDelete} danger large>
+ Delete api-key
+ </Button>
+ ) : (
+ <Button className='mt-4' onClick={onApiKeyGenerate} primary large>
+ Generate api-key
+ </Button>
+ )}
+ <hr className='mt-5' />
<h3 className='flex items-center mt-2 text-lg font-bold text-gray-900 dark:text-gray-50'>
{t('profileSettings.2fa')}
<div className='ml-5'>
@@ -675,6 +705,8 @@ UserSettings.propTypes = {
updateUserData: PropTypes.func.isRequired,
login: PropTypes.func.isRequired,
genericError: PropTypes.func.isRequired,
+ onApiKeyDelete: PropTypes.func.isRequired,
+ onApiKeyGenerate: PropTypes.func.isRequired,
}
export default memo(UserSettings)
diff --git a/src/redux/actions/auth/index.js b/src/redux/actions/auth/index.js
index c3b381c5f..53d830913 100644
--- a/src/redux/actions/auth/index.js
+++ b/src/redux/actions/auth/index.js
@@ -138,4 +138,11 @@ export const authActions = {
}
},
+ setApiKey(apiKey) {
+ return {
+ type: types.SET_API_KEY,
+ payload: { apiKey },
+ }
+ },
+
}
diff --git a/src/redux/actions/auth/types.js b/src/redux/actions/auth/types.js
index 12daf9529..faa994d7d 100644
--- a/src/redux/actions/auth/types.js
+++ b/src/redux/actions/auth/types.js
@@ -13,6 +13,7 @@ export const types = {
SET_USER_SHARE_DATA: 'SET_USER_SHARE_DATA',
SET_DONT_REMEMBER: 'SET_DONT_REMEMBER',
UPDATE_USER_DATA: 'UPDATE_USER_DATA',
+ SET_API_KEY: 'SET_API_KEY',
// Asynchronous
LOGIN_ASYNC: 'LOGIN_ASYNC',
diff --git a/src/redux/reducers/auth.js b/src/redux/reducers/auth.js
index 3c6e565ce..c3a4bbaee 100644
--- a/src/redux/reducers/auth.js
+++ b/src/redux/reducers/auth.js
@@ -26,6 +26,9 @@ const authReducer = (state = initialState, { type, payload }) => {
case types.SAVE_PATH:
return { ...state, redirectPath: payload.path }
+ case types.SET_API_KEY:
+ return { ...state, user: { ...state.user, apiKey: payload.apiKey } }
+
case types.LOGOUT:
case types.DELETE_ACCOUNT_SUCCESS:
return { ...state, authenticated: false, user: {} }