Created
October 3, 2019 21:58
-
-
Save djpeach/788bce6bc2a5b60b0ec96ad87662e46a to your computer and use it in GitHub Desktop.
Final app.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var path = require('path'); | |
var cookieParser = require('cookie-parser'); | |
var logger = require('morgan'); | |
const cors = require('cors') | |
const admin = require('firebase-admin') | |
const serviceAccount = require("./config/fbServiceAccountKey.json"); | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: "https://fbauthdemo-2a451.firebaseio.com" | |
}); | |
var app = express(); | |
app.use(cors()) | |
app.use(logger('dev')); | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
function checkAuth(req, res, next) { | |
if (req.headers.authtoken) { | |
admin.auth().verifyIdToken(req.headers.authtoken) | |
.then(() => { | |
next() | |
}).catch(() => { | |
res.status(403).send('Unauthorized') | |
}); | |
} else { | |
res.status(403).send('Unauthorized') | |
} | |
} | |
app.use('/', checkAuth) | |
app.get('/', (req, res) => { | |
res.json({ | |
message: 'Hello World!' | |
}) | |
}) | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment