node.js - Serve Angular app with express if user is authenticated -
we're building app created users in our database. app should not accessible no 1 else.
our thought serve simple html file, info app have. our backend app should nodejs , should check if user has cookie provided our authentication api , attached our domain. if user has cookie, should provide them app folder.
we want protect our js files , files belonging app public if not authenticated.
in simple html file, should have button says: "i'm authenticated, let me browse app".
<html lang="en"> <head> <meta charset="utf-8"> <title>landing page</title> </head> <body> <h1>landing page app!!!</h1> <input onclick="location.href='/app';" type="submit" value="i'm authenticated, let me browse app!"/> </body> </html>
the node server has route called /app
.
const express = require('express'); const app = express(); const port = process.env.port || 9090; const fs = require('fs'); app.use(express.static('public')); //only contains index.html app.listen(port, (err) => { if (err) { console.log(err); } }); app.get('/app', (req, res) => { if(req.user.isauthenticated){ //psuedo code below res.send(wholeangularapptouser()); } else{ // user should stay on landing page // information not authenticated } });
how can send whole angular app user?
unless 'i'm authenticated' button serves purpose during authentication process (like send credentials) should rid of , try access app directly. angular apps served static files should set static route that's protected middleware:
app.use('/app', function(req, res, next) { if (req.user.isauthenticated) { next() } else { res.sendfile(path.join(__dirname, 'public/index.html')) } }) app.use('/app', express.static('/app');
of course, wouldn't accept "isauthenticated" flag in request user being authenticated swap middleware bit more secure.
Comments
Post a Comment