Skip to content

Commit

Permalink
feat(auth): finished serverMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKanera committed Jun 11, 2020
1 parent 5f85ce0 commit 16e7029
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
39 changes: 22 additions & 17 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import * as admin from 'firebase-admin';

export default function(req: any, res: any, next: () => void) {
export default async function(req: any, res: any, next: () => void) {
const serviceAccount = require('../service-account.json');

// The Firebase Admin SDK is used here to verify the ID token.
console.log(admin.apps.length);

if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
}

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: any) => {
res.locals.user = decodedClaims;
})
.catch(() => {
next();
});
const idToken = getIdToken();

try {
const { uid, email } = await admin.auth().verifyIdToken(idToken);
const dataResult = await admin
.firestore()
.collection('users')
.doc(uid)
.get();

const userData = dataResult.data();

res.locals.user = {
uid,
email,
displayName: userData?.displayName,
profilePicture: userData?.profilePicture,
admin: userData?.admin,
};
} catch {}

next();

function getIdToken(req: any) {
function getIdToken() {
// Parse the injected ID token from the request header.
const authorizationHeader = req.headers.authorization || '';
const components = authorizationHeader.split(' ');
Expand Down
13 changes: 5 additions & 8 deletions plugins/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ export default (context) => {

if (process.server) {
const { res, beforeNuxtRender } = context;
const user = res.locals.user;
const userData = res.locals.user;

console.log(res.locals);
console.log(userData);

if (user) {
if (userData) {
state.user = {
uid: user.uid,
email: user.email,
...userData,
loggedIn: true,
};
}
Expand All @@ -36,9 +35,7 @@ export default (context) => {

if (nuxtState.serverState.user) {
state.user = {
uid: nuxtState.serverState.user.uid,
email: nuxtState.serverState.user.email,
loggedIn: nuxtState.serverState.user.loggedIn,
...nuxtState.serverState.user,
};
}
}
Expand Down
1 change: 1 addition & 0 deletions plugins/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const provideState = () => {
uid: '',
email: '',
loggedIn: false,
admin: false,
},
});
provide(StateSymbol, globalState);
Expand Down
3 changes: 3 additions & 0 deletions plugins/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export type StateType = {
user: {
uid?: string;
email?: string | null;
displayName?: string;
profilePicture?: string;
admin: boolean;
loggedIn: boolean;
};
};

0 comments on commit 16e7029

Please sign in to comment.