2

위로 Parse를 사용하면 SDK가 세션 데이터를 로컬에 저장하고 사용자가 페이지를 새로 고친 후 다시 로그인 할 필요가없는 것처럼 보였습니다. 앱). Firebase/Angularfire의 경우에는 그렇지 않습니다. 매번 웹 페이지를 새로 고침 할 때마다 인증 데이터가 많이 수집됩니다. 이것은 Firebase의 멋진 사람들이 구현하지 않은 것에 대해 놀랄 것입니다. 내가 놓친 게 있니?Ionic app/페이지를 다시로드 한 후 Firebase 자동 로그인

완전성을 위해;

// ASG june 2016 - Upgrade firebase SDK 
firebase.initializeApp(FirebaseConfig); 

// login as anonymous if not already logged in 
var currentUser = $firebaseAuth().$getAuth(); 
if (currentUser) { 
    console.log("Signed in as:", currentUser); 
} else { 
    console.log("Not logged in; going to log in as anonymous"); 
    $firebaseAuth().$signInAnonymously().then(function(authData) { 
     console.log("Signed in anonymously as:", authData.uid); 
    }).catch(function(error) { 
     console.error("Anonymous authentication failed:", error); 
    }); 
} 

// register the on auth callback 
$firebaseAuth().$onAuthStateChanged(function(authData) { 
    if (authData) { 
     console.log("Logged in as:", authData.uid); 
     if(typeof($rootScope.userProfile) == "undefined"){ 
      $rootScope.userProfile = FirebaseProfileService.getUserProfile(authData.uid, false); 
     } 
    } 
}); 

답변

1

인증 토큰은 페이지/앱 재로드간에 유지됩니다. 하지만 페이지를 새로 고침 할 때 새로운 새로 고침 토큰을 얻어야 할 필요가 있습니다. Firebase 서버로 왕복해야합니다. 시간이 걸리므로 초기 getAuth()은 진행중인 동안 null을 반환 할 수 있습니다.

var currentUser; 

$firebaseAuth().$onAuthStateChanged(function(authData) { 
    if (authData) { 
     console.log("Logged in as:", authData.uid); 
     currentUser = authData.currentUser; 
     if(typeof($rootScope.userProfile) == "undefined"){ 
      $rootScope.userProfile = FirebaseProfileService.getUserProfile(authData.uid, false); 
     } 
    } 
    else { 
     console.log("Not logged in; going to log in as anonymous"); 
     currentUser = null; 
     $firebaseAuth().$signInAnonymously().catch(function(error) { 
      console.error("Anonymous authentication failed:", error); 
     }); 
    } 
}); 
2

중포 기지가 절대적으로 authData을 presists하고 거의 다른 곳에서 찾을 수없는 장점을 많이 가지고 : 여기 app.run 내 코드는()입니다. 따라서 onAuthStateChanged 수신기에서 authData를 쉽게 가져올 수 있으므로 여기에 getAuth()를 호출하는 이유가 없습니다. 이 경우 getAuth()를 사용하여 currentUser를 가져 오는 작업을 제거하고 ELSE 조건을 onAuthStateChanged로 옮겨야 할 수도 있습니다. AuthData가 발견되지 않아 사용자를 익명으로 등록하려는 경우, 나는 희망. 희망 그 작동합니다.

+0

getAuth를 사용하지 않고 현재 로그인 한 사용자에 대한 데이터를 얻으려면 어떻게해야합니까? 사용자가 로그인했는지 여부를 확인하고 싶은 다른 주에서는? – rex

+0

나는 사용자가 이미 로그인되어 있고이 데이터가 지속되는 경우 getAuth가 null을 반환하는 이유를 조금 혼란스러워합니다. – rex

+0

필자는 app.run()에 넣기 때문에 가끔은 경쟁 조건으로 이어지기 때문에 다른 컨트롤러와 서비스에 의해 태클 될 다른 상태에서는 괜찮을 것이라고 생각합니다. 다시, 나는 단지 생각하고 여기에 아무것도 내 측면에서 테스트입니다 추측입니다. 그러나 시도하고 알려주세요. –

관련 문제