This package allows easily integrate Firebase Authentication/Identity Platform to your Hono API project.
- Hono middleware to decode and verify JWT token issued by Firebase Authentication service
- Hono context variable
currentUser
for you to access any where in your application - Allowed to custom
currentUser
shape
With NPM
npm install @fiboup/hono-firebase-auth
With Yarn
yarn add @fiboup/hono-firebase-auth
With pnpm
pnpm add @fiboup/hono-firebase-auth
import { Hono } from "hono";
import { validateFirebaseAuth } from "@fiboup/hono-firebase-auth";
const app = new Hono();
app.use(
"*",
validateFirebaseAuth({
projectId: "<your_firebase_project_id>",
})
);
Note: Go to your Firebase project, then visit Project settings for the project id
import { Hono } from "hono";
import { JwtDecodeError } from "@fiboup/hono-firebase-auth";
const app = new Hono();
app.onError((err, c) => {
if (err instanceof JwtDecodeError) {
return new Response(err.message, {
status: 401,
});
}
console.log(err);
return new Response("Uncaught exception", {
status: 500,
});
});
You can get the current user from Hono context with the key currentUser
import { Hono } from "hono";
import { validateFirebaseAuth } from "@fiboup/hono-firebase-auth";
const app = new Hono();
app.use(
"*",
validateFirebaseAuth({
projectId: "<your_firebase_project_id>",
})
);
app.get('/me', (c) => {
const currentUser = c.get('currentUser')
return c.json(currentUser)
})
To make it type-safe, you can use DefaultFirebaseAuthInjectedVariables
import type { DefaultFirebaseAuthInjectedVariables } from "@fiboup/hono-firebase-auth";
type Variables = DefaultFirebaseAuthInjectedVariables
const app = new Hono<{ Variables: Variables }>()
If you want to custom the context variable key, you can specify in the middleware config
import { Hono } from "hono";
import { validateFirebaseAuth } from "@fiboup/hono-firebase-auth";
const app = new Hono();
app.use(
"*",
validateFirebaseAuth({
projectId: "<your_firebase_project_id>",
currentUserContextKey: 'custom_current_user'
})
);
Leave the currentUserContextKey
a blank string to disable current user context
import { Hono } from "hono";
import { validateFirebaseAuth } from "@fiboup/hono-firebase-auth";
import type { DecodedIdToken } from "@fiboup/firebase-auth";
type MyCustomCurrentUser = {
userId: string
}
const app = new Hono();
app.use(
"*",
validateFirebaseAuth({
transformCurrentUser: <MyCustomCurrentUser>(decodedToken: DecodedIdToken) => {
return {
userId: decodedToken.sub,
}
}
})
);
To make your custom current user context type-safe, don't forget to write your custom Hono variable
type Variables = {
currentUser?: MyCustomCurrentUser
}
const app = new Hono<{ Variables: Variables }>()
MIT © Fiboup