Ionic login with session

 I'm new to angular js and i'm going to build a mobile application using  ionic framework. I'm struggling with my session because when i login into the home page if i click the back button at the top it just return to the log in page. What i want to do is that when i'm login into the page it will not go back to login page only if i log out. this is my codes for login. Please help me thank you :D

This is my Controller.js for login

             .controller('LoginCtrl', function($scope, $state, LoginService) {
              $scope.form = {};
  
               '$scope.login = function(){'

               if((angular.isDefined($scope.form.username)
               && $scope.form.username !== "")
               && angular.isDefined($scope.form.password)
               && $scope.form.password !== ""){

               LoginService.login($scope.form).then(function(response){
         
               if(Storage)
               {
       

               localStorage.setItem('loginDetails',JSON.stringify
               (response.data.username));
               $state.go('classattendance');
               } else {
   alert('String is not defined in this browser. please use other   storage.');
     }
      }, function(error){
  
      });
   } else {
               }
               }
               })

And this is for my class controller

.controller('ClassCtrl', function ($state, $scope, $http) {
 if (localStorage['loginDetails'] == 'undefined')
    {
   alert("Invalid access"); 
      $state.go('login');
    }
     })
               This is my Service.js
       .service('LoginService', function($http);
      var vm = this;
       var url = 'http://127.0.0.1/mobile/';

               vm.login = function(loginData){

   return $http.post(url + 'login_service.php',loginData);
    }
   })

and this is my login_service.php

   <?php
	          header("Access-Control-Allow-Origin: *");	
	          require_once('connection.php');
	
	          $post_data = file_get_contents('php://input');
	          $request = json_decode($post_data);

	          $user = $request->username;
              $pass = $request->password;
    
      $select_sql ="SELECT * FROM users WHERE username = '" . $user . "'  AND passwrd = password('" . $pass . "')";

       $result = $con->query($select_sql);
       $re=$result->fetch_assoc();
    echo json_encode($re);
       ?>