2017-01-17 4 views
0

그래서 내 앱의 페이스 북 로그인 작업을하고 있으며 여기에 문제가 있습니다. 어떻게해야합니까?이전에 실패한 경우에만 기능 실행

(1) - 페이스 북에서 인증 (DONE) 2 - 페이스 북에서 도시 탐색 구조 데이터를 검색 (DONE) 3 - 그는 이메일 및 FB 토큰을 사용하여 로그인합니다 않는 경우 - 사용자가 를 존재하는지 확인합니다. - 사용자가 로그인하지 않으면 시스템에서 데이터를 등록하고 자동으로 로그인합니다.

문제는 다음과 같습니다. 여기에 인증 방법과 인증 방법이 있습니다. 내가해야 할 일은 무엇입니까? 호출 인증, 오류가 발생하면 인증 및 등록을 호출해야합니다. 어떻게해야합니까? AngularJS와 에서 NodeJS

에서

등록

api.register= function(req, res) { 
    if (req.body.password!==null) { 
      var hash = bcrypt.hashSync(req.body.password, bcrypt.genSaltSync(9)); 
      req.body.password= hash; 
      console.log('Default Login'); 
    } 
    if(req.body.tokenFB!==null){ 
     var tokenHash = bcrypt.hashSync(req.body.tokenFB, bcrypt.genSaltSync(9)); 
     req.body.tokenFB = tokenHash; 
     console.log('Login with facebook'); 
    } 
    userModel.create(req.body) 
     .then(function(user){ 
      res.json(user); 
      console.log('We have a new user! =)'); 
     }, function(error){ 
      res.status(500).json(error); 
     }); 
}; 

인증합니다

api.authenticate= function(req, res) { 
     var email = req.body.email; 
     console.log(req.body); 
     userModel.findOne({"email":email}) 
     .then(function(user){ 
      if (user == null) { 
       res.status(404).json(); 
      } 
      else { 
       if (req.body.password!==null && bcrypt.compareSync(req.body.password, user.password)) { 
        //user found 
        console.log('default user') 
        var token = jwt.sign({email:user.email}, app.get('secret'), { 
         expiresIn: 84600 
        }); 

        res.set('x-access-token', token); 
        res.json(user); 
       } else if (req.body.tokenFB!==null && bcrypt.compareSync(req.body.tokenFB, user.tokenFB)){ 
        console.log('facebook user') 
        var token = jwt.sign({email:user.email}, app.get('secret'), { 
         expiresIn: 84600 
        }); 

        res.set('x-access-token', token); 
        res.json(user); 

       } 
       else { 
        res.status(404).json(); 
       } 
      } 
     }, function(error){ 
      res.status(404).json(error); 
     }); 
}; 

페이스 북 로그인 NodeJS에서는 만약 부분은 분명히 내가 그렇게하려고했던 사촌의 존재, 작동하지 않고 무엇을 보여 I 뭘하려고합니까?

$scope.FBLogin = function(){ 
    firebase.auth().signInWithPopup(provider).then(function(result) { 

    var token = result.credential.accessToken; 
    var user = result.user; 

    $http.get('https://graph.facebook.com/v2.5/me? access_token='+token+'&fields=id,name,first_name,last_name,email') 
    .success(function(jsonService){ 
     $scope.user.firstName= jsonService.first_name; 
     $scope.user.lastName = jsonService.last_name 
     $scope.user.email = jsonService.email; 
     $scope.user.password=null; 
     $scope.user.tokenFB=token; 
     $scope.user=true; 
     console.log($scope.user); 

     if($scope.authenticate()){ 
     console.log("Logged in with Facebook! :)") 
     } else { 
     $scope.checkingUser=false; 
     $scope.Register(); 
     $scope.Authenticate(); 
     }; 
    }); 

    }).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    // The email of the user's account used. 
    var email = error.email; 
    // The firebase.auth.AuthCredential type that was used. 
    var credential = error.credential; 
    // ... 
    }); 
}; 

아무도 도와 줄 수 없나요?

+0

$ scope.authenticate() 코드도 게시 할 수 있습니다. –

+0

감사합니다. @BalaAbhinav, 내 문제는 이미 해결했습니다. 하지만 코드는 제 대답에 있습니다! :) –

답변

0

그래서 ... 나는 약속을 사용하여 내 문제를 해결했습니다. 내가 한 일이 여기에있다. AngularJS와의 AngularJS와의 NodeJS

에서

등록

api.register = function(req, res) { 
    if (req.body.password!==null) { 
      var hash = bcrypt.hashSync(req.body.password, bcrypt.genSaltSync(9)); 
      req.body.password = hash; 
      console.log('Default Login'); 
    } 
    if (req.body.idFb!==null) { 
      var idFbHash = bcrypt.hashSync(req.body.idFb, bcrypt.genSaltSync(9)); 
      req.body.idFb = idFbHash; 
      console.log('Login with FB'); 
    } 
    userModel.create(req.body) 
     .then(function(user){ 

      console.log('We have a new account!'); 
      return res.json(user); 
     }, function(error){ 
      return res.status(500).json(error); 
     }); 
}; 

인증합니다 NodeJS에서

api.authenticate = function(req, res) { 
     var email = req.body.email; 
     userModel.findOne({"email":email}) 
     .then(function(user){ 
      console.log(user); 
      if (user == null) { 
       console.log('The user does not exist') 
       return res.status(404).json(); 
      } 
      else { 
       console.log('User found') 
       if (req.body.password!==null && bcrypt.compareSync(req.body.password, user.password)){ 
        //user found 
        console.log('Default user') 
        var token = jwt.sign({email:user.email}, app.get('secret'), { 
         expiresIn: 84600 
        }); 
        res.set('x-access-token', token); 
        return res.json(user); 


       } else if (req.body.tokenFB!==null && bcrypt.compareSync(req.body.idFb, user.idFb)){ 
        console.log('Facebook user') 
        var token = jwt.sign({email:user.email}, app.get('secret'), { 
         expiresIn: 84600 
        }); 

        res.set('x-access-token', token); 
        return res.json(user); 


       } 
       else { 
        return res.status(404).json(); 
       } 
      } 
     }, function(error){ 
      return res.status(404).json(error); 
     }); 
}; 

하여 인증은

const authenticate = function ($scope){ 
return new Promise((resolve, reject) => { 
    if ($scope.user.email == null) { 
    console.log('Email was not received'); 
    var alertPopup = $ionicPopup.alert({ 
     title: 'We could not log you in', 
     template: 'Please, verify your data!' 
    }); 
    $scope.user.password = ""; 

    // "Return" 
    reject(false); 
    } else if($scope.user.tokenFB == null && $scope.user.password == null){ 
    console.log("No password or Facebook token") 
    var alertPopup = $ionicPopup.alert({ 
     title: 'We could not log you in', 
     template: 'Please, verify your data!' 
    }); 

    // "Return" 
    reject(false) 
    } else { 
    $http.post(serveradress.url+'/authenticate/', $scope.user).success(function(user){ 


     $localStorage.user=user; 
     //$scope.isLoggedIn=true; 
     $scope.user = user; 
     $scope.user.password = ""; 
     UserService.setUser(user); 
     window.location = '#/timeline'; 
     $scope.hide(); 
     //teste aidax 
     ax.ready(function() { 
     ax.toggle_debug(); 
     }); 
     ax.goal('login', true, false, { 
     email: user.email 
     }); 
     ax.identify(user.email); 



     resolve(true) 
    }).error(function(erro){ 

     if($scope.checkingUser){ 
     console.log("User does not exist") 
     } else { 
     $scope.hide(); 
     console.log("tá entrando aqui!") 
     var alertPopup = $ionicPopup.alert({ 
      title: 'We could not log you in', 
      template: 'Please, verify your data!' 
     }); 
     console.log(erro); 
     } 

     // "Return" 
     reject(false); 
    }); 
    } 
}) 
}; 

페이스 북 로그인

$scope.FBLogin = function(){ 
firebase.auth().signInWithPopup(provider).then(function(result) { 
    $scope.user.tokenFB = result.credential.accessToken; 
    $scope.user.password = null; 
    $scope.user.email = result.user.email; 
    $scope.user.idFb = result.user.providerData[0].uid; 
    $scope.checkingUser=true; 



authenticate($scope).then(function(userExists){ 
    console.log("Welcome facebook user!") 
}) 
.catch(function(failure){ 
    console.log("Ops, you don't have an account yet, let me solve this!") 
    RegisterFB($scope).then(function(userRegistered){ 

    $scope.checkingUser=false; 
    console.log("Done, now you have an account!"); 
    authenticate($scope).then(function(userExists){ 
    console.log("Welcome facebook user!") 
}) 
    }) 
    .catch(function(failure){ 
    console.log("Sorry, an error occured") 
    console.log(failure) 

    }) 
}); 
관련 문제