From 5ef42cf93a228c68c44e8f670eb6a64df281d89c Mon Sep 17 00:00:00 2001
From: Jeffrey Morgan <251292+jmorganca@users.noreply.github.com>
Date: Thu, 9 Jun 2022 18:17:23 -0400
Subject: [PATCH] fix: allow non email logins and user mgmt (#2256)
---
ui/components/admin.js | 52 +++++++++++++-------------------
ui/components/grant.js | 52 ++++++++++++++------------------
ui/pages/login/index.js | 67 +++++++++++++++++++----------------------
ui/pages/users/index.js | 2 +-
4 files changed, 76 insertions(+), 97 deletions(-)
diff --git a/ui/components/admin.js b/ui/components/admin.js
index 7b3622c21f..92471c854b 100644
--- a/ui/components/admin.js
+++ b/ui/components/admin.js
@@ -2,8 +2,6 @@ import { useState } from 'react'
import useSWR, { useSWRConfig } from 'swr'
import { PlusIcon } from '@heroicons/react/outline'
-import { validateEmail } from '../lib/email'
-
import InputDropdown from './input'
import DeleteModal from './modals/delete'
import ErrorMessage from './error-message'
@@ -60,7 +58,7 @@ export default function () {
const { data: auth } = useSWR('/api/users/self')
const { mutate } = useSWRConfig()
- const [adminEmail, setAdminEmail] = useState('')
+ const [name, setName] = useState('')
const [error, setError] = useState('')
const userGrants = grants?.filter(g => g.user)
@@ -73,43 +71,35 @@ export default function () {
})
.then(() => {
mutate('/api/grants?resource=infra&privilege=admin')
- setAdminEmail('')
+ setName('')
}).catch((e) => setError(e.message || 'something went wrong, please try again later.'))
}
const handleInputChange = (value) => {
- setAdminEmail(value)
+ setName(value)
setError('')
}
const handleKeyDownEvent = (key) => {
- if (key === 'Enter' && adminEmail.length > 0) {
+ if (key === 'Enter' && name.length > 0) {
handleAddAdmin()
}
}
const handleAddAdmin = () => {
- if (validateEmail(adminEmail)) {
- setError('')
-
- fetch(`/api/users?name=${adminEmail}`)
- .then((response) => response.json())
- .then(({ items = [] }) => {
- if (items.length === 0) {
- fetch('/api/users', {
- method: 'POST',
- body: JSON.stringify({ name: adminEmail })
- })
- .then((response) => response.json())
- .then((user) => grantAdminAccess(user.id))
- .catch((error) => console.error(error))
- } else {
- grantAdminAccess(items[0].id)
- }
- })
- } else {
- setError('Invalid email')
- }
+ setError('')
+
+ fetch(`/api/users?name=${name}`)
+ .then((response) => response.json())
+ .then(({ items = [] }) => {
+ if (items.length === 0) {
+ setError('User does not exist')
+ } else {
+ grantAdminAccess(items[0].id)
+ }
+ }).catch(e => {
+ setError(e)
+ })
}
return (
@@ -118,9 +108,9 @@ export default function () {
handleInputChange(e.target.value)}
handleKeyDown={(e) => handleKeyDownEvent(e.key)}
@@ -129,7 +119,7 @@ export default function () {