2.0.0 • Published 6 months ago

elysia-oauth2 v2.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

elysia-oauth2

npm npm downloads

Elysia plugin for OAuth 2.0 Authorization Flow.

Powered by Arctic@2 with more than 53 oauth2 providers!

Installation

bun install elysia-oauth2 arctic

Update

if Arctic will release some new providers, you can update it with

bun install arctic@latest

Example

import { Elysia } from "elysia";
import { oauth2 } from "elysia-oauth2";

new Elysia()
    .use(
        oauth2({
            VK: [
                "clientID",
                "clientSecret",
                "https://example.com/auth/vk/callback",
            ],
        })
    )
    .get("/auth/vk", ({ oauth2 }) => oauth2.redirect("VK"))
    .get("/auth/vk/callback", async ({ oauth2 }) => {
        const tokens = await oauth2.authorize("VK");

        const accessToken = tokens.accessToken();

        // send request to API with token
    })
    .listen(3000);

!IMPORTANT You should return oauth2.redirect from the handler, because it relies on elysia's redirect() which need to return Response

CreateURL example

import { Elysia } from "elysia";
import { oauth2 } from "elysia-oauth2";

new Elysia()
    .use(
        oauth2({
            Google: [
                "clientID",
                "clientSecret",
                "https://example.com/auth/google/callback",
            ],
        })
    )
    .get("/auth/google", async ({ oauth2, redirect }) => {
        const url = oauth2.createURL("Google", ["email"]);
        url.searchParams.set("access_type", "offline");

        return redirect(url.href);
    })
    .get("/auth/google/callback", async ({ oauth2 }) => {
        const tokens = await oauth2.authorize("Google");

        const accessToken = tokens.accessToken();

        // send request to API with token
    })
    .listen(3000);

Options

You can configure plugin by providing options object in second argument.

import { Elysia } from "elysia";
import { oauth2 } from "elysia-oauth2";

new Elysia().use(
    oauth2(
        {},
        {
            cookie: {
                // defaults
                secure: true,
                sameSite: "lax",
                path: "/",
                httpOnly: true,
                maxAge: 60 * 30, // 30 min
            },
        }
    )
);

Supported providers

2.0.0

6 months ago

1.3.0

12 months ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.0.7

1 year ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.3

2 years ago

0.0.4

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago