Skip to content

IT-Blogs.com

Find your IT solution here
Menu
  • Dashboard
    • JBoss DashBuilder
  • Development
    • Node.js – Restful with Express framework + MySQL
    • Spring MVC + Hibernate with AngularJS
    • Spring Restful (JSON) + Hibernate with AngularJS
  • Mobile
    • IONIC with Restful API
  • NoSQL
    • Cloud MongoDB Stitch + CRUD with JS
  • Search Engine
    • Solr
November 23, 2017
HomeMobileIONIC with Restful APIExample 5: Client Login / Logout With $http.post() No ratings yet.

Example 5: Client Login / Logout With $http.post()

By Kelvin Hui IONIC with Restful API  0 Comments

This example will show you how to post data to Restful server by using $http.post() in order to demonstrate login and logout functions. Besides, it will use window.localStorage to store the login information. Please follow below steps:

  1. Update C:\workspace_ionic\itBlogsApp\www\templates\menu.html by changing:

<ion-item menu-close ng-click=”login()”>Login</ion-item>

to

<ion-item menu-close ng-click=”login()”>{{loginout}}</ion-item>

 

  1. Update C:\workspace_ionic\itBlogsApp\www\templates\login.html by changing:

<h1 class=”title”>Login</h1>

to

<h1 class=”title”>Login <font color=”red”>{{msg}}</font></h1>

 

  1. Update C:\workspace_ionic\itBlogsApp\www\js\controllers.js by changing ‘AppCtrl’ as:

.controller(‘AppCtrl’, function($scope, $http, $ionicModal, $timeout) {

  // With the new view caching in Ionic, Controllers are only called

  // when they are recreated or on app start, instead of every page change.

  // To listen for when this page is active (for example, to refresh data),

  // listen for the $ionicView.enter event:

  //$scope.$on(‘$ionicView.enter’, function(e) {

  //});

  // Form data for the login modal

  $scope.loginData = {};

  // Create the login modal that we will use later

  $ionicModal.fromTemplateUrl(‘templates/login.html’, { scope: $scope }).then(function(modal) {

                                $scope.modal = modal;

  });

  // Triggered in the login modal to close it

  $scope.closeLogin = function() {

                                $scope.modal.hide();

  };

  // Open the login modal

  $scope.login = function() {

                                if(window.localStorage.getItem(“login”) !== null) {

                                                $scope.doLogout();

                                } else {

                                                $scope.msg = ”;

                                                $scope.loginData.username = ”;

                                                $scope.loginData.password = ”;

                                                $scope.modal.show();

                                }

  };

  // Perform the login action when the user submits the login form

  $scope.doLogin = function() {

                                console.log(‘Doing login’, $scope.loginData);

                                // send login data

                                $http({

                                                method: ‘POST’,

                                                url: ‘http://localhost:8080/SpringRestfulExample/customer/login’,

                                                data: ‘username=’+$scope.loginData.username+’&password=’+$scope.loginData.password,

                                                headers: {‘Content-Type’: ‘application/x-www-form-urlencoded’}

                                }).success(function (data, status, headers, config) {

                                                // handle success things

                                                console.log(‘AppCtrl doLogin success…’, status);

                                                $scope.msg = ‘Successful’;

                                                //setting session here

                                                window.localStorage.setItem(“login”, $scope.loginData.username);

                                                $scope.loginout = “Logout”

                                                $scope.closeLogin();

                                }).error(function (data, status, headers, config) {

                                                // handle error things

                                                console.log(‘AppCtrl doLogin error…’, status);

                                                $scope.msg = ‘Failed’;

                                });

  };

  // Perform the logout action

  $scope.doLogout = function() {

                                console.log(‘Doing logout’);

                                $scope.loginout = “Login”

                                window.localStorage.removeItem(“login”);

  };

                //Setting default one

                if(window.localStorage.getItem(“login”) !== null) {

                                $scope.loginout = “Logout”

                } else {

                                $scope.loginout = “Login”

                }

})

 

Done, let’s try to refresh the browser, and click on “Login” menu:

 

It will show you the login popup page:

 

Just simply click on “Log in” button, it will show you “Login Failed”:

 

Let’s try entering “peterpan” as username and “peterpan” as password, and click on “Log in” button again, it will make $http.post request to the Restful server, and return with status=200. window.localStorage will set an item “login” with username. Then, the login popup page will be hide, please check again the menu:

 

It will show you “Logout” instead of “Login” now. Let’s try to logout, and see what happen, window.localStorage will remove the item “login” and the menu will resume to:

 

Cong!!! It’s done.

Please rate this

Tags:$http.post(), Client Login, Client Logout, window.localStorage

Related Posts

Example 3: Products Show Room With Searching and Sorting

Example 1: Show Company Background With Simple $http.get()

Example 4: Showing Promotion With QR Code and Push Notification

About Author

Kelvin Hui

Recent Posts

  • noSQL Search Query
  • Bulk Operations
  • JS GUI for Simple CRUD
  • How to create application, database and collection?
  • Why MongoDB?
  • Example 5: Client Login / Logout With $http.post()
  • Example 4: Showing Promotion With QR Code and Push Notification
  • Example 3: Products Show Room With Searching and Sorting
  • Example 2: Listing Shop Integrating With Google Map
  • Example 1: Show Company Background With Simple $http.get()

IT-Blogs.com Copyright © 2022.
Back to Top ↑