I am in the process of building a login page that checks certain conditions and redirects the user to the home page. However, I want to implement a feature where once the user has visited the home page, they cannot go back to the login page by clicking the back button on their browser. I attempted to use a variable called "isLoggedIn" that starts as false, then gets set to true after checking the username and password. I stored this variable in local storage as a flag, but I seem to be missing something
Below is my app.js code:
var validationApp = angular.module('validationApp',['ngRoute','ngStorage']);
var loggedIn = false;
validationApp.run(function($rootScope, $localStorage){
//var loggedIn = false;
//if(!loggedIn) {
storage = window.localStorage;
//storage.setItem("loggedIn", true);
//console.log(storage.getItem("loggedIn"));
// $rootScope.$storage = $localStorage.$default({
// loggedIn: false
// })
// }
});
validationApp.controller('mainController', function($scope,loginservice,$localStorage) {
$scope.dologin = function () {
//storage.setItem("loggedIn" ,true);
//loginsucess = storage.getItem("loggedIn");
if(loggedIn == false) {
//console.log(loginsucess);
if ($scope.userForm.$valid) {
loginservice.login($scope.user.email, $scope.user.password);
}
storage.setItem("loggedIn", true);
}
}
});
I also tried implementing a check condition in my routes, but now even with valid credentials, I cannot access the home page.
validationApp.config(
function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'main.html',
controller: 'mainController'
})
.when('/home', {
templateUrl: 'home.html',
resolve:{
"check":function($location){
if(storage.getItem(loggedIn) == true)
{
alert(loggedIn);
$location.path("/home");
}
else
{
$location.path("/main");
}
}
},
controller: 'mainController'
})
.otherwise({
redirectTo: '/'
});
});
validationApp.service('loginservice',function($location)
{
this.login = function(username, password){
console.log(username,password);
if(username == "example@email.com" && password=="1234")
{
//alert('thank you for submitting your form');
$location.path("/home");
//console.log(storage.getItem("loggedIn"));
}
else
{
//alert("invalid username and password");
$location.path("/main");
}
}
});