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,
|
||||
|
||||
9
frontend/lib/schemas/admin.ts
Normal file
9
frontend/lib/schemas/admin.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import z from 'zod';
|
||||
import { registerSchema } from './auth';
|
||||
|
||||
export const createUserSchema = registerSchema.extend({
|
||||
admin: z
|
||||
.boolean()
|
||||
.default(false)
|
||||
.prefault(() => false),
|
||||
});
|
||||
23
frontend/lib/schemas/auth.ts
Normal file
23
frontend/lib/schemas/auth.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import z from 'zod';
|
||||
|
||||
export const registerSchema = z.object({
|
||||
name: z.string('This field is required').trim().min(1),
|
||||
email: z
|
||||
.email({
|
||||
error: 'This field is required',
|
||||
pattern: z.regexes.rfc5322Email,
|
||||
})
|
||||
.trim(),
|
||||
password: z.string('This field is required').trim().min(12).max(32),
|
||||
});
|
||||
|
||||
export const loginSchema = z.object({
|
||||
email: z
|
||||
.email({
|
||||
error: 'This field is required',
|
||||
pattern: z.regexes.rfc5322Email,
|
||||
})
|
||||
.trim(),
|
||||
// Don't include the min and max here to let bad actors waste their time
|
||||
password: z.string('This field is required').trim(),
|
||||
});
|
||||
Reference in New Issue
Block a user