scuff/src/components/Login.svelte

35 lines
906 B
Svelte
Raw Normal View History

2022-11-12 20:11:50 -05:00
<script lang="ts">
import api from '$lib/api';
import { tokenStore, userStore } from '../stores';
2022-12-18 20:54:15 -05:00
import { PUBLIC_INSTANCE_URL } from '$env/static/public';
2022-11-12 20:11:50 -05:00
let username = '';
let password = '';
const onLogin = async () => {
2022-12-18 20:54:15 -05:00
const app = await api.oauth.createApp(PUBLIC_INSTANCE_URL);
const result = await api.oauth.getTokenFromCredentials(
PUBLIC_INSTANCE_URL,
app,
username,
password
);
2022-11-12 20:11:50 -05:00
tokenStore.set(result.access_token);
2022-12-18 20:54:15 -05:00
const user = await api.user.verifyCredentials(PUBLIC_INSTANCE_URL, result.access_token);
2022-11-12 20:11:50 -05:00
console.log('user', user);
userStore.set(user);
};
</script>
<form on:submit|preventDefault={onLogin}>
<label>
Username
<input type="text" name="" placeholder="username" bind:value={username} required />
</label>
<label>
Password
<input type="password" name="password" bind:value={password} required />
</label>
<button>Login</button>
</form>