Auth 시스템과 함께 새로운 Firebase를 사용하고 $routeProvider
에있는 경로를 resolves
으로 제한하려고합니다.새로운 Firebase Auth를 사용하여 AngularJS 라우트 및 해결
그러나 나는 꽤 이해하지 못합니다.
이것은 내가 가지고있는 것입니다. 내 .config 함수에서 경로를 정의하고 firebase를 초기화하려고합니다. 다음은 나의 설정 블록에있다.
$routeProvider
.when('/', {
templateUrl: 'templates/dashboard.ejs',
//resolve. this needs restricted
})
.when('/login', {
templateUrl: 'templates/login.ejs'
})
firebase.initializeApp(config);
나는 각도 내 .run()
블록에 나와있는 이러한 기능을 사용할 수 있는지 새로운 문서 사이트에서 발견했다.
.run(function($rootScope, $location) {
$rootScope.authentication = firebase.auth();
/*firebase.auth().onAuthStateChanged(function(user) {
if(user) {
$rootScope.user = user;
} else {
$rootScope.user = false;
$location.path('/login')
}
})*/
var user = firebase.auth().currentUser;
if (user) {
$rootScope.user = user;
} else {
$rootScope.user = false;
console.log('No User!');
$location.path('/login');
}
})
이제 내가 무엇을 위의 단지 내 사이트의 모든 URL에 액세스 every other time
을 발사한다.
내 질문은 어떻게 내 .run() 함수에있는 걸릴 및 경로를 다시 제한 할 수 있도록 내 routeProvider에 대한 해결 방법으로 만들 수 있습니다.
이전 firebase에서는 아래와 같이 $ firebaseAuth()를 호출하고 다음과 같이 호출 된 routeChangeError 함수가 있습니다.
// In config block. **old way**
$routeProvider
.when('/', {
templateUrl: 'templates/dashboard.ejs',
resolve: {
"currentAuth": ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase(fbUrl);
var authObj = $firebaseAuth(ref);
return authObj.$requireAuth();
}]
}
})
그리고 나서 .run() 블록의 routechangeerror.
// .run block **old way**
.run(function($rootScope, $location) {
$rootScope.$on("$routeChangeError", function(event, current, previous, eventObj) {
if (eventObj === 'AUTH_REQUIRED') {
console.log('auth required!');
$location.path("/login");
}
});
})
더 이상 $firebaseAuth()
를 사용하지 않기 때문에이 더 이상 작동하지 않습니다. 또는 new Firebase(fbUrl);
도 마찬가지입니다.
UPDATE
내가 조금 지저분하지만 경로를 조사하는 것은 조금 더, 나는이 함께했다처럼 느낀다. 내 경로 해결에
:
.when('/', {
templateUrl: 'templates/dashboard.ejs',
resolve: {
userAuthenticated: ["$http", "$q", function($http, $q) {
var deferred = $q;
if(firebase.auth().currentUser) {
deferred.resolve();
} else {
deferred.reject();
console.log('Really!?');
}
return deferred.promise;
}]
}
})
그리고 간단한 routeChangeError
. 는$rootScope.$on("$routeChangeError", function (event, current, previous, rejection) {
alert("Not authorised");
})
유일한 문제는 deferred.promise가 정의되지 않은 반환 표시입니다. 그것은 routeChangeError 함수를 활성화하지 않습니다. 내 console.log()
이 호출 되더라도.
사용자가 인증되지 않은 경우 firebase.auth().currentUser
이 null을 반환하기 때문일 수 있습니까?
사용중인 버전 또는 타사 종속성을 알 수는 없지만이 기능은 전혀 작동하지 않으며 문서에서 찾은 것처럼 보이지 않습니다. firebase 모듈을 내 앱에로드하려고 할 때 정의되지 않았다는 오류가 발생합니다. 그래서, 어떤 제 3자를 사용하고 있습니까? –
당신은 또한 당신의 URL을 더 이상 정의하지 않고 Firebase에서 제공 한 코드 블록을 사용하여 firebase를 초기화합니다. 내 질문은 타사 종속성에 관한 것이 아닙니다. –
저는 Firebase 팀에서 일하고 있으며 저는 AngularFire의 관리자입니다. 나는'$ firebaseRefProvider'와'$ firebaseAuthService' 기능을 썼습니다. 데이터베이스 참조를 사용하여 종속성 주입을 처리하는 방법입니다. 우리는 문서 작업을하고 있습니다. github의 문서에서 인증 방법을 확인하십시오 : https://github.com/firebase/angularfire/blob/master/docs/guide/user-auth.md. –