Skip to content

FirebaseServerApp #366

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

Merged
merged 8 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"license": "Apache 2.0",
"dependencies": {
"firebase": "^8.10.0",
"firebase-admin": "^12.0.0",
"firebaseui": "^5.0.0"
}
}
43 changes: 43 additions & 0 deletions auth/service-worker-sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,46 @@ function svcSignInEmail(email, password) {
});
// [END auth_svc_sign_in_email]
}

function svcRedirectAdmin() {
const app = { use: (a) => {} };

// [START auth_svc_admin]
// Server side code.
const admin = require('firebase-admin');

// The Firebase Admin SDK is used here to verify the ID token.
admin.initializeApp();

function getIdToken(req) {
// Parse the injected ID token from the request header.
const authorizationHeader = req.headers.authorization || '';
const components = authorizationHeader.split(' ');
return components.length > 1 ? components[1] : '';
}

function checkIfSignedIn(url) {
return (req, res, next) => {
if (req.url == url) {
const idToken = getIdToken(req);
// Verify the ID token using the Firebase Admin SDK.
// User already logged in. Redirect to profile page.
admin.auth().verifyIdToken(idToken).then((decodedClaims) => {
// User is authenticated, user claims can be retrieved from
// decodedClaims.
// In this sample code, authenticated users are always redirected to
// the profile page.
res.redirect('/profile');
}).catch((error) => {
next();
});
} else {
next();
}
};
}

// If a user is signed in, redirect to profile page.
app.use(checkIfSignedIn('/'));
// [END auth_svc_admin]
}
12 changes: 12 additions & 0 deletions firebaseserverapp-next/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "firebaseserverapp-next",
"version": "1.0.0",
"scripts": {
"compile": "cp ../tsconfig.json.template ./tsconfig.json && tsc"
},
"license": "Apache-2.0",
"dependencies": {
"firebase": "^10.0.0",
"next": "^14.1.3"
}
}
27 changes: 27 additions & 0 deletions firebaseserverapp-next/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { headers } from 'next/headers';
// @ts-expect-error this is not in the firebase js sdk yet
import { initializeServerApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { redirect } from 'next/navigation';

const config = {};

// [START firebaseserverapp_auth]
export default async function MyServerComponent({ params }) {

// get relevant request headers (NextJS)
const authIdToken = headers().get('Authorization')?.split('Bearer ')[1];

// Initialize the Firebase server app instance
const serverApp = initializeServerApp(config, { authIdToken });

// Initialize the auth SDK
const auth = getAuth(serverApp);

if (auth.currentUser) {
redirect('/profile');
}

// ...
}
// [END firebaseserverapp_auth]