So whenever I logout from Firebase, I got coupled
Error: permission_denied: Client doesn't have permission to access the desired data.
I understand it is because login session is terminated, some of my objects cannot access firebase data any more. But how can I disconnect this objects before logout?
For logout button in one of my Ionic View, it just call a firebase service:
function logout() {
auth.$unauth();
getCurrentUser();
};
function getCurrentUser() {
var authData = auth.$getAuth();
if (authData) {
$rootScope.userId = authData.uid;
$rootScope.currentUser = $firebaseObject(authRef.child("users").child(authData.uid));
return $rootScope.currentUser;
} else {
console.log("User is not login!");
$rootScope.userId = null;
$location.path("/auth/signin");
if ($rootScope.currentUser) {
$rootScope.currentUser.$destroy();
}
}
};
So I destroy the $rootScope.currentUser there. I use the same getCurrentUser for profile page. So the Error did not show up this way. But when in other views, which I have another $firebaseArray, and also another Ref.on("child_added", function(snap) with the same $firebaseObject. When I view the profile page, then this page with at least 3 firebase connection, I got 3 permission_denied Errors when I logout (logout button is on user profile page).
My question is, how do I disconnect this firebase connection before I logout? Is there a way disconnect ALL the firebase connection - no matter AngularFire or regular Firebase? So I can logout without worry about which firebase connection I have no close yet? Also, since the Logout button is in Profile scope and the others connection is in a different scope, I have no idea how to close the connection which is not even in the profile scope...
You need to destroy all the firebase references on logout.
Something like this.
In logout function.
function logout() {
auth.$unauth();
$rootScope.$broadcast('logout');
};
In controller
vm.profile = authService.profile(user.uid); // FirebaseObject Reference
vm.parties = partyService.getPartiesByUser(user.uid); // FirebaseArray Reference
$rootScope.$on('logout', function () {
vm.parties.$destroy();
vm.profile.$destroy();
});
well, i guess you have a button to logout.
so in your function logout() you'd first $destroy the data object, somehow wait (whichs' best practice i'm trying to figure out), and then authref.unauth();
i'd say
You need destroy the firebase ref for the object that you saved data previously. How?
Before, I initialize my var songs like:
this.songs = this.af.list('/songs');
When I signOut(), I should destroy the reference of the variable that I initialized so that I execute:
this.songs.$ref.off();
With this line, your problem
Related
I am trying to clear login session using AngularJS code.
$scope.clearLogin = function () {
delete $window.sessionStorage;
window.localStorage.clear();
$window.location.href = 'index.html';
};
This is redirecting me to index page but when I click on back button I am getting into the application and the session is still available.
How to clear session restricting the user to log into the application after logging out?
It should be $window
$scope.clearLogin = function () {
delete $window.sessionStorage;
$window.localStorage.clear();
$window.location.href = 'index.html';
};
The problem is when a user 1 has signed out, and once the user 2 signs in, either the info of the user 2 is not show, or the info of user 1 is still shown. Apparently the cookies from the first user still hang around and prevent the transition softly.
here is my signout controller, does anyone have any improvements on this?
app.controller("MysignOutCtrl", ["$scope",function ($scope) {
$scope.signOut = function () {
firebase.auth().signOut().then(function() {
console.log("Sign-out successful");
}, function(error) {
toastr.error(error.message, error.reason, { timeOut: 10000 });
})
};
}]);
add the localStorage.clear(); after signout.
If you use AngularFire, always use its wrapper methods to prevent some unexpected behaviors.
I would use $firebaseAuth().$signOut() method.
Firebase does not support multiple logged in users. If you loggin with second account, it overwrites existing user data.
So my Marionette application has folowing 2 routes and controllers
AuthRoute and AuthController for user login and logout
UserRoute and UserControler for user listing, new user and edit user
In AuthController when user login I need to save the user session somewhere so that it can be acceable to both these routes, So next time when user hits user listing page I can check for user session and then load the user listing page.
How can I achieve this?
Also, what will be the best way to check if user session is valid or not? My plan is to create a service which will return user details if the session is valid or else will return 401, but till the time service returns the response I can't load the route specific views.
Can someone help me on these?
Thanks in Advance
MSK
I usually save session data in a singleton Beckbone.Model referenced by the Marionette.Application.
Session Model:
var SessionModel = Backbone.Model.extend({
initialize: function () {
this.listenTo(this, 'change', function (model) {
// Manage cookies storage
// ex. using js.cookie
Cookies.set('session', model.toJSON(), {expires: 1});
});
},
checkAuth: function () {
...
},
login: function (user, password, remember) {
...
},
logout: function () {
...
}
});
Application:
var app = new Marionette.Application({
session: new SessionModel(),
...
})
app.reqres.setHandlers({
session: function () {
return app.session;
}
});
Then you can access session model from any point through "global channel":
Backbone.Wreqr.radio.channel('global').reqres.request('session').checkAuth()
So you can implement all your session procedures in the session model class that can be accessed from the whole application. Using cookies you will also save the user session in the browser.
If you use backend API for Backbone then:
How can I achieve this?
The best way for saving a user session is cookies. Use it for storing and retrieving the user authentication_token.
Also, what will be the best way to check if user session is valid or
not?
You have to store authentication_token on the backend side. For login, logout, signup you should iterate with your API.
I have implemented the logout function in my application which uses site minder for login.Every time i logout for the first time its logs me out.If i login again and try to log out it directly logs in without asking credentials.
Can you help me on this. I have used $cookiestore to remove the cookies but its doesn't helps. Is there any way to get rid out of it.
Code:-
$scope.logoutUser = function() {
$cookieStore.remove('AWSELB');
$cookieStore.remove('SMSESSION');
window.location.href = config['logout_url'];
};
After calling $cookieStore.remove, try to read the value from cookie, it is still there, remove does not work properly, I already faced the same issue.
Finally I decided to use the local storage, really it is easy, better and faster
service.logout = function () {
$localStorage.loggedInUser = null;
$state.go('login');
};
On Login:
service.login = function () {
// validate and get data say user
$localStorage.loggedInUser = user;
$state.go('home');
};
Can someone tell how is authentication actually working in MEAN.JS [meanjs.org] here?
When the user logs in,where is the USER json object stored. The code is
// Authentication service for user variables
angular.module('users').factory('Authentication', [
function() {
var _this = this;
_this._data = {
user: window.user
};
return _this._data;
}
]);
//The implementation of the service in the controller
$scope.authentication=Authentication;
// If successful we assign the response to the global user model
$scope.authentication.user = response;
but in console window.user returns null
Another question, how the app retains the user JSON object when the page is refreshed? After the page is refreshed window.user returns the user JSON object, I don't see this object in the Local Storage or Session Storage, neither do i see any ajax being made to the server to fetch this object.
Thanks