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

next-auth-http-adapter

v0.3.0

Published

next auth adapter for authentication over http

Downloads

110

Readme

next-auth-http-adapter

next-auth-http-adapter is an adapter for next-auth that allows you to use any HTTP API as a database. It is useful for when you want to use a database that is not supported by next-auth, or when you want your server to be the source of truth for authentication but still want to take advantage of next-auth's session management and providers in your next applications.

Installation

# npm
npm install next-auth-http-adapter

# yarn
yarn add next-auth-http-adapter

# pnpm
pnpm add next-auth-http-adapter

Usage

Under the hood next-auth-http-adapter use ofetch for making requests to your backend. Responses from your backend are validated using zod. Take a loog at the validation schemas If the response is not in the expected format, an error is thrown. Check out Converting the response to convert responses from your backend to desired format.

You can almost configure anything about the each request being made to your backend. The only thing you need to do is to configure the adapter providing all configurations for all the callbacks. An example of how to do this is shown below:

import { httpAdapter } from "next-auth-http-adapter";

const myHttpAdapter = httpAdapter({
  baseURL: "http://localhost:8000", // or any other base url
  headers: {
    Authorization: process.env.REMOTE_AUTH_RPC_TOKEN!,
    // or set any global headers to be able to authenticate your requests to your backend
  },
  // you can provide any other
  adapterProcedures: {
    createUser(user) {
      return {
        path: "auth/signup/",
        method: "POST",
        body: user,
      };
    },
    getUserById: (id) => ({
      path: `auth/get-user/${id}/`,
    }),
    getUserByEmail: (email) => ({
      path: `auth/get-user-by-email/${encodeURIComponent(email)}/`,
    }),
    getUserByAccount: ({ providerAccountId, provider }) => ({
      path: `auth/get-user-by-account/${encodeURIComponent(
        provider
      )}/${encodeURIComponent(providerAccountId)}/`,
    }),
    updateUser: (user) => ({
      path: "auth/update-user/",
      method: "PATCH",
    }),
    deleteUser: (id) => ({
      path: `auth/delete-user/${id}/`,
      method: "DELETE",
    }),
    linkAccount: (account) => ({
      path: "auth/link-account/",
      method: "POST",
      body: account,
    }),
    unlinkAccount: ({ provider, providerAccountId }) => ({
      path: `auth/unlink-account/${encodeURIComponent(
        provider
      )}/${encodeURIComponent(providerAccountId)}/`,
      method: "DELETE",
    }),
    createSession: (session) => ({
      path: "auth/create-session/",
      method: "POST",
      body: session,
    }),
    getSessionAndUser: (sessionToken) => ({
      path: `auth/get-session/${sessionToken}/`,
    }),
    updateSession: (session) => ({
      path: "auth/update-session/",
      method: "PATCH",
      body: session,
    }),
    deleteSession: (sessionToken) => ({
      path: `auth/delete-session/${sessionToken}/`,
      method: "DELETE",
    }),
    createVerificationToken: (verificationToken) => ({
      path: "auth/create-verification-token/",
      method: "POST",
      body: verificationToken,
    }),
    useVerificationToken: (params) => ({
      path: "auth/use-verification-token/",
      method: "POST",
      body: params,
    }),
  },
});

Passing adpater to next-auth is as simple as:

import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";

NextAuth({
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_OAUTH2_KEY!,
      clientSecret: process.env.GOOGLE_OAUTH2_SECRET!,
    }),
  ],
  adapter: myHttpAdapter,
  // other next-auth options
});

Converting the response from your backend to the expected format

You can provide select functions that are used to convert the response from your backend to the expected format before being validated. You can provide serializers for each of the callbacks.

/**
 * Assuming that your backend returns the following response for the `createUser` callback of the adapter:
 *
 * {
 *    id: 1,
 *    user_email: "[email protected]"
 *    full_name: "John Doe",
 *    email_verified: true,
 *    image: "https://example.com/john-doe.png"
 * }
 */

function createUserSerializer(response) {
  return {
    id: response.id,
    name: response.full_name,
    email: response.user_email,
    image: response.image,
    emailVerified: response.email_verified,
  };
}

const myHttpAdapter = httpAdapter({
  adapterProcedures: {
    createUser(user) {
      return {
        path: "auth/signup/",
        method: "POST",
        body: user,
        select: createUserSerializer,
      };
    },
    // ......
  },
});

License

MIT