Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse, Check to See If User is Logged In

I want to check to see if my user is logged in, at the moment I am using

if(currentUser == null) { window.location.replace("login.html"); }

to redirect my user to the login page. But I've seen tutorials where user's wrap the full code in a if(currentUser) {...} instead.

I was also intriguied by Parse's authenticated() but the documentation doesn't really specify how to use it, other than state that it returns a boolean.

I just wanted to know if either one of these three is faster/more efficient than the other, and if parse's auth is, I'd like to know how that works.

And I am looking to redirect the user back to the login.html page if he is not logged in.

Thanks!

like image 405
NotToBrag Avatar asked Sep 22 '14 10:09

NotToBrag


2 Answers

I assume Javascript - The introduction docs are good enough

    var currentUser = Parse.User.current();
if (currentUser) {
    // do stuff with the user
} else {
    // show the signup or login page
}

This is copied from the docs

like image 69
teopeurt Avatar answered Oct 24 '22 01:10

teopeurt


You might want to check if the session is actually valid too. User might be logged in but session might have expired (or account hacked).

One way to achieve this would be to query an object in Parse and catch error 209. If caught, you could log the user out and pop the nav controller back to login screen.

Take a look at this answer.

like image 20
zevij Avatar answered Oct 24 '22 03:10

zevij