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

@slashid/remix

v0.2.22

Published

![SlashID Remix SDK](https://raw.githubusercontent.com/slashid/javascript/main/packages/remix/slashid_remix_banner.png)

Downloads

310

Readme

SlashID Remix SDK

npm build

Documentation

Check out our developer docs for guides and API documentation.

Quick Start

@slashid/remix requires Remix v2.

You will need to sign up to SlashID and create an organization, once you've done this you'll find your organization ID in Settings -> General.

1. Install @slashid/remix

Once you have a Remix application ready to go, you need to install the SlashID Remix SDK. This SDK provides a prebuilt log-in & sign-up form, control components and hooks - tailor made for Remix.

npm install @slashid/remix

2. Create SlashID app primitives

In your Remix application create a file app/slashid.ts.

In this file create the SlashID application primitives and re-export them, you'll use them later.

// app/slashid.ts

import { createSlashIDApp } from "@slashid/remix";

export const { SlashIDApp, slashIDRootLoader, slashIDLoader } =
  createSlashIDApp({ oid: "YOUR_ORGANIZATION_ID" });

Tip: oid is your organization ID, you can find it in the SlashID console under Settings -> General.

3. Configure slashIDRootLoader

To configure SlashID in your Remix application you'll need to update your root loader. With a small change you'll have easy access to authentication in all of your Remix routes.

Import the slashIDRootLoader you created in the previous step, invoke it and export it as loader.

// root.ts

import { LoaderFunction } from "@remix-run/node";
import {
  Links,
  LiveReload,
  Meta,
  Outlet,
  Scripts,
  ScrollRestoration,
  useLoaderData,
} from "@remix-run/react";

import { slashIDRootLoader } from "~/slashid";

export const loader: LoaderFunction = slashIDRootLoader();

export default function App() {
  return (
    <html lang="en">
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
      </head>
      <body>
        <Outlet />
        <ScrollRestoration />
        <Scripts />
        <LiveReload />
      </body>
    </html>
  );
}

If you need to load additional data via the root loader, you can simply pass a loader function to slashIDRootLoader. You can even check for authentication right here in the root loader.

// root.ts

import { slashIDRootLoader } from "~/slashid";
import { getUser } from "@slashid/remix";

export const loader: LoaderFunction = slashIDRootLoader((args) => {
  const user = getUser(args);

  if (user) {
    // the user is logged in
  }

  // fetch data

  return {
    hello: "world!", // your data
  };
});

4. Wrap your application body with <SlashIDApp>

In step 2 you created SlashIDApp, it provides authentication state to your React component tree. There is no configuration, just add it to your Remix application.

// root.tsx

import { SlashIDApp } from "~/slashid";

export default function App() {
  return (
    <html lang="en">
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
      </head>
      <body>
        {/* Wrap the contents of your <body> with SlashIDApp */}
        <SlashIDApp>
          <Outlet />
          <ScrollRestoration />
          <Scripts />
          <LiveReload />
        </SlashIDApp>
      </body>
    </html>
  );
}

5. Create your log-in/sign-up page

SlashID offers a prebuilt form that works for both log-in and sign-up. Users with an account can log-in here and users without one need to simply complete the form to create their account.

// routes/login.tsx

import { ConfigurationProvider, Form, type Factor } from "@slashid/remix";

const factors: Factor[] = [{ method: "email_link" }];

export default function LogIn() {
  return (
    <div style={{ width: "500px" }}>
      <ConfigurationProvider factors={factors}>
        <Form />
      </ConfigurationProvider>
    </div>
  );
}

After the user has logged in, you might want to redirect them to another page. You can this with a loader.

When authentication has complete the form will tell the loader to run again, refreshing the authentication context, and triggering the redirect.

// routes/login.tsx

import { slashIDLoader } from "~/slashid";

export const loader = slashIDLoader((args) => {
  const user = getUser(args);

  if (user) {
    return redirect("index"); // redirect to your desired destination
  }

  return {};
});

6. Protecting your pages

Server side

To protect your routes you can use the slashIDLoader you created in step 2. This utility is a wrapper for your loaders that provides authentication state to your loader code.

You'll check if the user exists, and if not redirect them to the login page.

The useSlashID() hook provides authentication state & helper functions to your React code, here you'll implement logOut too.

// routes/_index.tsx

import { slashIDLoader } from "~/slashid";
import { getUser, useSlashID } from "@slashid/remix";

export const loader = slashIDLoader((args) => {
  const user = getUser(args);

  if (!user) {
    return redirect("login");
  }

  return {};
});

export default function Index() {
  const { logOut } = useSlashID();

  return (
    <div>
      <h1>Index</h1>
      <p>You are logged in!</p>
      <button onClick={logOut}>Log out</button>
    </div>
  );
}

Client side

SlashID has several Control Components that allow you to conditionally show or hide content based on the users authentication state.

You'll implement <LoggedIn>, <LoggedOut>, and provide the option to log-out by implementing the logOut helper function from the useSlashID() hook.

// routes/_index.tsx

import { LoggedIn, LoggedOut, useSlashID } from "@slashid/remix";
import { useNavigate } from "@remix-run/react";

export default function Index() {
  const { logOut } = useSlashID();
  const navigate = useNavigate();

  return (
    <div>
      <LoggedIn>
        You are logged in!
        <button onClick={logOut}>Log out</button>
      </LoggedIn>
      <LoggedOut>{navigate("login")}</LoggedOut>
    </div>
  );
}