Files
sweet/docs/latest/examples/using-deno-kv-oauth.md
T
Asher Gomez 88985fe933 chore: update Deno KV OAuth plugin and docs (#1826)
These changes aim to improve the clarity and understandability of the
Deno KV OAuth plugin. It also updates to use the latest version of the
module.
2023-09-28 08:05:53 +00:00

5.4 KiB

description
description
Easily integrate OAuth 2.0 into your Fresh project with the official Deno KV OAuth plugin.

⚠️ Please note that this functionality relies on Deno KV which is currently marked as experimental and is subject to change. It is only available when using the --unstable flag.

⚠️ Please note that Deno KV OAuth is still in beta and is subject to change. See the documentation for further details.

⚠️ Please note that the minimum required version for plugins in Fresh is 1.3.0.

Fresh comes with an official Deno KV OAuth plugin based on the first-party Deno KV OAuth module. This plugin creates and configures your OAuth routes for your Fresh project.

Basic Setup

The most basic setup is that using a single provider with a pre-defined OAuth configuration. This automatically creates the following routes:

  • GET /oauth/signin
  • GET /oauth/callback
  • GET /oauth/signout

This is implemented as follows:

  1. Create your OAuth 2.0 application for your given provider.

    This example uses GitHub. However, you can choose from a list of providers that have pre-defined configurations here.

  2. Create your pre-defined OAuth configuration and configure Fresh to use the plugin.

    import { start } from "$fresh/server.ts";
    import {
      createGitHubOAuth2Client,
      createRoutes,
    } from "$fresh/plugins/kv_oauth.ts";
    import manifest from "./fresh.gen.ts";
    
    await start(manifest, {
      plugins: [
        {
          name: "kv-oauth",
          routes: createRoutes(createGitHubOAuth2Client()),
        },
      ],
    });
    
  3. Create and configure your protected route(s) using getSessionId().

    import type { Handlers } from "$fresh/server.ts";
    import { getSessionId } from "$fresh/plugins/kv_oauth.ts";
    
    export const handler: Handlers = {
      async GET(req) {
        return getSessionId(request) === undefined
          ? new Response("Unauthorized", { status: 401 })
          : new Response("You are allowed");
      },
    };
    
  4. ⚠️ While Deno KV is still experimental you need to add the --unstable option to the start task in the deno.json file.

    "start": "deno run -A --watch=static/,routes/ --unstable dev.ts",
    
  5. Start your project with the necessary environment variables.

    GITHUB_CLIENT_ID=xxx GITHUB_CLIENT_SECRET=xxx deno task start
    

Further Setup

The plugin is capable of having multiple providers, custom OAuth configurations and custom parent OAuth routes. These are implemented similar to the above example, as follows:

  1. Create your OAuth 2.0 applications for your given providers.

  2. Create your configurations and configure Fresh to use the plugin.

    import { start } from "$fresh/server.ts";
    import {
      createNotionOAuthConfig,
      createRoutes,
    } from "$fresh/plugins/kv_oauth.ts";
    import manifest from "./fresh.gen.ts";
    
    await start(manifest, {
      plugins: [
        {
          name: "kv-oauth",
          routes: [
            ...createRoutes(createNotionOAuthConfig(), "/oauth/notion"),
            ...createRoutes({
              clientId: getRequiredEnv("CUSTOM_CLIENT_ID"),
              clientSecret: getRequiredEnv("CUSTOM_CLIENT_SECRET"),
              authorizationEndpointUri: "https://custom.com/oauth/authorize",
              tokenUri: "https://custom.com/oauth/token",
              redirectUri: "https://my-site.com/oauth/custom/callback",
            }, "/oauth/custom"),
          ],
        },
      ],
    });
    

    Here, the 2nd parameter of createRoutes() is used to set the parent path for OAuth routes.

  3. Create and configure your protected route(s) using getSessionId().

    import type { Handlers } from "$fresh/server.ts";
    import { getSessionId } from "$fresh/plugins/kv_oauth.ts";
    
    export const handler: Handlers = {
      async GET(req) {
        return getSessionId(request) === undefined
          ? new Response("Unauthorized", { status: 401 })
          : new Response("You are allowed");
      },
    };
    
  4. ⚠️ While Deno KV is still experimental you need to add the --unstable option to the start task in the deno.json file.

    "start": "deno run -A --watch=static/,routes/ --unstable dev.ts",
    
  5. Start your project with the necessary environment variables.

    NOTION_CLIENT_ID=xxx NOTION_CLIENT_SECRET=xxx CUSTOM_CLIENT_ID=yyy CUSTOM_CLIENT_SECRET=yyy deno task start
    

Advanced Setups

If you require more advanced setups, you can create your own custom plugin that defines the routes and their behaviors. To do this, check out the following resources: