Skip to content

Commit a9160f6

Browse files
authored
FirebaseServerApp (#366)
* 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
1 parent e74e8fd commit a9160f6

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

auth/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"license": "Apache 2.0",
88
"dependencies": {
99
"firebase": "^8.10.0",
10+
"firebase-admin": "^12.0.0",
1011
"firebaseui": "^5.0.0"
1112
}
1213
}

auth/service-worker-sessions.js

+43
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,46 @@ function svcSignInEmail(email, password) {
162162
});
163163
// [END auth_svc_sign_in_email]
164164
}
165+
166+
function svcRedirectAdmin() {
167+
const app = { use: (a) => {} };
168+
169+
// [START auth_svc_admin]
170+
// Server side code.
171+
const admin = require('firebase-admin');
172+
173+
// The Firebase Admin SDK is used here to verify the ID token.
174+
admin.initializeApp();
175+
176+
function getIdToken(req) {
177+
// Parse the injected ID token from the request header.
178+
const authorizationHeader = req.headers.authorization || '';
179+
const components = authorizationHeader.split(' ');
180+
return components.length > 1 ? components[1] : '';
181+
}
182+
183+
function checkIfSignedIn(url) {
184+
return (req, res, next) => {
185+
if (req.url == url) {
186+
const idToken = getIdToken(req);
187+
// Verify the ID token using the Firebase Admin SDK.
188+
// User already logged in. Redirect to profile page.
189+
admin.auth().verifyIdToken(idToken).then((decodedClaims) => {
190+
// User is authenticated, user claims can be retrieved from
191+
// decodedClaims.
192+
// In this sample code, authenticated users are always redirected to
193+
// the profile page.
194+
res.redirect('/profile');
195+
}).catch((error) => {
196+
next();
197+
});
198+
} else {
199+
next();
200+
}
201+
};
202+
}
203+
204+
// If a user is signed in, redirect to profile page.
205+
app.use(checkIfSignedIn('/'));
206+
// [END auth_svc_admin]
207+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @ts-nocheck
2+
// [START serverapp_auth]
3+
import { initializeServerApp } from 'firebase/app';
4+
import { getAuth } from 'firebase/auth';
5+
import { headers } from 'next/headers';
6+
import { redirect } from 'next/navigation';
7+
8+
export default function MyServerComponent() {
9+
10+
// Get relevant request headers (in Next.JS)
11+
const authIdToken = headers().get('Authorization')?.split('Bearer ')[1];
12+
13+
// Initialize the FirebaseServerApp instance.
14+
const serverApp = initializeServerApp(firebaseConfig, { authIdToken });
15+
16+
// Initialize Firebase Authentication using the FirebaseServerApp instance.
17+
const auth = getAuth(serverApp);
18+
19+
if (auth.currentUser) {
20+
redirect('/profile');
21+
}
22+
23+
// ...
24+
}
25+
// [END serverapp_auth]
26+
27+
const firebaseConfig = {};

firebaseserverapp-next/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "firebaseserverapp-next",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"compile": "cp ../tsconfig.json.template ./tsconfig.json && tsc"
6+
},
7+
"license": "Apache-2.0",
8+
"dependencies": {
9+
"firebase": "^10.0.0",
10+
"next": "^14.1.3"
11+
}
12+
}

0 commit comments

Comments
 (0)