Recently, I've been exploring the Firebase documentation on authentication with Routers. In my project, I'm using Ionic with ui-router, incorporating abstract views and actual views with controllers. Here's the structure I have set up:
.state('auth', {
url: "/auth",
abstract: true,
templateUrl: "app/auth/auth.html",
resolve: {
"currentAuth": ["Auth", function(Auth){
// $waitForAuth returns a promise so the resolve waits for it to complete
return Auth.$waitForAuth();
}]
}
})
.state('auth.signin', {
url: '/signin',
views: {
'auth-signin': {
templateUrl: 'app/auth/auth-signin.html',
controller: 'SignInCtrl'
}
}
})
.state('home', {
cache: false,
abstract: true,
url: "/home",
templateUrl: "app/home/home.html",
controller: 'homeTabCtrl',
resolve: {
"currentAuth": ["Auth", function(Auth){
// $requireAuth returns a promise so the resolve waits for it to complete
// If the promise is rejected, it will throw a $stateChangeError (see above)
return Auth.$requireAuth();
}]
}
})
.state('home.courses', {
cache: false,
url: "/courses",
views: {
"tab-courses": {
templateUrl: "app/home/courses.html",
controller: "courseCtrl"
}
}
})
The current setup works smoothly. Whenever a user tries to access home.courses without logging in, they are redirected to auth.signin. However, now I'm facing a challenge where I want to access the currentAuth object/promise within SignInCtrl. The goal is that if a logged-in user navigates to the auth.signin state, they should be redirected to home.courses instead. Following the documentation, I tried injecting currentAuth into the controller like this:
(function () {
'use strict';
angular.module("myApp").controller('SignInCtrl', ['currentAuth', '$scope', '$rootScope', SignInCtrl]);
function SignInCtrl(currentAuth, $scope, $rootScope){
console.log (currentAuth);
}
})();
However, an error occurs:
Error: [$injector:unpr] Unknown provider: currentAuthProvider <- currentAuth <- SignInCtrl
I followed the examples in the docs but haven't been able to fetch the currentAuth promise object successfully (which would help me in implementing a redirect logic based on authentication status). Any suggestions or guidance from the Firebase team would be greatly appreciated.
UPDATE:
Based on feedback from Kato, it seems accessing the currentAuth promise directly might not be feasible. In light of this, what would be the standard approach to check if a user is already logged in while they are at the /auth/signin/ route, and then redirect them to the /home/courses/ route accordingly?