From 16e70294871c42800b0c4a259e43603a810a690b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kan=C4=9Bra?= Date: Thu, 11 Jun 2020 18:16:30 +0200 Subject: [PATCH] feat(auth): finished serverMiddleware --- api/index.ts | 39 ++++++++++++++++++++++----------------- plugins/core.ts | 13 +++++-------- plugins/state.ts | 1 + plugins/types.d.ts | 3 +++ 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/api/index.ts b/api/index.ts index 1aef67c..052bd7f 100644 --- a/api/index.ts +++ b/api/index.ts @@ -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(' '); diff --git a/plugins/core.ts b/plugins/core.ts index 66dd5f3..a131188 100644 --- a/plugins/core.ts +++ b/plugins/core.ts @@ -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, }; } @@ -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, }; } } diff --git a/plugins/state.ts b/plugins/state.ts index 32446ff..73d152f 100644 --- a/plugins/state.ts +++ b/plugins/state.ts @@ -9,6 +9,7 @@ export const provideState = () => { uid: '', email: '', loggedIn: false, + admin: false, }, }); provide(StateSymbol, globalState); diff --git a/plugins/types.d.ts b/plugins/types.d.ts index fb66f6f..eb76b70 100644 --- a/plugins/types.d.ts +++ b/plugins/types.d.ts @@ -2,6 +2,9 @@ export type StateType = { user: { uid?: string; email?: string | null; + displayName?: string; + profilePicture?: string; + admin: boolean; loggedIn: boolean; }; };