2017-09-06 1 views
0

그래서 자바와 MySQL을 백엔드로, AngularJS, HTML을 프론트 엔드로하여 로그인 기능을 만들고 있습니다. 유효한 전자 메일과 암호를 입력하면 올바르게 로그인되지만 잘못된 전자 메일과 암호를 입력하면 로그인하게됩니다. 이 문제를 해결하는 방법을 모르겠습니다. User 객체를 반환하는 대신 부울을 반환하는 사용자 서비스 클래스를 생각하고있었습니다. 그런 다음 angularJS에서 post 메서드를 호출하면 var res가 true인지 false인지 확인할 수 있습니다. 나는 다른 방법이나 더 좋은 방법백엔드로 자바 휴식 api를 가진 Angular JS 로그인

사용자 서비스

// Checking if the user can login successfully 
    public User login(User user) 
    { 
     try 
     { 
      User dbUser = new User(); 

      String query = "SELECT * FROM user WHERE email=?"; 
      stmt = conn.prepareStatement(query); 
      stmt.setString(1, user.getEmail()); 

      ResultSet rs = stmt.executeQuery(); 
      rs.next(); 

      dbUser.setId(rs.getInt("id")); 
      dbUser.setFirstName(rs.getString("firstname")); 
      dbUser.setLastName(rs.getString("lastname")); 
      dbUser.setEmail(rs.getString("email")); 
      dbUser.setPassword(rs.getString("password")); 
      dbUser.setPhoneNum(rs.getString("phonenum")); 

      if(user.getEmail().equals(dbUser.getEmail()) && 
        user.getPassword().equals(dbUser.getPassword())) 
      { 
       System.out.println("Successfully Logged-In"); 
      } 
      else 
      { 
       System.out.println("Failed To Log-In"); 
       dbUser = null; 
      } 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 

     return user; 
    } 

사용자 자원

@Path("/login") 
    @POST 
    @Consumes(MediaType.APPLICATION_JSON) 
    @Produces(MediaType.APPLICATION_JSON) 
    public void login(User user) 
    { 
     userService.login(user); 

    } 

login.js가 있는지 확실하지 않다

var app = angular.module("loginApp", []); 

app.controller("loginController", function($scope, $http) { 

    // POST METHOD.... Checking if the login is successfull 
    $scope.checkLogin = function() { 
     // retrieving the data from the login form 
     var dataObj = { 
       email: $scope.email, 
       password: $scope.password 
     }; 

     if($scope.email.length === 0 || typeof $scope.email === 'undefined' 
     && $scope.password.length === 0 || typeof $scope.password === 'undefined') { 
      console.log("pls enter something"); 
     }else { 
      console.log("something entered"); 
      var res = $http.post("http://localhost:8080/quiz/webapi/account/login", dataObj).then(successCallback, errorCallback); 
     } 

     function successCallback(res){ 
      console.log("Successfully Connected to Login API"); 
      console.log("Redirecting to dashboard page"); 
      window.location = "http://localhost:8080/quiz_webclient/dashboard.html"; 
     } 
     function errorCallback(error){ 
      console.log("Faile To Connect to Login API"); 
     } 
    }; 

    $scope.cancelLogin = function() { 
     console.log("Redirecting to home page"); 
     window.location = "http://localhost:8080/quiz_webclient/homepage.html"; 
    } 
}); 

login.html

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Login</title> 
<script src="lib/angular.js"></script> 
<script src="js/login.js"></script> 
<link rel="stylesheet" href="cs/login.css"> 
<!-- Latest compiled and minified bootstrap CSS --> 
<link rel="stylesheet" 
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 
    integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" 
    crossorigin="anonymous"> 
</head> 
<body> 
    <h3>Login</h3> 
    <div ng-app="loginApp" ng-controller="loginController"> 
     <form class="loginForm"> 
      <label id="label">Email</label> <br> 
      <input type="text" name="email" ng-model="email" required> <br> 
      <label id="passwordlabel">Password</label> <br> 
      <input type="password" name="password" ng-model="password" required> <br> 

      <button type="submit" ng-click="checkLogin()">Login</button> <br> 
      <button type="submit" ng-click="cancelLogin()">Cancel</button> 
     </form> 
    </div> 
</body> 
</html> 
(예를 들어) 모델 이름에
+0

출력이란 무엇입니까? 실패 할 때'성공적으로 로그인 했습니까? ' – Isaac

+0

아니요 로그인에 실패했지만 계속 실패하면 로그인하지 못합니다. – bubbles2189

답변

0

사용 도트 POST 작업에 대한 또한

<input type="text" ng-model="loginData.email" autofocus required> 

, - 당신이 사용하는 각의 버전을 알고 도움이 될 것이다. 그러나 Angular 버전 < 1.6을 사용하는 경우 .success() 및 .error() 메서드를 .then 대신 직접 사용 해보겠습니다. 다음과 같은 것 :

$scope.checkLogin = function() { 
     $http.post("http://localhost:8080/quiz/webapi/account/login", $scope.loginData) 
      .success(function(data) { 
       window.location = "http://localhost:8080/quiz_webclient/dashboard.html"; 
      }) 
      .error(function(data) { 
       console.log('Error: ' + data); 
      }); 
    }; 

거기에 무슨 일이 일어나고 있는지에 관해서는 Java에 대해 충분히 알지 못합니다. 대신 Node.js를 사용해보십시오. 인증 메소드가 사용자 또는 부울 대신 토큰을 리턴 할 수 있습니다.

관련 문제