better errors
This commit is contained in:
parent
78addafe18
commit
0f53063832
@ -69,17 +69,17 @@ export function Routes(services: Services) {
|
||||
);
|
||||
router.get(
|
||||
'/read/:uuid',
|
||||
CheckPermissions(),
|
||||
ReadUserSchema(),
|
||||
SchemaValidator(),
|
||||
CheckPermissions(),
|
||||
ReadHandler(services),
|
||||
);
|
||||
|
||||
router.post(
|
||||
'/create',
|
||||
CheckPermissions(),
|
||||
CreateUserSchema(),
|
||||
SchemaValidator(),
|
||||
CheckPermissions(),
|
||||
CreateHandler(services),
|
||||
);
|
||||
|
||||
|
@ -1,10 +1,25 @@
|
||||
<script>
|
||||
import 'papercss/dist/paper.min.css';
|
||||
import { onMount } from 'svelte';
|
||||
import { Router, Route } from 'svelte-navigator';
|
||||
import { currentUser } from './store/user';
|
||||
import { read } from './functions/user';
|
||||
|
||||
import Test3D from './pages/test3D.svelte';
|
||||
import Login from './pages/Login.svelte';
|
||||
import Profile from './pages/Profile.svelte';
|
||||
|
||||
onMount(async () => {
|
||||
if ($currentUser) {
|
||||
try {
|
||||
const user = await read($currentUser.uuid);
|
||||
currentUser.set(user);
|
||||
} catch (error) {
|
||||
currentUser.set(null);
|
||||
window.location.href = '/';
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<Router>
|
||||
|
@ -4,18 +4,15 @@ enum Methods {
|
||||
'PUT' = 'PUT',
|
||||
}
|
||||
|
||||
export async function get<T>(route: string): Promise<T | null> {
|
||||
export async function get<T>(route: string): Promise<T> {
|
||||
return request<T>('GET', route);
|
||||
}
|
||||
|
||||
export async function post<T>(
|
||||
route: string,
|
||||
data?: unknown,
|
||||
): Promise<T | null> {
|
||||
export async function post<T>(route: string, data?: unknown): Promise<T> {
|
||||
return request<T>('POST', route, data);
|
||||
}
|
||||
|
||||
export async function put<T>(route: string, data: unknown): Promise<T | null> {
|
||||
export async function put<T>(route: string, data: unknown): Promise<T> {
|
||||
return request<T>('PUT', route, data);
|
||||
}
|
||||
|
||||
@ -23,7 +20,7 @@ async function request<T>(
|
||||
method: keyof typeof Methods,
|
||||
route: string,
|
||||
data?: unknown,
|
||||
): Promise<T | null> {
|
||||
): Promise<T> {
|
||||
const response = await fetch(`${process.env.APIURL}${route}`, {
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
@ -34,8 +31,11 @@ async function request<T>(
|
||||
body: data ? JSON.stringify(data) : null,
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(await response.json());
|
||||
|
||||
if (response.ok && response.status !== 204) {
|
||||
return response.json() as T;
|
||||
}
|
||||
return null;
|
||||
|
||||
return {} as T;
|
||||
}
|
||||
|
@ -1,22 +1,18 @@
|
||||
import type { CreateUserBody, LoginUserBody, UserInfo } from '@core';
|
||||
import { post, get } from './request';
|
||||
import { currentUser } from '../store/user';
|
||||
|
||||
export async function login(raw: LoginUserBody) {
|
||||
const user = await post<UserInfo>('/user/login', raw);
|
||||
currentUser.set(user);
|
||||
return user;
|
||||
return post<UserInfo>('/user/login', raw);
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
await post('/user/logout');
|
||||
currentUser.set(null);
|
||||
await post<void>('/user/logout');
|
||||
}
|
||||
|
||||
export async function read(uuid: string) {
|
||||
const user = await get<UserInfo>(`/user/read/${uuid}`);
|
||||
return get<UserInfo>(`/user/read/${uuid}`);
|
||||
}
|
||||
|
||||
export async function create(raw: CreateUserBody) {
|
||||
const user = await post<UserInfo>('/user/create', raw);
|
||||
return post<UserInfo>('/user/create', raw);
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
import NavBar from '../components/NavBar.svelte';
|
||||
import { Form, Input, Button } from 'spaper';
|
||||
import { login } from '../functions/user';
|
||||
import { currentUser } from '../store/user';
|
||||
import { useNavigate } from 'svelte-navigator';
|
||||
|
||||
const user: LoginUserBody = {
|
||||
@ -13,10 +14,11 @@
|
||||
const navigate = useNavigate();
|
||||
|
||||
const loginHandler = async () => {
|
||||
const result = await login(user);
|
||||
if (result) {
|
||||
try {
|
||||
const res = await login(user);
|
||||
currentUser.set(res);
|
||||
navigate('/');
|
||||
}
|
||||
} catch (error) {}
|
||||
};
|
||||
</script>
|
||||
|
||||
@ -39,7 +41,7 @@
|
||||
type="secondary"
|
||||
class="row sm-2 col-4"
|
||||
block
|
||||
on:click={() => loginHandler()}>Sign in</Button
|
||||
on:click={loginHandler}>Sign in</Button
|
||||
>
|
||||
</Form>
|
||||
</div>
|
||||
|
@ -8,12 +8,16 @@
|
||||
const navigate = useNavigate();
|
||||
|
||||
const logoutHandler = async () => {
|
||||
try {
|
||||
await logout();
|
||||
} finally {
|
||||
currentUser.set(null);
|
||||
navigate('/');
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<Button type="danger" block on:click={() => logoutHandler()}>Logout</Button>
|
||||
<Button type="danger" block on:click={logoutHandler}>Logout</Button>
|
||||
{$currentUser?.uuid}
|
||||
{$currentUser?.username}
|
||||
{$currentUser?.role}
|
||||
|
Loading…
x
Reference in New Issue
Block a user