diff --git a/README.md b/README.md index 049dcdd..8184d03 100644 --- a/README.md +++ b/README.md @@ -7,39 +7,48 @@ A [Portier] client library for Node.js ### Example ```js -const express = require("express"); -const formParser = require("body-parser").urlencoded({ extended: false }); -const { PortierClient } = require("portier"); +import Fastify from "fastify"; +import formPlugin from "@fastify/formbody"; +import PortierClient from "portier"; const portier = new PortierClient({ redirectUri: "http://localhost:8000/verify", }); -const app = express(); +const app = Fastify(); +app.register(formPlugin); app.get("/", (req, res) => { - res.type("html").end(` + res.type("text/html"); + return `
Enter your email address:
- `); + `; }); -app.post("/auth", formParser, (req, res) => { - portier.authenticate(req.body.email).then((authUrl) => { - res.redirect(303, authUrl); - }); +app.post("/auth", async (req, res) => { + const authUrl = await portier.authenticate(req.body.email); + res.redirect(303, authUrl); }); -app.post("/verify", formParser, (req, res) => { - portier.verify(req.body.id_token).then((email) => { - res.type("html").end(` -Verified email address ${email}!
- `); - }); +app.post("/verify", async (req, res) => { + if (req.body.error) { + res.type("text/html"); + return ` +Error: ${req.body.error_description}
+ `; + } + + const email = await portier.verify(req.body.id_token); + + res.type("text/html"); + return ` +Verified email address ${email}!
+ `; }); -app.listen(8000); +app.listen({ port: 8000 }); ```