create users through admin page
This commit is contained in:
43
frontend/lib/api/admin/createUser.ts
Normal file
43
frontend/lib/api/admin/createUser.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user