Creating A Registration And Login Using AngularJS - Part 2

In this tutorial we will create a login form using AngularJS. Last time in my previous tutorial Creating A Registration And Login Using AngularJS - Part 1 we already created a sign up form. This time we will continue on the project that we left behind, and we will add a login code to make this application complete. So let's start coding.. Creating the login form This is where the user will input their account for login. To do that open any kind of text editor, then copy/paste the code below name it "index.php"
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3.         <head>
  4.                 <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
  5.                 <script src = "js/angular.js"></script>
  6.                 <script src = "js/loginapp.js"></script>
  7.         </head>
  8. <body ng-app = "myModule" ng-controller = "myController">
  9.         <nav class = "navbar navbar-default">
  10.                 <div class = "container-fluid">
  11.                         <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class = "row">
  15.                 <div class = "col-md-4"></div>
  16.                 <div class = "col-md-4 well">
  17.                         <form ng-submit = "loginForm()">
  18.                         <h3 class  = "text-primary">Creating A Registration And Login Using AngularJS - Part 2</h3>
  19.                         <hr style = "border-top:1px dotted #000;"/>
  20.                         <h3>Member Login</h3><a href =  "signup.php" class = "pull-right">Sign up</a>
  21.                         <br />
  22.                         <div class = "form-group">
  23.                                 <label>Username</label>
  24.                                 <input type = "text" class = "form-control" ng-model = "username" ng-required = "true"/>
  25.                         </div>
  26.                         <div class = "form-group">
  27.                                 <label>Password</label>
  28.                                 <input type = "password" class = "form-control" ng-model = "password" ng-required = "true"/>
  29.                         </div>
  30.                         <center><div class = "text-danger" ng-model = "result">{{result}}</div></center>
  31.                         <br />
  32.                         <div class = "form-group">
  33.                                 <button class = "btn btn-primary form-control"><span class = "glyphicon glyphicon-log-in"></span> Login</button>
  34.                         </div>
  35.                         </form>
  36.                 </div>
  37.         </div>
  38. </body>
  39. </html>
The code above will generate the request of the user if the account is correct. The Login Script This is where the data of an user will be processed. Copy/paste the code below then name it "loginapp.js"
  1. 'use strict';
  2.  
  3. var app = angular.module("myModule", [])
  4.                                                             .controller("myController", function($scope, $http){
  5.                                                                         $scope.loginForm = function(){
  6.                                                                                 $scope.result = "";
  7.                                                                                 var request = $http({
  8.                                                                                         method: "POST",
  9.                                                                                         url: "login_query.php",
  10.                                                                                         data: {
  11.                                                                                                 username : $scope.username,
  12.                                                                                                 password: $scope.password
  13.                                                                                         },
  14.                                                                                         headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  15.                                                                                 });
  16.                                                                                 request.then(function(response){
  17.                                                                                         console.log(response);
  18.                                                                                         if(response.data == "true"){
  19.                                                                                                 $scope.username = "";
  20.                                                                                                 $scope.password = "";
  21.                                                                                                 window.location = "login.php";
  22.                                                                                         }else{
  23.                                                                                                 $scope.result = "Invalid username or Password";
  24.                                                                                         }
  25.                                                                                 });
  26.                                                                         };
  27.                                                                  });
The code above will processed the value of an input, then will be send to a php server. The Login Query This is where the data will be check if the user is already a member. To do that copy/paste the code below then name it "login_query.php"
  1. <?php
  2.         $conn = new mysqli("localhost", "root", "", "db_member") or die(mysqli_error());
  3.         $postdata = file_get_contents("php://input");
  4.     $request = json_decode($postdata);
  5.         $username = $request->username;
  6.         $password = $request->password;
  7.         $query = $conn->query("SELECT * FROM `member` WHERE `username` = '$username' && `password` = '$password'") or die(mysqli_error());
  8.         $valid = $query->num_rows;
  9.         $fetch = $query->fetch_array();
  10.         if($valid > 0){
  11.                 session_start();
  12.                 $_SESSION['mem_id'] = $fetch['mem_id'];
  13.                 echo "true";
  14.         }else{
  15.                 echo "false";
  16.         }
  17. ?>
The code above will receive the data from the angularjs directives, then will be decoded by json script and store the value in the variable simultaneously. After that it will check by the SQL query to check if the user is already a member. Creating a Home Form This is where the user will go after login. So copy/paste the code below then name it "login.php"
  1. <!DOCTYPE html>
  2. <?php
  3.         session_start();
  4.         if(!ISSET($_SESSION['mem_id'])){
  5.                 header("location:index.php");
  6.         }
  7. ?>
  8. <html lang = "en">
  9.         <head>
  10.                 <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
  11.         </head>
  12.         <body>
  13.                 <nav class = "navbar navbar-default">
  14.                         <div class = "container-fluid">
  15.                                 <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
  16.                         </div>
  17.                 </nav>
  18.                 <div class = "row">
  19.                         <div class = "col-md-4"></div>
  20.                         <div class = "col-md-4 well">
  21.                                 <h3 class  = "text-primary">Creating A Registration And Login Using AngularJS - Part 2</h3>
  22.                                 <hr style = "border-top:1px dotted #000;"/>
  23.                                 <br /><br />
  24.                                 <?php
  25.                                         $conn = new mysqli("localhost", "root", "", "db_member") or die(mysqli_error());
  26.                                         $query = $conn->query("SELECT * FROM `member` WHERE `mem_id` = '$_SESSION[mem_id]'") or die(mysqli_error());
  27.                                         $fetch = $query->fetch_array();
  28.                                 ?>
  29.                                 <center><h3 class = "text-info">Welcome: </h3><h3><?php echo $fetch['firstname']." ".$fetch['lastname'] ?></h3></center>
  30.                                 <br />
  31.                                 <a class = "btn btn-danger" href = "logout.php"><span class = "glyphicon glyphicon-log-out"></span> Logout</a>
  32.                         </div>
  33.                 </div>
  34.         </body>
  35. </html>
The Logout Script This is where all the session of user will be dispose to log out properly. To do that copy/paste the code below and name it "logout.php"
  1. <?php
  2.         session_start();
  3.         session_unset(mem_id);
  4.         header("location:index.php");
  5. ?>
There you have we created a login form using AngularJS. I hope that this tutorial help you to your projects. For more tutorials and updates just kindly visit this site. Enjoy Coding!!
Tags

Add new comment