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):
- ```bash
- npm install remix-auth
- ```
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.
- ```ts
- // app/services/session.server.ts
- import { createCookieSessionStorage } from "@remix-run/node";
- // export the whole sessionStorage object
- export let sessionStorage = createCookieSessionStorage({
- cookie: {
- name: "_session", // use any name you want here
- sameSite: "lax", // this helps with CSRF
- path: "/", // remember to add this so the cookie will work in all routes
- httpOnly: true, // for security reasons, make this cookie http only
- secrets: ["s3cr3t"], // replace this with an actual secret
- secure: process.env.NODE_ENV === "production", // enable this in prod only
- },
- });
- // you can also export the methods individually for your own usage
- export let { getSession, commitSession, destroySession } = sessionStorage;
- ```
Now, create a file for the Remix Auth configuration. Here import the Authenticator class and your sessionStorage object.
- ```ts
- // app/services/auth.server.ts
- import { Authenticator } from "remix-auth";
- import { sessionStorage } from "~/services/session.server";
- // Create an instance of the authenticator, pass a generic with what
- // strategies will return and will store in the session
- export let authenticator = new Authenticator<User>(sessionStorage);
- ```
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.
- ```ts
- import { FormStrategy } from "remix-auth-form";
- // Tell the Authenticator to use the form strategy
- authenticator.use(
- new FormStrategy(async ({ form }) => {
- let email = form.get("email");
- let password = form.get("password");
- let user = await login(email, password);
- // the type of this user must match the type you pass to the Authenticator
- // the strategy will automatically inherit the type if you instantiate
- // directly inside the `use` method
- return user;
- }),
- // each strategy has a name and can be changed to use another one
- // same strategy multiple times, especially useful for the OAuth2 strategy.
- "user-pass"
- );
- ```
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.