Remix Auth

Simple Authentication for Remix

README

Remix Auth


Simple Authentication for Remix


Features


- Full Server-Side Authentication
- Complete TypeScript Support
- Strategy-based Authentication
- Easily handle success and failure
- Implement custom strategies
- Supports persistent sessions

Overview


Remix Auth is a complete open-source authentication solution for Remix.run applications.

Heavily inspired by Passport.js, but completely rewrote it from scratch to work on top of the Web Fetch API. Remix Auth can be dropped in to any Remix-based application with minimal setup.

As with Passport.js, it uses the strategy pattern to support the different authentication flows. Each strategy is published individually as a separate npm package.

Installation


To use it, install it from npm (or yarn):

  1. ```bash
  2. npm install remix-auth
  3. ```

Also, install one of the strategies. A list of strategies is available in the Community Strategies discussion.

Usage


Remix Auth needs a session storage object to store the user session. It can be any object that implements the SessionStorage interface from Remix.

In this example I'm using the createCookieSessionStorage function.

  1. ```ts
  2. // app/services/session.server.ts
  3. import { createCookieSessionStorage } from "@remix-run/node";

  4. // export the whole sessionStorage object
  5. export let sessionStorage = createCookieSessionStorage({
  6.   cookie: {
  7.     name: "_session", // use any name you want here
  8.     sameSite: "lax", // this helps with CSRF
  9.     path: "/", // remember to add this so the cookie will work in all routes
  10.     httpOnly: true, // for security reasons, make this cookie http only
  11.     secrets: ["s3cr3t"], // replace this with an actual secret
  12.     secure: process.env.NODE_ENV === "production", // enable this in prod only
  13.   },
  14. });

  15. // you can also export the methods individually for your own usage
  16. export let { getSession, commitSession, destroySession } = sessionStorage;
  17. ```

Now, create a file for the Remix Auth configuration. Here import the Authenticator class and your sessionStorage object.

  1. ```ts
  2. // app/services/auth.server.ts
  3. import { Authenticator } from "remix-auth";
  4. import { sessionStorage } from "~/services/session.server";

  5. // Create an instance of the authenticator, pass a generic with what
  6. // strategies will return and will store in the session
  7. export let authenticator = new Authenticator<User>(sessionStorage);
  8. ```

The User type is whatever you will store in the session storage to identify the authenticated user. It can be the complete user data or a string with a token. It is completely configurable.

After that, register the strategies. In this example, we will use the FormStrategy to check the documentation of the strategy you want to use to see any configuration you may need.

  1. ```ts
  2. import { FormStrategy } from "remix-auth-form";

  3. // Tell the Authenticator to use the form strategy
  4. authenticator.use(
  5.   new FormStrategy(async ({ form }) => {
  6.     let email = form.get("email");
  7.     let password = form.get("password");
  8.     let user = await login(email, password);
  9.     // the type of this user must match the type you pass to the Authenticator
  10.     // the strategy will automatically inherit the type if you instantiate
  11.     // directly inside the `use` method
  12.     return user;
  13.   }),
  14.   // each strategy has a name and can be changed to use another one
  15.   // same strategy multiple times, especially useful for the OAuth2 strategy.
  16.   "user-pass"
  17. );
  18. ```

Now that at least one strategy is registered, it is time to set up the routes.

First, create a /login page. Here we will render a form to get the email and password of the user and use Remix Auth to authenticate the user.