AngularJS Login with PHP/MySQLi

Getting Started

I've used CDN for Bootstrap, Angular JS and Angular Route so you need internet connection for them to work.

Creating our Database

First, we are going to create our database and insert sample users to test our app. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as angular. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `members` (
  2.   `memid` int(11) NOT NULL AUTO_INCREMENT,
  3.   `username` varchar(30) NOT NULL,
  4.   `password` varchar(30) NOT NULL,
  5. PRIMARY KEY(`memid`)
  1. INSERT INTO `members` (`memid`, `username`, `password`) VALUES
  2. (1, 'neovic', 'devierte'),
  3. (2, 'julyn', 'divinagracia'),
  4. (3, 'gemalyn', 'cepe');
database sql

index.html

This is the main page of our app.
  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3.         <title>AngularJS Login with PHP/MySQLi</title>
  4.         <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  5.        
  6. </head>
  7. <div class="container">
  8.   <h1 class="page-header text-center">AngularJS Login with PHP/MySQLi</h1>
  9.   <div ng-view></div>
  10. </div>
  11. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  12. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.min.js"></script>
  13. <script src="js/angular.js"></script>
  14. <script src="js/controllers/loginCtrl.js"></script>
  15. <script src="js/services/loginService.js"></script>
  16. </body>
  17. </html>

login.html

This contains our login form.
  1. <div class="col-md-4 col-md-offset-4">
  2.     <div class="login-panel panel panel-primary">
  3.         <div class="panel-heading">
  4.             <h3 class="panel-title"><span class="glyphicon glyphicon-lock"></span> Login
  5.             </h3>
  6.         </div>
  7.         <div class="panel-body">
  8.                 <form role="form" name="logform">
  9.                 <fieldset>
  10.                         <div class="form-group">
  11.                         <input class="form-control" placeholder="Username" type="text" autofocus ng-model="user.username" required>
  12.                         </div>
  13.                         <div class="form-group">
  14.                         <input class="form-control" placeholder="Password" type="password" ng-model="user.password" required>
  15.                         </div>
  16.                         <button type="button" id="loginbutton" class="btn btn-lg btn-primary btn-block" ng-disabled="logform.$invalid" ng-click="login(user)"><span class="glyphicon glyphicon-log-in"></span> <span id="logtext">Login</span></button>
  17.                 </fieldset>
  18.                 </form>
  19.         </div>
  20.     </div>
  21.  
  22.     <div class="alert alert-danger text-center" ng-show="errorLogin">
  23.         <button type="button" class="close" ng-click="clearMsg()"><span aria-hidden="true">&times;</span></button>
  24.         {{ errorMsg }}
  25.     </div>
  26.  
  27.     <div class="alert alert-success text-center" ng-show="successLogin">
  28.         <button type="button" class="close" ng-click="clearMsg()"><span aria-hidden="true">&times;</span></button>
  29.         {{ successMsg }}
  30.     </div>
  31. </div>

angular.js

This is the main angular js script.
  1. var app = angular.module('app', ['ngRoute']);
  2.  
  3. app.config(function($routeProvider){
  4.         $routeProvider
  5.         .when('/', {
  6.                 templateUrl: 'login.html',
  7.                 controller: 'loginCtrl'
  8.         })
  9.         .when('/home', {
  10.                 templateUrl: 'home.html'
  11.         })
  12.         .otherwise({
  13.                 redirectTo: '/'
  14.         });
  15. });

loginCtrl.js

This is the controller for our login.html.
  1. 'use strict';
  2.  
  3. app.controller('loginCtrl', function($scope, loginService){
  4.         $scope.errorLogin = false;
  5.         $scope.successLogin = false;
  6.  
  7.         $scope.login = function(user){
  8.                 loginService.login(user, $scope);
  9.         }
  10.  
  11.         $scope.clearMsg = function(){
  12.                 $scope.errorLogin = false;
  13.                 $scope.successLogin = false;
  14.         }
  15. });

loginService.js

This is our service that sends post request to our PHP API for login.
  1. 'use strict';
  2.  
  3. app.factory('loginService', function($http){
  4.         return{
  5.                 login: function(user, $scope){
  6.                         var validate = $http.post('login.php', user);
  7.                         validate.then(function(response){
  8.                                 if(response.data.error == true){
  9.                                         $scope.successLogin = false;
  10.                                         $scope.errorLogin = true;
  11.                                         $scope.errorMsg = response.data.message;
  12.                                 }
  13.                                 else{
  14.                                         $scope.errorLogin = false;
  15.                                         $scope.successLogin = true;
  16.                                         $scope.successMsg = response.data.message;
  17.                                 }
  18.                         });
  19.                 }
  20.         }
  21. });

login.php

Lastly, this is our PHP api/code in checking our login.
  1. <?php
  2.        
  3. $conn = new mysqli("localhost", "root", "", "angular");
  4.  
  5. if ($conn->connect_error) {
  6.     die("Connection failed: " . $conn->connect_error);
  7. }
  8.  
  9. $out = array('error' => false);
  10.  
  11. $user = json_decode(file_get_contents('php://input'));
  12.  
  13. $username = $user->username;
  14. $password = $user->password;
  15.  
  16. $sql = "SELECT * FROM members WHERE username='$username' AND password='$password'";
  17. $query = $conn->query($sql);
  18.  
  19. if($query->num_rows>0){
  20.         $out['message'] = 'Login Successful';
  21. }
  22. else{
  23.         $out['error'] = true;
  24.         $out['message'] = 'Invalid Login';
  25. }
  26.  
  27. echo json_encode($out);
  28.  
  29. ?>
That ends this tutorial. Happy Coding :)

Add new comment