NotToBrag
NotToBrag

Reputation: 665

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!

Upvotes: 6

Views: 6820

Answers (2)

zevij
zevij

Reputation: 2446

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.

Upvotes: 2

teopeurt
teopeurt

Reputation: 461

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

Upvotes: 7

Related Questions