Start adding some handlers for routes.

This commit is contained in:
Dave Smith-Hayes 2024-03-03 21:57:16 -05:00
parent 62d345b666
commit 134c9904c0
5 changed files with 48 additions and 29 deletions

View File

@ -1,6 +1,6 @@
import { FC } from "hono/jsx"
const Error: FC = (props) => {
const Error = (props: { message: string }) => {
return (
<div class="form-error">
{props.message}

View File

@ -0,0 +1,9 @@
import { Layout } from "../layout";
export const HomePage = (props) => {
return (
<Layout title="Home">
<div>Welcome to Slovocast</div>
</Layout>
);
};

View File

@ -1,11 +1,11 @@
import { Layout } from '../layout';
import { LoginForm } from '../components/login-form';
const LoginPage = (props: { error?: any }) => {
export const LoginPage = (props: { error?: any }) => {
return (
<Layout title="Login">
<LoginForm action="/login" error=error />
<LoginForm action="/login" error={props.error} />
</Layout>
);
};
export default LoginPage;

View File

@ -0,0 +1,28 @@
import { Hono } from 'hono';
import { Context } from 'hono';
import { LoginPage } from '../frontend/pages/login';
const LoginHandler = new Hono();
LoginHandler.get('/login', async (c: Context) => {
return c.html(<LoginPage />);
});
LoginHandler.post('/login', async (c: Context) => {
const form = await c.req.formData();
// validate form
if (form.has('login')) {
if (form.get('email') == 'me@davesmithhayes.com') {
if (form.get('password') == 'test') {
c.set('auth', true);
return c.redirect('/');
}
}
}
c.set('login-errors', {});
return c.redirect('/login');
});
export default LoginHandler;

View File

@ -1,35 +1,17 @@
import { Hono } from 'hono';
import Layout from './frontend/layout';
import { SiteConfig, configMiddleware } from './middleware/config';
import LoginForm from './frontend/user/login';
import { configMiddleware } from './middleware/config';
import { HomePage } from './frontend/pages/home';
import { LoginPage } from './frontend/pages/login';
import LoginHandler from './handlers/login';
const app = new Hono();
app.use(configMiddleware);
app.get('/', async (c) => {
const siteData: SiteConfig = c.get('config');
const layout = (
<Layout siteData={siteData}>
<main>
<h1>Welcome to Slovocast</h1>
</main>
</Layout>
);
return c.html(layout);
return c.html(<HomePage />);
});
app.get('/login', async (c) => {
const config: SiteConfig = c.get('config');
return c.html(
<Layout siteData={config}>
<main>
<LoginForm action="/login" />
</main>
</Layout>
);
});
// Handlers
app.route('/*', LoginHandler);
export default app;