Interested in developing a web application using Node.js that allows users to log in (authentication). The app will have 3 non-secure pages (/home, /contact, /about) and one secure page (/admin). I've been consulting the Mean Machine book from scotch.io for guidance.
I'm facing an issue with setting up the authentication. While the login feature works correctly and redirects me to /admin upon logging in, I can still access the /admin page by directly entering the URL without logging in. I need help figuring out where to implement the actual protection mechanism.
Here's an overview of how I structured my app. I'm looking for a conceptual answer on the correct approach rather than just a code snippet.
Services:
- The auth service sends the inputted username/password to the server and returns either false or success (along with user info and JWT token).
- The auth service also injects the token into each HTTP header as AuthInterceptor
Router:
angular.module('routerRoutes', ['ngRoute'])
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html',
controller: 'homeController',
controllerAs: 'home'
})
// More route configurations here...
.when('/admin', {
templateUrl: 'views/admin/admin.html',
controller: 'adminController',
controllerAs: 'admin'
});
$locationProvider.html5Mode(true);
});
Controllers:
homeController, aboutController, contactController are currently empty
adminController:
.controller('adminController', function($rootScope, $location, Auth) {
// Controller logic here });
});
Below is a snippet of my index.html file:
<body class="container" ng-app="meanApp" ng-controller="adminController as admin">
// HTML content here
</body>
If you have suggestions on improving my setup and any best practices I should follow, please let me know.
Lastly, I have a small question regarding the visibility of elements based on conditions in Angular:
- I expected that elements with "ng-if" directives wouldn't appear in the 'view source' if the condition wasn't met, but they do show up. Is this normal behavior?