npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@finsel-dgi/pasby-next

v1.0.3

Published

Next.js authentication with pasby

Downloads

288

Readme

@finsel-dgi/pasby-next

A Next.js package for seamless integration with pasby eID authentication. This package provides both server and client-side utilities to implement pasby's OAuth-based electronic identification in your Next.js applications.

npm version

Features

  • 🔐 Secure OAuth-based authentication
  • 📱 Cross-device authentication support
  • 🎨 Customizable UI components
  • 🔄 Automatic session management
  • 📦 Type-safe API

Installation

npm install @finsel-dgi/pasby-next
# or
yarn add @finsel-dgi/pasby-next
# or
pnpm add @finsel-dgi/pasby-next

Requirements

  • Next.js 13.4 or higher
  • React 18 or higher
  • Node.js 16 or higher

Environment Setup

Create a .env.local file in your project root and add the following variables:

PASBY_CLIENT_SECRET=<App secret>
PASBY_CONSUMER_KEY=<organisations api key>
PASBY_CLIENT_ID=<App id>
PASBY_LOGIN_REDIRECT=<fallback path after authentication e.g /dashboard>
PASBY_LOGOUT_REDIRECT=<fallback path after logout e.g /login>
SECRET_GEN=<generated random 16 string>

Quick Start

1. Setup API Route

Create a new API route at app/api/eid/[auth]/route.ts:

import { handler } from "@finsel-dgi/pasby-next/server";
import { cookies } from "next/headers";

export const GET = handler({
  claims: ["naming.given", "naming.family", "contact.email"], // Add required claims
  action: 'signup', // login | signup | link
  payload: "User registration request" // describe action intent
}, (key, value, exp) => {
  cookies().set(key, value, { 
    secure: true, 
    sameSite: true, 
    maxAge: (exp) 
  });
}, async (key) => {
  return cookies().get(key)?.value;
});

2. Add Authentication Button

In your client component:

"use client"

import { LoginButton } from "@finsel-dgi/pasby-next";

export function AuthComponent() {
  return (
    <LoginButton 
      variant="dark" 
      action="identify"
      fallbackPath="/dashboard"
    />
  );
}

Available button variants:

  • original - Red background with white text
  • dark - Dark background with white text
  • light - White background with red text
  • darktext - White background with black text

3. Access User Data

In your server component:

import { getEID } from "@finsel-dgi/pasby-next/server";
import { cookies } from "next/headers";

export async function UserProfile() {
  const cookieStore = cookies();
  const eid = await getEID(cookieStore);

  if (!eid) {
    return <div>Not authenticated</div>;
  }

  return (
    <div>
      <h1>Welcome, {eid.claims.contact.email}</h1>
      {/* Access other user data as needed */}
    </div>
  );
}

User Data Structure

The getEID function returns a user object with the following structure:

interface User {
  national: string;
  country: string;
  claims: {
    contact: {
      email: string;
      phone: string;
      emailVerified: boolean;
      phoneVerified: boolean;
    };
    address: {
      country: string;
      city: string;
      countryCode: string;
      formatted: string;
      postCode: string;
      place: string;
    };
    bio: {
      birthdate: string;
      gender: string;
      birthplace: string;
      birthnumber: number;
      maritalStatus: string;
      age: number;
    };
    nationality: {
      pep: boolean;
      nationalities: string[];
      residence: string;
      primary: string;
    };
  };
}

End user session

Simply have a button that makes a router push to /api/eid/logout. Before doing so ensure you have provided a fallback logout path at your env as PASBY_LOGOUT_REDIRECT

Documentation

For detailed documentation and advanced usage, visit:

License

MIT © Finsel DGI