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

remix-session-csrf

v1.0.0

Published

The CSRF helper from v6 of [remix-utils](https://github.com/sergiodxa/remix-utils/tree/main) published as a standalone package for Node.

Downloads

1,379

Readme

remix-session-csrf

The CSRF helper from v6 of remix-utils published as a standalone package for Node.

Installation

npm install remix-session-csrf

Usage

Set up a session store:

// app/utils/session.server.ts

import { createCookieSessionStorage } from '@remix-run/node';

export const sessionStorage = createCookieSessionStorage({
    cookie: {
        name: '__session', // use any name you want here
        sameSite: 'lax', 
        path: '/',
        httpOnly: true, // for security reasons, make this cookie http only
        secrets: ['s3cr3t'], // replace this with an actual secret from env variable
        secure: process.env.NODE_ENV === 'production', // enable this in prod only
    },
});

export const { getSession, commitSession, destroySession } = sessionStorage;

Configure your root.tsx to generate a csrf token, and pass it to the client.

// app/root.tsx

import { json, type LoaderFunctionArgs } from '@remix-run/node';
import { AuthenticityTokenProvider } from 'remix-session-csrf/react';
import { verifyAuthenticityToken } from 'remix-session-csrf/server';
import {
    // rest of imports
    useLoaderData,
} from '@remix-run/react';

import { commitSession, getSession } from '~/utils/session.server';

export async function loader({ request }: LoaderFunctionArgs) {
    const session = await getSession(request.headers.get('cookie'));
    const csrfToken = createAuthenticityToken(session);

    return json({
        csrfToken,
    }, {
        headers: {
            'Set-Cookie': await commitSession(session),
        },
    })
}

export default function App() {
    const { csrfToken } = await useLoaderData<typeof loader>();

    return (
        <AuthenticityTokenProvider token={csrfToken}>
            {/* App markup */}
        </AuthenticityTokenProvider>
    )
}

Then, add a token and validation to your form + action:

// app/routes/some-route.tsx

import { type ActionFunctionArgs } from '@remix-run/node';
import { Form } from '@remix-run/react';
import { AuthenticityTokenInput } from 'remix-session-csrf/react';
import { verifyAuthenticityToken } from 'remix-session-csrf/server';

import { getSession } from '~/utils/session.server';

export async function action({ request }: ActionFunctionArgs)  {
    const session = await getSession(request.headers.get('Cookie'));
    await verifyAuthenticityToken(request, session);

    return {}
}

export default function Component() {
    return (
        <Form method="POST">
            <AuthenticityTokenInput />
            <input type="text" name="name">
            <button type="submit">Submit</button>
        </Form>
    )
}

You can also use the useAuthenticityToken hook to get the token and validation function:

// app/routes/some-route.tsx

import { type ActionFunctionArgs } from '@remix-run/node';
import { useFetcher } from '@remix-run/react';
import { useAuthenticityToken } from 'remix-session-csrf/react';
import { verifyAuthenticityToken } from 'remix-session-csrf/server';

import { getSession } from '~/utils/session.server';

export async function action({ request }: ActionFunctionArgs) {
    const session = await getSession(request.headers.get('Cookie'));
    await verifyAuthenticityToken(request, session);

    return null;
}

export default function Component() {
    const fetcher = useFetcher();
    const csrf = useAuthenticityToken();

    return (
        <div>
            <button
                type="button"
                onClick={() => {
                    fetcher.submit({ csrf, name: 'Steve' }, { method: 'POST' });
                }}
            >
                Submit
            </button>
        </div>
    );
}