Skip to main content
The Deno 2 Release Candidate is here
Learn more

oak_csrf

Extension of oak using deno_csrf.
This module influenced by oak_Session.
Filled with gratitude.

Usage

import {RedisStore} from "https://deno.land/x/sessions@v1.5.4/mod.ts";
import {CsrfVerify} from "https://deno.land/x/oak_csrf@0.0.1/mod.ts"

// Requires encryption key. It would be better to use environment variables
const key = Deno.env.get("CSRF_KEY") as string;

// Use one of the store published in https://deno.land/x/sessions@v1.5.4/mod.ts 
const store = new RedisStore({
  host: "redis",
  port: 6379,
});
await store.init();

const csrfVerify = new CsrfVerify(key, store);

router.get("/", async (context) => {
  const csrfToken = await context.state.session.get("csrfToken");
  // Please embed token in template
  const body = render(Deno.readTextFileSync("./page/form.html"), {
    token: csrfToken
  });

  context.response.body = body;
});

const app = new Application();
app.use(csrfVerify.verify());