SITUATION:
I am trying to check if my user is authenticated when he makes a request to the server.
I googled a bit and found this:
How to show different page if user is logged in via firebase
So here's my try at implementing this solution using this library:
https://github.com/js-cookie/js-cookie/blob/latest/src/js.cookie.js
CODE:
server-side
var cookies = require("cookie-parser");
router.get("/", function(req, res, next){
const { token } = req.cookies;
console.log('Verifying token', token);
});
client-side
<script src="/public/js/cookieScript.js"> </script>
<a href="/upload" class = "authIn uploadButton btn btn-success pull-right">UPLOAD</a>
<script>
const setAppCookie = () => firebase.auth().currentUser &&
firebase.auth().currentUser.getToken().then(token => {
Cookies.set('token', token, {
domain: window.location.hostname,
expire: 1 / 24, // One hour
path: '/',
secure: true // If served over HTTPS
});
});
</script>
In addition to requiring cookie-parser you should also configure your express app to use it:
var express = require('express');
var app = express();
var cookies = require("cookie-parser");
app.use(cookies());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With