middleware icon indicating copy to clipboard operation
middleware copied to clipboard

[firebase - auth] want to get uid from decoded idToken

Open rahInoue opened this issue 1 year ago • 2 comments

Hello!

I am using @hono/firebase-auth for verifying Firebase ID tokens. During this verification process, if I can obtain the UID in the context (for example, if the UID is stored in KV), I believe it would allow me to execute processes dependent on the UID without having to interact with the client, based solely on the authorization header.

This may not be a common practice, but if such a feature were available, it seems like it would reduce one concern for front-end tasks.

rahInoue avatar Sep 30 '24 12:09 rahInoue

Hi @rahInoue

@Code-Hex Can you handle this issue?

yusukebe avatar Oct 01 '24 03:10 yusukebe

@rahInoue Can you elaborate a little bit on your intent as I don't understand it 100%?

Given this example

import { Hono } from "hono";
import {
  VerifyFirebaseAuthConfig,
  VerifyFirebaseAuthEnv,
  verifyFirebaseAuth,
  getFirebaseToken,
} from "@hono/firebase-auth";

const config: VerifyFirebaseAuthConfig = {
  projectId: "some-id",
};

const app = new Hono<{ Bindings: VerifyFirebaseAuthEnv }>();

app.use("/api/*", verifyFirebaseAuth(config));

app.use(async (c, next) => {
  const idToken = getFirebaseToken(c);
  console.log("idToken.:>> ", idToken?.uid);
  await next();
});

app.get("/api/hello", (c) => {
  const idToken = getFirebaseToken(c);
  return c.json(idToken);
});

export default app;

It logs me the uid in the middleware and returns me the uid via the get handler. So it looks like you can get the uid of the token. What is missing for your use-case?

hffmnn avatar Oct 28 '24 14:10 hffmnn