Skip to content

Commit

Permalink
Merge pull request #11 from portier/feat/upgrade
Browse files Browse the repository at this point in the history
Node.js 18, ESM, dependency upgrades
  • Loading branch information
stephank authored Nov 10, 2023
2 parents 7da2e83 + 4611074 commit 3871f51
Show file tree
Hide file tree
Showing 14 changed files with 1,616 additions and 126 deletions.
56 changes: 17 additions & 39 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,32 @@ name: Build

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Checkout
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x

- name: Npm cache
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Npm install
run: npm install

- name: Test
run: npm run-script test
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18.x
cache: npm

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ^1.16
- name: Npm install
run: npm install

- name: Go cache
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Test
run: npm run-script test

- name: Build tester
run: go get -v github.com/portier/client-tester
- name: Build tester
run: go install github.com/portier/client-tester@latest

- name: Run test suite
run: ~/go/bin/client-tester -bin ./client-tester.js
- name: Run test suite
run: ~/go/bin/client-tester -bin ./client-tester.js
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/dist
/node_modules
/package-lock.json
/yarn.lock
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 });
```
7 changes: 4 additions & 3 deletions client-tester.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

const { PortierClient } = require(".");
import PortierClient from "./dist/index.js";
import { createInterface } from "node:readline";

if (process.argv.length !== 3) {
console.error("Broker required");
Expand All @@ -12,7 +13,7 @@ const client = new PortierClient({
redirectUri: "http://imaginary-client.test/fake-verify-route",
});

const rl = require("readline").createInterface({
const rl = createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
Expand All @@ -25,7 +26,7 @@ const wrap = (fn) => {
},
(err) => {
console.log(`err\t${err.message}`);
}
},
);
};

Expand Down
Loading

0 comments on commit 3871f51

Please sign in to comment.