create users through admin page

This commit is contained in:
2025-07-19 22:18:49 +02:00
parent deff81d2ff
commit 7f2aac12e6
65 changed files with 1394 additions and 139 deletions

View File

@@ -0,0 +1,43 @@
import { toast } from 'vue-sonner';
import type { ApiResponse } from '~/types/api';
import type { AuthUser, AuthUserFields } from '~/types/auth';
import { getApiHeaders } from '..';
/** Admin function to create a new user */
export async function createUser(
user: AuthUserFields & { password: string }
): Promise<{ success: true; user: AuthUser } | { success: false }> {
const { data, error } = await useFetch<ApiResponse<AuthUser>>(
getApiUrl('admin/users'),
{
method: 'POST',
headers: getApiHeaders(),
body: JSON.stringify({
name: user.name,
email: user.email,
password: user.password,
admin: user.admin,
}),
responseType: 'json',
}
);
if (data.value == null) {
toast.error('Create user', {
description: error.value?.data ?? 'Failed to create user',
});
return {
success: false,
};
}
toast.success('Create user', {
description: 'Successfully created user',
});
return {
success: true,
user: data.value.data,
};
}

View File

@@ -1,6 +1,7 @@
import { toast } from 'vue-sonner';
import type { ApiResponse } from '~/types/api';
import type { AuthUser } from '~/types/auth';
import { getApiHeaders } from '..';
export async function loginUser(
email: string,
@@ -10,9 +11,7 @@ export async function loginUser(
ApiResponse<{ token: string; user: AuthUser; expiresAt: number }>
>(getApiUrl('auth/login'), {
method: 'POST',
headers: {
'content-type': 'application/json',
},
headers: getApiHeaders(false),
body: JSON.stringify({
email: email,
password: password,