Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I don't get the example provided with sqlite #1

Open
wiredmatt opened this issue Jul 21, 2023 · 0 comments
Open

I don't get the example provided with sqlite #1

wiredmatt opened this issue Jul 21, 2023 · 0 comments

Comments

@wiredmatt
Copy link

source

why is the uuid static? I don't get how to apply this to a real app

This is the same but using drizzle instead, where are you supposed to get the id from?

import oauth2, { github } from "@bogeychan/elysia-oauth2";
import { randomBytes } from "crypto";
import { and, eq } from "drizzle-orm";
import { Elysia } from "elysia";
import { db } from "./db";
import { storage } from "./db/schema";

const states = new Set();

const auth = oauth2({
  profiles: {
    github: {
      provider: github(),
      scope: ["user"],
    },
  },
  state: {
    generate(req, name) {
      console.log("generate name", name);
      const state = randomBytes(8).toString("hex"); // ???
      states.add(state);
      return state;
    },
    check(req, name, state) {
      console.log("check state", name, state);
      if (states.has(state)) {
        states.delete(state);
        return true;
      }

      return false;
    },
  },
  storage: {
    async get(req, name) {
      console.log(`get token: ${name}`);

      const token = db
        .select()
        .from(storage)
        .where(and(eq(storage.name, name), eq(storage.id, id))); // -> where do I pull this ID from?

      if (!token) {
        return;
      }

      return JSON.parse(token);
    },
    async set(req, name, token) {
      console.log(`new token: ${name}`);

      db.insert(storage)
        .values({
          id, // -> where do I pull this ID from?
          name,
          token: JSON.stringify(token),
        })
        .onConflictDoUpdate({
          set: {
            token: JSON.stringify(token),
          },
          target: storage.id,
        });
    },
    async delete(req, name) {
      console.log(`delete token: ${name}`);

      db.delete(storage).where(
        and(eq(storage.name, name), eq(storage.id, id)) // -> where do I pull this ID from?
      );
    },
  },
});

new Elysia()
  .use(auth)
  .group("/auth", (authRouter) => {
    return authRouter.get("/protected", ({ headers }) => {
      return { foo: "bar" };
    });
  })
  .listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant