55 lines
1.2 KiB
TypeScript
55 lines
1.2 KiB
TypeScript
import { Hono } from "hono";
|
|
import { AccountServiceInterface } from "../service/AccountServiceInterface";
|
|
|
|
export default function toRoutes(
|
|
accountService: AccountServiceInterface,
|
|
): Hono {
|
|
const app = new Hono();
|
|
|
|
app.post("/login", async (ctx) => {
|
|
try {
|
|
const { email, password } = await ctx.req.json();
|
|
|
|
const account = await accountService.login(email, password);
|
|
|
|
return ctx.json({
|
|
success: true,
|
|
accountId: account.id,
|
|
email: account.email,
|
|
});
|
|
} catch (error) {
|
|
return ctx.json(
|
|
{
|
|
success: false,
|
|
error: error instanceof Error ? error.message : "Erreur inconnue",
|
|
},
|
|
400,
|
|
);
|
|
}
|
|
});
|
|
|
|
app.post("/register", async (ctx) => {
|
|
try {
|
|
const { email, password } = await ctx.req.json();
|
|
|
|
const account = await accountService.createAccount(email, password);
|
|
|
|
return ctx.json({
|
|
success: true,
|
|
accountId: account.id,
|
|
email: account.email,
|
|
});
|
|
} catch (error) {
|
|
return ctx.json(
|
|
{
|
|
success: false,
|
|
error: error instanceof Error ? error.message : "Erreur inconnue",
|
|
},
|
|
400,
|
|
);
|
|
}
|
|
});
|
|
|
|
return app;
|
|
}
|