Angular JS Simple Register using Ui-Router

Getting Started

Please take a look at our previous tutorial, Angular JS Simple Login using Ui-Router, which tackles on how to create a simple login using Angular JS in Ui-Router since we're gonna be using it in this tutorial.

Creating Link for Register

First, let's create a link to our register form by adding the ff in the login.html of the previous login tutorial.
  1. <p><a ui-sref="register" style="text-decoration:none;">Register a new Account</a></p>

Creating our Registration Form

Next, we're gonna create our registration form by creating a new file in our root folder and name it as register.html
  1. <div class="col-sm-4 col-sm-offset-4">
  2. <div class="login-panel panel panel-primary">
  3. <div class="panel-heading">
  4. <h3 class="panel-title"><span class="glyphicon glyphicon-user"></span> Signup
  5. </h3>
  6. </div>
  7. <div class="panel-body">
  8. <form name="signupform" ng-submit="signup()">
  9. <div class="form-group">
  10. <input class="form-control" placeholder="Username" type="text" autofocus ng-model="user.username" required>
  11. </div>
  12. <div class="form-group">
  13. <input class="form-control" placeholder="Password" type="password" ng-model="user.password" required>
  14. </div>
  15. <div class="form-group">
  16. <input class="form-control" placeholder="Firstname" type="text" ng-model="user.firstname" required>
  17. </div>
  18. <div class="form-group">
  19. <input class="form-control" placeholder="Lastname" type="text" ng-model="user.lastname" required>
  20. </div>
  21. <div class="form-group">
  22. <input class="form-control" placeholder="Address" type="text" ng-model="user.address" required>
  23. </div>
  24. <p><a ui-sref="login" style="text-decoration:none;">I already have an account</a></p>
  25. <button type="submit" class="btn btn-lg btn-primary btn-block" ng-disabled="signupform.$invalid"><span class="glyphicon glyphicon-check"></span> Verify</button>
  26. </form>
  27. </div>
  28. </div>
  29.  
  30. <div class="alert alert-danger text-center" ng-show="errorSignup">
  31. <button type="button" class="close" ng-click="clearMsg()"><span aria-hidden="true">&times;</span></button>
  32. {{ errorMsg }}
  33. </div>
  34. <div class="alert alert-success text-center" ng-show="successSignup">
  35. <button type="button" class="close" ng-click="clearMsg()"><span aria-hidden="true">&times;</span></button>
  36. {{ successMsg }}
  37. </div>
  38. </div>

Adding "register" to our States

Next, in our app.js located in js folder, add the ff to our .config.
  1. .state('register', {
  2. url: '/register',
  3. templateUrl: 'register.html',
  4. controller: 'registerCtrl'
  5. })

Creating our Register Controller and Service

Next, we are going to create our controller and service. registerCtrl.js In js folder, open controllers folder and create a new file and name it as registerCtrl.js
  1. 'use strict';
  2.  
  3. app.controller('registerCtrl', function($scope, signupService, loginService, $location){
  4. //redirect back to home if user is logged in
  5. var connected = loginService.islogged();
  6. connected.then(function(response){
  7. if(response.data == 'true'){
  8. $location.path('/home');
  9. }
  10. });
  11.  
  12. $scope.errorSignup = false;
  13. $scope.successSignup = false;
  14.  
  15. $scope.signup = function(){
  16. signupService.signup($scope.user, $scope);
  17. }
  18.  
  19. $scope.clearMsg = function(){
  20. $scope.errorSignup = false;
  21. $scope.successSignup = false;
  22. }
  23. });
signupService.js In js folder, open services folder and create a new file and name it as signupService.js
  1. 'use strict';
  2.  
  3. app.factory('signupService', function($http, $location, sessionService){
  4. return{
  5. signup: function(user, $scope){
  6. var validate = $http.post('api/signup.php', user);
  7. validate.then(function(response){
  8. var uid = response.data.user;
  9. if(uid){
  10. sessionService.set('user',uid);
  11. $location.path('/home');
  12. }
  13.  
  14. else{
  15. $scope.successLogin = false;
  16. $scope.errorLogin = true;
  17. $scope.errorMsg = response.data.message;
  18. }
  19. });
  20. }
  21. }
  22. });

Adding Controller and Service to our App

Next, we add the controller and service that we created in our app by adding the ff. in index.html which is our main page.
  1. <script src="js/controllers/registerCtrl.js"></script>
  2. <script src="js/services/signupService.js"></script>

Creating our Register API

Lastly, we create our PHP register API by opening api folder and create a new file named signup.php.
  1. <?php
  2.  
  3.  
  4. $conn = new mysqli("localhost", "root", "", "angular");
  5.  
  6. if ($conn->connect_error) {
  7. die("Connection failed: " . $conn->connect_error);
  8. }
  9.  
  10. $out = array('error' => false);
  11.  
  12. $user = json_decode(file_get_contents('php://input'));
  13.  
  14. $username = $user->username;
  15. $password = $user->password;
  16. $firstname = $user->firstname;
  17. $lastname = $user->lastname;
  18. $address = $user->address;
  19.  
  20. $sql = "INSERT INTO members (username, password, firstname, lastname, address) VALUES ('$username', '$password', '$firstname', '$lastname', '$address')";
  21. $query = $conn->query($sql);
  22.  
  23. if($query){
  24. $nid = $conn->insert_id;
  25. $out['user'] = uniqid('ang_');
  26. $_SESSION['user'] = $nid;
  27. }
  28. else{
  29. $out['error'] = true;
  30. $out['message'] = 'Signup Failed';
  31. }
  32.  
  33. echo json_encode($out);
  34.  
  35. ?>
That ends this tutorial. Happy Coding :)

Add new comment