Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
bb190245
Commit
bb190245
authored
Dec 26, 2023
by
Timothy J. Baek
Browse files
chore: admin page refac
parent
1303407f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
47 deletions
+70
-47
src/lib/apis/users/index.ts
src/lib/apis/users/index.ts
+58
-0
src/routes/admin/+page.svelte
src/routes/admin/+page.svelte
+12
-47
No files found.
src/lib/apis/users/index.ts
0 → 100644
View file @
bb190245
import
{
WEBUI_API_BASE_URL
}
from
'
$lib/constants
'
;
export
const
updateUserRole
=
async
(
token
:
string
,
id
:
string
,
role
:
string
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/users/update/role`
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
Authorization
:
`Bearer
${
token
}
`
},
body
:
JSON
.
stringify
({
id
:
id
,
role
:
role
})
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
catch
((
error
)
=>
{
console
.
log
(
error
);
error
=
error
.
detail
;
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
;
};
export
const
getUsers
=
async
(
token
:
string
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/users`
,
{
method
:
'
GET
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
Authorization
:
`Bearer
${
token
}
`
}
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
catch
((
error
)
=>
{
console
.
log
(
error
);
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
?
res
:
[];
};
src/routes/admin/+page.svelte
View file @
bb190245
...
...
@@ -6,62 +6,27 @@
import toast from 'svelte-french-toast';
import { updateUserRole, getUsers } from '$lib/apis/users';
let loaded = false;
let users = [];
const updateUserRole = async (id, role) => {
const res = await fetch(`${WEBUI_API_BASE_URL}/users/update/role`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.token}`
},
body: JSON.stringify({
id: id,
role: role
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((error) => {
console.log(error);
toast.error(error.detail);
return null;
});
const updateRoleHandler = async (id, role) => {
const res = await updateUserRole(localStorage.token, id, role).catch((error) => {
toast.error(error);
return null;
});
if (res) {
await getUsers();
users =
await getUsers(
localStorage.token
);
}
};
const getUsers = async () => {
const res = await fetch(`${WEBUI_API_BASE_URL}/users`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((error) => {
console.log(error);
toast.error(error.detail);
return null;
});
users = res ? res : [];
};
onMount(async () => {
if ($user?.role !== 'admin') {
await goto('/');
} else {
await getUsers();
users =
await getUsers(
localStorage.token
);
}
loaded = true;
});
...
...
@@ -115,11 +80,11 @@
class=" dark:text-white underline"
on:click={() => {
if (user.role === 'user') {
update
User
Role(user.id, 'admin');
updateRole
Handler
(user.id, 'admin');
} else if (user.role === 'pending') {
update
User
Role(user.id, 'user');
updateRole
Handler
(user.id, 'user');
} else {
update
User
Role(user.id, 'pending');
updateRole
Handler
(user.id, 'pending');
}
}}>{user.role}</button
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment