Skip to content

Commit

Permalink
FirebaseServerApp (#366)
Browse files Browse the repository at this point in the history
* First.

* already snippets for that

* cleanup

* auth_svc_admin

* Auth, not auth-next

* Express stub should not be in snippet

* Move import into snippet

* Update firebaseserverapp.js
  • Loading branch information
jamesdaniels authored Mar 13, 2024
1 parent e74e8fd commit a9160f6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
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]
}
27 changes: 27 additions & 0 deletions firebaseserverapp-next/firebaseserverapp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @ts-nocheck
// [START serverapp_auth]
import { initializeServerApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { headers } from 'next/headers';
import { redirect } from 'next/navigation';

export default function MyServerComponent() {

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

// Initialize the FirebaseServerApp instance.
const serverApp = initializeServerApp(firebaseConfig, { authIdToken });

// Initialize Firebase Authentication using the FirebaseServerApp instance.
const auth = getAuth(serverApp);

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

// ...
}
// [END serverapp_auth]

const firebaseConfig = {};
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"
}
}

0 comments on commit a9160f6

Please sign in to comment.