Skip to main content
Home
Works with
This package works with Node.js, Deno, Bun
This package works with Node.js
This package works with Deno
This package works with Bun
JSR Score88%
Published3 months ago (0.7.0)

lastlogin auth for smallweb

Lastlogin Authentication

Usage in smallweb

First wrap your endpoint in the lastlogin middleware.

// ~/smallweb/lastlogin-example/main.ts
import { lastlogin } from "jsr:@pomdtr/lastlogin";

export default {
  fetch: lastlogin((req: Request) => {
    const email = req.headers.get("X-Lastlogin-Email");
    if (!email) {
      return new Response("You are not logged in", { status: 401 } );
    }

    return new Response(`Hello, ${email}!`);
  }),
};

By default, routes are not protected behind auth. To protect all routes, you can provide the isProtected option.

// ~/smallweb/lastlogin-example/main.ts
import { lastlogin } from "jsr:@pomdtr/lastlogin";

export default {
  fetch: lastlogin(
    {
      // protect all routes behind auth
      isProtected: true,
    },
    (req: Request) => {
      const email = req.headers.get("X-Lastlogin-Email");
      return new Response(`Hello, ${email}!`);
    }
  ),
};

You can also provide a function to isProtected to protect only certain routes.

// ~/smallweb/lastlogin-example/main.ts
import { lastlogin } from "jsr:@pomdtr/lastlogin";

export default {
  fetch: lastlogin(
    {
      isProtected: (pathname: string) => {
        return pathname.startsWith("/admin");
      },
    },
    (req: Request) => {
      const email = req.headers.get("X-Lastlogin-Email");
      return new Response(`Hello, ${email}!`);
    }
  ),
};

If you only want to allow emails from a specific provider (ex: Google), you can provide the provider option.

// ~/smallweb/lastlogin-example/main.ts

import { lastlogin } from "jsr:@pomdtr/lastlogin";
export default {
  fetch: lastlogin({
      isProtected: true,
      provider: "google",
    }, (req: Request) => {
      const email = req.headers.get("X-Lastlogin-Email");
      return new Response(`Hello, ${email}!`);
    }
  ),
};

If you want to restrict access to certain emails, you can provide a isAuthorized function.

// ~/smallweb/lastlogin-example/main.ts
import { lastlogin } from "jsr:@pomdtr/lastlogin";

export default {
  fetch: lastlogin({
      isProtected: true,
      verifyEmail: async (email: string) => {
        return email == "pomdtr@smallweb.run";
      }
    }, (req: Request) => {
      const email = req.headers.get("X-Lastlogin-Email");
      return new Response(`Hello, ${email}!`);
    },
  ),
};

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@pomdtr/lastlogin

Import symbol

import * as lastlogin from "@pomdtr/lastlogin";
or

Import directly with a jsr specifier

import * as lastlogin from "jsr:@pomdtr/lastlogin";

Add Package

pnpm i jsr:@pomdtr/lastlogin
or (using pnpm 10.8 or older)
pnpm dlx jsr add @pomdtr/lastlogin

Import symbol

import * as lastlogin from "@pomdtr/lastlogin";

Add Package

yarn add jsr:@pomdtr/lastlogin
or (using Yarn 4.8 or older)
yarn dlx jsr add @pomdtr/lastlogin

Import symbol

import * as lastlogin from "@pomdtr/lastlogin";

Add Package

vlt install jsr:@pomdtr/lastlogin

Import symbol

import * as lastlogin from "@pomdtr/lastlogin";

Add Package

npx jsr add @pomdtr/lastlogin

Import symbol

import * as lastlogin from "@pomdtr/lastlogin";

Add Package

bunx jsr add @pomdtr/lastlogin

Import symbol

import * as lastlogin from "@pomdtr/lastlogin";