Skip to content

Update auth.md to add a note about authentication on nuxt server routes #1423

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions docs/nuxt/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,54 @@ export default defineNuxtConfig({
},
})
```

**Note:** The session cookie doesn't get verified automatically in the server routes yet. Till the time this is supported natively, you can authenticate incoming requests using your own implementation. For e.g.

```ts
import { H3Event } from "h3";

import {
App,
getApps,
initializeApp,
applicationDefault,
} from "firebase-admin/app";
import { getAuth } from "firebase-admin/auth";

const useFirebase = () => {
let app: App;
if (!getApps().length) {
app = initializeApp({
credential: applicationDefault(),
});
} else {
[app] = getApps();
}

const auth = getAuth(app);

return {
app,
auth,
};
};

export const authenticateRequest = async (event: H3Event) => {
const { auth } = useFirebase();

const sessCookie = getCookie(event, "__session");
if (sessCookie) {
try {
const decodedUser = await auth.verifySessionCookie(sessCookie);

return decodedUser;
} catch (error) {
console.log("failed to authenticate request", error);
}
}

return null;
};
```

For `applicationDefault` to work, you must set the env var `GOOGLE_APPLICATION_CREDENTIALS` with your `service-account.json` file path or its content.