Firebase Update
This commit is contained in:
@ -1,120 +1,160 @@
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
var session;
|
||||
|
||||
// var firebase = require('firebase');
|
||||
var admin = require('firebase-admin');
|
||||
//Database
|
||||
var postgres = require("./../db-connect/db-connect");
|
||||
|
||||
// GoogleOauth2.0
|
||||
passport = require('passport'),
|
||||
auth = require('../public/login/auth');
|
||||
auth(passport);
|
||||
router.use(passport.initialize());
|
||||
|
||||
cookieParser = require('cookie-parser'),
|
||||
cookieSession = require('cookie-session');
|
||||
// Initialize Firebase
|
||||
// const config = {
|
||||
// apiKey: "AIzaSyCuvwf78cmSDoZ2yS4XxHZhnjUn7yIHYfw",
|
||||
// authDomain: "test-667ca.firebaseapp.com",
|
||||
// databaseURL: "https://test-667ca.firebaseio.com",
|
||||
// projectId: "test-667ca",
|
||||
// storageBucket: "test-667ca.appspot.com",
|
||||
// messagingSenderId: "221332577314"
|
||||
// };
|
||||
// firebase.initializeApp(config);
|
||||
|
||||
router.use(cookieSession({
|
||||
name: 'session',
|
||||
keys: ['aoisdzflkudheowe777elkjv']
|
||||
}));
|
||||
var serviceAccount = require('../login/firebaseAdminKey.json');
|
||||
|
||||
router.use(cookieParser());
|
||||
|
||||
function isLoggedIn(req, res, next) {
|
||||
console.log("Auth: ", req.session.passport.user.profile.id);
|
||||
if (req.session.passport.user.profile.id) {
|
||||
return next();
|
||||
}
|
||||
res.redirect('/')
|
||||
}
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
// if (req.session.passport.user.token) {
|
||||
// res.redirect('/dash');
|
||||
// } else {
|
||||
res.redirect('/auth/google');
|
||||
// }
|
||||
});
|
||||
router.get('/auth/google', passport.authenticate('google', {
|
||||
scope: ['https://www.googleapis.com/auth/userinfo.profile']
|
||||
}));
|
||||
router.get('/auth/google/callback',
|
||||
passport.authenticate('google', {failureRedirect:'/'}),
|
||||
(req, res) => {
|
||||
// req.session.passport.user.token = req.user.token;
|
||||
res.redirect('/dash');
|
||||
}
|
||||
);
|
||||
|
||||
router.get('/logout', (req, res) => {
|
||||
req.logout();
|
||||
res.redirect("https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://localhost:7000/");
|
||||
var firebaseAdmin = admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
databaseURL: 'https://test-667ca.firebaseio.com'
|
||||
});
|
||||
|
||||
router.get('/dash', isLoggedIn, function(req, res, next) {
|
||||
// console.log("Session: ", req.session.passport.user.token);
|
||||
// console.log("User ID: " + req.session.passport.user.profile.id);
|
||||
// if (req.session.passport.user.token) {
|
||||
res.render("index");
|
||||
|
||||
// Login und Dash
|
||||
router.get('/', function(req, res, next) {
|
||||
res.render('index');
|
||||
});
|
||||
|
||||
|
||||
router.get('/dash/:idtoken', function(req, res, next) {
|
||||
|
||||
// } else {
|
||||
// res.redirect('/auth/google');
|
||||
// }
|
||||
var token = req.params.idtoken;
|
||||
var uid;
|
||||
firebaseAdmin.auth().verifyIdToken(token)
|
||||
.then(function(decodedToken) {
|
||||
uid = decodedToken.uid;
|
||||
// HIER WEITER MACHEN
|
||||
// uid ist nur hier drinen verfügbar
|
||||
|
||||
|
||||
}).catch(function(error) {
|
||||
console.log(error)
|
||||
});
|
||||
|
||||
|
||||
res.render('dash');
|
||||
});
|
||||
|
||||
router.get('/logout', (req, res, next) => {
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
// SmartShopper
|
||||
|
||||
//GET Own shoppinglists as JSON
|
||||
|
||||
router.get("/myshoppinglists", async function(req, res, next) {
|
||||
|
||||
try {
|
||||
//Get user id: req.session.passport.user.profile.id
|
||||
res.status(200).send(await postgres.getShoppinglistsAdmin(req.session.passport.user.profile.id));
|
||||
}
|
||||
|
||||
catch(err) {
|
||||
res.status(400).send(await err);
|
||||
}
|
||||
router.get("/myshoppinglists",async function(req, res, next) {
|
||||
var token = req.query.idtoken;
|
||||
console.log("/myshoppinglists idtoken: ", token)
|
||||
firebaseAdmin.auth().verifyIdToken(token)
|
||||
.then(async function(decodedToken) {
|
||||
uid = decodedToken.uid;
|
||||
console.log("uid", uid)
|
||||
try {
|
||||
//Get user id: req.session.passport.user.profile.id
|
||||
res.status(200).send(await postgres.getShoppinglistsAdmin(uid));
|
||||
}
|
||||
|
||||
catch(err) {
|
||||
res.status(400).send(await err);
|
||||
}
|
||||
|
||||
}).catch(function(error) {
|
||||
console.log(error)
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
//GET Shared shoppinglists as JSON
|
||||
|
||||
router.get("/sharedshoppinglists", async function(req, res, next) {
|
||||
try {
|
||||
//Get user id: req.session.passport.user.profile.id
|
||||
res.status(200).send(await postgres.getShoppinglistsShared(req.session.passport.user.profile.id));
|
||||
}
|
||||
|
||||
catch(err) {
|
||||
res.status(400).send(await err);
|
||||
}
|
||||
var token = req.query.idtoken;
|
||||
console.log("/sharedshoppinglists idtoken: ", token)
|
||||
var uid;
|
||||
firebaseAdmin.auth().verifyIdToken(token)
|
||||
.then(async function(decodedToken) {
|
||||
uid = decodedToken.uid;
|
||||
console.log("UID: ", uid);
|
||||
try {
|
||||
//Get user id: das da oben
|
||||
res.status(200).send(await postgres.getShoppinglistsShared(uid));
|
||||
}
|
||||
|
||||
catch(err) {
|
||||
res.status(400).send(await err);
|
||||
}
|
||||
}).catch( async function(error) {
|
||||
console.log(error)
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
//POST new shoppinglist
|
||||
|
||||
router.post("/shoppinglist", async function(req, res, next) {
|
||||
try {
|
||||
res.status(200).send(await postgres.newShoppinglist(req.body.name, req.body.description, req.session.passport.user.profile.id, req.body.color));
|
||||
}
|
||||
|
||||
catch(err) {
|
||||
res.status(400).send(await err);
|
||||
}
|
||||
});
|
||||
|
||||
console.log("/shoppinglist idtoken: ", req.body.idtoken)
|
||||
var token = req.body.idtoken;
|
||||
|
||||
var uid;
|
||||
firebaseAdmin.auth().verifyIdToken(token)
|
||||
.then(async function(decodedToken) {
|
||||
uid = decodedToken.uid;
|
||||
console.log("UID: ", uid);
|
||||
console.log("So andere sachen: ", req.body.name, req.body.description);
|
||||
try {
|
||||
|
||||
res.status(200).send(await postgres.newShoppinglist(req.body.name, req.body.description, uid, req.body.color));
|
||||
}
|
||||
|
||||
catch(err) {
|
||||
res.status(400).send(await err);
|
||||
}
|
||||
}).catch(function(error) {
|
||||
console.log(error)
|
||||
});
|
||||
});
|
||||
//GET Shoppinglist detail
|
||||
|
||||
router.get("/shoppinglist_json/:sl_id", async (req, res) => {
|
||||
try {
|
||||
res.status(200).send(await postgres.displayShoppinglist(req.params.sl_id, req.session.passport.user.profile.id));
|
||||
}
|
||||
|
||||
catch(err) {
|
||||
res.status(400).send(await err);
|
||||
}
|
||||
var token = req.query.idtoken;
|
||||
var uid;
|
||||
firebaseAdmin.auth().verifyIdToken(token)
|
||||
.then(async function(decodedToken) {
|
||||
uid = decodedToken.uid;
|
||||
console.log("UID: ", uid);
|
||||
try {
|
||||
res.status(200).send(await postgres.displayShoppinglist(req.params.sl_id, uid));
|
||||
}
|
||||
|
||||
catch(err) {
|
||||
res.status(400).send(await err);
|
||||
}
|
||||
}).catch(function(error) {
|
||||
console.log(error)
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
//DELETE Shoppinglist
|
||||
@ -159,7 +199,15 @@ router.get("/test1", (req, res) => {
|
||||
});
|
||||
|
||||
router.get("/userinfo_json", (req, res) => {
|
||||
res.send(req.session.passport.user);
|
||||
var token = req.query.idtoken;
|
||||
|
||||
console.log("/userinfo_json idtoken: ", token)
|
||||
firebaseAdmin.auth().verifyIdToken(token)
|
||||
.then(function(decodedToken) {
|
||||
res.send(decodedToken);
|
||||
}).catch(function(error) {
|
||||
console.log(error)
|
||||
});
|
||||
});
|
||||
|
||||
// Invite System
|
||||
@ -175,16 +223,22 @@ router.post("/invite", async (req, res) => {
|
||||
});
|
||||
|
||||
router.get("/invite/:link", async (req, res) => {
|
||||
try {
|
||||
await postgres.verifyInvite(req.params.link, req.session.passport.user.profile.id);
|
||||
res.redirect("/dash");
|
||||
}
|
||||
|
||||
catch(err) {
|
||||
res.redirect("/dash");
|
||||
}
|
||||
|
||||
|
||||
var token = req.query.idtoken;
|
||||
var uid;
|
||||
firebaseAdmin.auth().verifyIdToken(token)
|
||||
.then(function(decodedToken) {
|
||||
uid = decodedToken.uid;
|
||||
console.log("UID: ", uid);
|
||||
try {
|
||||
postgres.verifyInvite(req.params.link, uid);
|
||||
res.redirect("/dash");
|
||||
}
|
||||
catch(err) {
|
||||
res.redirect("/dash");
|
||||
}
|
||||
}).catch(function(error) {
|
||||
console.log(error)
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
Reference in New Issue
Block a user