summaryrefslogtreecommitdiff
path: root/src/pages/LogIn.tsx
blob: c787dcec5597727a9ea62228fe10112a5cdb36bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { useContext } from 'preact/hooks';
import { useSignal } from '@preact/signals';
import { route } from 'preact-router';
import { Pb } from '../pb.ts';

export const LogIn = () => {
	const pb = useContext(Pb);

	const email = useSignal('');
	const password = useSignal('');

	const onSubmit = async (event: SubmitEvent) => {
		event.preventDefault();
		const user = await pb.collection('users').authWithPassword(email.value, password.value);
		if (pb.authStore.isValid) {
			route('/' + user.username);
		}
	};

	return (
		<form onSubmit={onSubmit}>
			<label>
				Email:
				<input type="text" value={email} onInput={e => email.value = e.target.value} />
			</label>
			<label>
				Password:
				<input type="password" value={password} onInput={e => password.value = e.target.value} />
			</label>
			<button>Log In</button>
		</form>
	);
};