2

I have a Node.js application where I have used Yeoman scaffolding for Angular. I am also using ExpressJS for server side.

So basically my app structure is:

app
  -->views
      --> more files
server.js

The structure is more heavy than this, but this is to keep it simple.

Basically I have used PassportJS on the server side for authentication. The routing is currently being carried out via AngularJS routing parameters.

I have come across an issue now as I need to carry out authentication by using a middleware method:

function ensureAuthenticated(req, res, next) {
    console.log('authenticate=' + req.isAuthenticated());
    if (req.isAuthenticated()) { 
        return next(); 
    }
    res.redirect('/')
}

I have created an app.get on the server side to check if the user tries to get to the admin page without logging in:

app.get('/admin', ensureAuthenticated,
    function(req, res){
        console.log('get admin' + req.params);
        res.sendfile(__dirname + '/app/views/admin/users.html');
    });  

But it never goes to the method. What am I doing wrong?

Any advice would be great.

2 Answers 2

1
if (!req.isAuthenticated()) {
            return res.send(401, 'User is not authorized');
        }
        next();
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, the authentication works if i just put /admin but my url is localhost:8888/#/admin. It never hits the route in express if i type the url in as shown
localhost:8888/#/admin is angular realm so you should change your logic I think ie put your view in admin path
I have a further issue now which is when I route to the admin page i press refresh on the browser and it tries to render the page but it states No default engine was specified. I am just using html pages?
imo all in all it's more easy/secure to manage admin side server side and not in angularjs that is quite hard
0

The reason it was not working was because angularjs locationprovider was not set to htmlmode5 when configuring the routes on the client side. The following line was added:

$locationProvider.html5Mode(true);

This basically removes the '#' from the url.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.