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