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

@skavl-media/subrite-auth

v1.0.1

Published

Authentication package for Subrite

Downloads

121

Readme

@skavl-media/subrite-auth

A l library for integrating authentication with subrite and next.js applications.

Installation

npm install @skavl-media/subrite-auth

Peer Dependencies

This package requires the following peer dependencies:

npm install next@^15.0.0 [email protected] react@^19

Usage

Initialize Environment Variables

First, set up the required environment variables in your .env file:

NEXT_PUBLIC_SUBRITE_URL="https://minside.your-subrite-url.com"
NEXT_PUBLIC_SUBRITE_CLIENT_ID="your-subrite-client-id"
SUBRITE_CLIENT_SECRET="your-subrite-client-secret"
SUBRITE_API_URL="https://api.subrite.no"
NEXT_PUBLIC_NEXTAUTH_URL="http://localhost:3010" # Your Next.js application URL
NEXTAUTH_SECRET="your-nextauth-secret"

Mandatory Configuration

in your app/api/auth/[...nextauth]/route.ts file:

import NextAuth, { NextAuthOptions } from 'next-auth';
import { getAuthConfig } from '@skavl-media/subrite-auth';

const handler = NextAuth(getAuthConfig() as NextAuthOptions);

export { handler as GET, handler as POST };

in your app/layout.tsx

import { getServerAuth } from '@skavl-media/subrite-auth';
import { SessionContextProviders } from '@skavl-media/subrite-auth/components';

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  const { session } = await getServerAuth();
  return (
    <html lang="en">
      <head>
        <link rel="icon" href="/images/favicon.ico" type="image/png" />
      </head>
      <body>
        <SessionContextProviders session={session}>{children}</SessionContextProviders>
      </body>
    </html>
  );
}

You'll need a page to redirect users for authenticating and redirecting back to your application. Create a new page at app/auth-check/pages.tsx with the following content:


'use client';

import { useAuthCheck } from '@skavl-media/subrite-auth/hooks';

export default function AuthCheck() {
  useAuthCheck();

  return (
    <div>
      <p>Checking authentication...</p>
    </div>
  );
}

You'll need a page for signout callback. Create a new page at app/auth-check/signout/pages.tsx with the following content:


'use client';

import { useSignOut } from '@skavl-media/subrite-auth/hooks';

export default function SignOut() {
  useSignOut();

  return (
    <div className="flex items-center justify-center">
      <h1 className="text-center">Logging out..</h1>
    </div>
  );
}

Helper Hooks

Login and logout url.

You can get the login and logout url by using the useLoginUrl and useLogOutUrl hooks.

import { useAuth, useLoginUrl, useLogOutUrl } from '@skavl-media/subrite-auth/hooks';

export default function Header() {
  const { isLoggedIn } = useAuth();
  const loginUrl = useLoginUrl();
  const logOutUrl = useLogOutUrl();

  return (
    <header>
      {isLoggedIn ? (
        <>
          <a href={logOutUrl}>Log out</a>
          <a href={process.env.NEXT_PUBLIC_SUBRITE_URL}> My Page</a>
        </>
      ) : (
        <a href={loginUrl}>Log in</a>
      )}
    </header>
  );
}

You can get user authentication information on client side by using the useAuth hook.

'use client';
import { useAuth } from '@skavl-media/subrite-auth/hooks';

export default function Content() {
  const { session, user, hasAccess, isAdmin, isActiveSubscriber, checkPackage, checkProduct } =
    useAuth();

  return (
    <div>
      <div>
        <h1>Welcome {user?.name}</h1>
        <p>Has access: {hasAccess ? 'Yes' : 'No'}</p>
        <p>Is admin: {isAdmin ? 'Yes' : 'No'}</p>
        <p>Is active subscriber: {isActiveSubscriber ? 'Yes' : 'No'}</p>
        <p>Check package: {checkPackage('packageId') ? 'Yes' : 'No'}</p>
        <p>Check product: {checkProduct('productId') ? 'Yes' : 'No'}</p>
        <p> Session: {JSON.stringify(session)}</p>
      </div>
    </div>
  );
}

Adding cta/checkout link search parameter:

import { useCheckoutParams } from '@skavl-media/subrite-auth/hooks';

export function CtaButton({ link }) {
  const subriteParams = useCheckoutParams();
  const linkURL = new URL(link);
  linkURL.search = subriteParams.toString();

  return <a href={linkURL.toString()}>Buy this subscription</a>;
}

Server Side Rendering

You can get user authentication information on server side by using the getServerAuth function.

import { getServerAuth } from '@skavl-media/subrite-auth';

async function ServerContent() {
  const { session, user, hasAccess, isAdmin, isActiveSubscriber, checkPackage, checkProduct } =
    await getServerAuth();

  return (
    <div>
      <div>
        <h1>Welcome {user?.name}</h1>
        <p>Has access: {hasAccess ? 'Yes' : 'No'}</p>
        <p>Is admin: {isAdmin ? 'Yes' : 'No'}</p>
        <p>Is active subscriber: {isActiveSubscriber ? 'Yes' : 'No'}</p>
        <p>Check package: {checkPackage('packageId') ? 'Yes' : 'No'}</p>
        <p>Check product: {checkProduct('productId') ? 'Yes' : 'No'}</p>
        <p> Session: {JSON.stringify(session)}</p>
      </div>
    </div>
  );
}