JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 14
  • Score
    100M100P100Q106319F
  • License MIT

A Remix Auth strategy for working with JWT

Package Exports

  • remix-auth-jwt
  • remix-auth-jwt/build/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (remix-auth-jwt) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

JwtStrategy

A Remix Auth strategy for working with JWT.

Supported runtimes

Runtime Has Support
Node.js
Cloudflare

How to use

First, install the strategy and Remix Auth.

$ npm install remix-auth remix-auth-jwt

Then, create an Authenticator instance.

// app/auth.server.ts
import { Authenticator } from "remix-auth";
import { sessionStorage } from "~/session.server";

export let authenticator = new Authenticator<{ requestname: string }>(sessionStorage);

And you can tell the authenticator to use the JwtStrategy.

import { JwtStrategy } from 'remix-auth-jwt';

// The rest of the code above here...

authenticator.use(
  new JwtStrategy(
    {
      secret: "s3cr3t",
    },
    // Define what to do when the request is authenticated
    async ({ payload, context }) => {
      // You can access decoded token values here using payload
      // and also use `context` to access more things from the server
      return payload;
    }
  ),
  // each strategy has a name and can be changed to use another one
  "jwt"
);

In order to authenticate a request, you can use the following inside of an loader function:

import { LoaderArgs } from "@remix-run/server-runtime";
import { authenticator } from "~/auth.server";

export async function loader({ params, request }: LoaderArgs) {
  const result = await authenticator.authenticate("jwt", request);
  return result;
}