Apex – Custom Login with Password for Experience Cloud Sites

apexcommunityexperience-cloudoauth2

We have a few experience cloud sites in production. Right now, the login and register flows are passwordless, using the salesforce's native passwordless login and verification functionalities. Our client asked to change this to now add a password to this.

In the backend, when someone registers in the site, a User is created, and a randomized password is set with System.setPassword. When logging in, a verification code is sent via mail to the user and has to enter it to successfuly log in (if the user is found).

Now, they want to remove that verification code flow and only ask for username and password. I understand there is no way to validate a user password from Apex alone as the password is not accessible. I googled a bit and I didn't find any specific information as to how validate a password in a community site context, so here I am, asking for tips or any help.

Do I have to use a OAuth authentication method? If that is the case, I understand that the Username-password Oauth flow is NOT recommended or even supported in community sites. Which one is the best suited for my use case? Really, any help or direction is appreciated.

Best Answer

You can use Site.login to use their username and password. On successful login, you'll get a PageReference that contains the URL necessary to access site in a logged-in state.

Related Topic