Skip to content

Commit

Permalink
Update README example
Browse files Browse the repository at this point in the history
  • Loading branch information
stephank committed Nov 10, 2023
1 parent e339e38 commit 4611074
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `
<p>Enter your email address:</p>
<form method="post" action="/auth">
<input name="email" type="email">
<button type="submit">Login</button>
</form>
`);
`;
});

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(`
<p>Verified email address ${email}!</p>
`);
});
app.post("/verify", async (req, res) => {
if (req.body.error) {
res.type("text/html");
return `
<p>Error: ${req.body.error_description}</p>
`;
}

const email = await portier.verify(req.body.id_token);

res.type("text/html");
return `
<p>Verified email address ${email}!</p>
`;
});

app.listen(8000);
app.listen({ port: 8000 });
```

0 comments on commit 4611074

Please sign in to comment.