I am currently following a tutorial on thinkster.io and encountering some issues. The tutorial uses a deprecated SimpleLogin, so I have made the necessary code changes. However, I keep running into an error that says:
TypeError: Cannot read property 'finally' of undefined at Scope.$scope.register (http://localhost:9000/scripts/controllers/auth.js:9:31)
I suspect the problem lies within the promise in my controller logic. It seems to be returning undefined? Any assistance would be greatly appreciated. I can provide more details as needed. Thank you.
SERVICE LOGIC
'use strict';
app.factory('Auth', function ($firebaseAuth, $rootScope) {
var ref = new Firebase('https://park-reservation.firebaseio.com/');
//var auth = $firebaseSimpleLogin(ref);
var Auth = {
register: function (user) {
return ref.createUser({
email: user.email,
password: user.password
}, function(error, userData) {
if (error) {
switch (error.code) {
case 'EMAIL_TAKEN':
console.log('The new user account cannot be created because the email is already in use.');
break;
case 'INVALID_EMAIL':
console.log('The specified email is not a valid email.');
break;
default:
console.log('Error creating user:', error);
}
} else {
console.log('Successfully created user account with uid:', userData.uid);
}
});
},
login: function (user) {
return ref.authWithPassword({
email: user.email,
password: user.password
}, function(error, authData) {
if (error) {
console.log('Login Failed!', error);
} else {
console.log('Authenticated successfully with payload:', authData);
}
});
},
logout: function () {
ref.unauth();
},
resolveUser: function() {
return ref.getAuth();
},
signedIn: function() {
return !!Auth.user.provider;
},
user: {}
};
$rootScope.$on('login', function(e, user) {
console.log('logged in');
angular.copy(user, Auth.user);
});
$rootScope.$on('logout', function() {
console.log('logged out');
angular.copy({}, Auth.user);
});
return Auth;
});
CONTROLLER LOGIC
app.controller('AuthCtrl', function ($scope, $location, Auth, user) {
if (user) {
// $location.path('/');
}
$scope.register = function () {
Auth.register($scope.user).finally(function() {
return Auth.login($scope.user).finally(function() {
$location.path('/');
});
});
};
});