function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amol_NikamAmol_Nikam 

how to check if user is logged in or not?

hi all ,

 

i am new bee in salesforce. i have created sample site in force.com and i have my custom object table for user,now i want to check that whether the user logged in or not and i want to show the page to that user  also if someone tries to directly access that url then  want to be redirected the user to login page.

 

Thanks in advance,

Amol.

bob_buzzardbob_buzzard

In your controller, you can execute the following method to get the type of user:

 

UserInfo.getUserType();

 for unauthenticated users, this will return the string 'Guest'.

 

There may be other ways to achieve this, but the above is the only one I've been able to find!

 

Tim May 9Tim May 9
I used this as the best answer. I then wrote the following code around it so I could use it in an <aura:if>
 
@AuraEnabled 
    public static Boolean fetchUserType() {
        Boolean isLoggedIn = false;
        String userType = UserInfo.getUserType();
        if (userType != 'Guest') {
            isLoggedIn = true;
        }
        return isLoggedIn;
    }