Log in a member - Silverstripe 4

Sometimes there’s a need to log-in a member manually, (ie. not through the normal login forms).
In Silverstripe 3 you could do this with Member::login(), but this is now deprecated in SS4

In Silverstripe 4, the way that this process is handled has changed. You can use Security::setCurrentUser(), but this only sets the user for the current request and won’t persist. To perform an ‘actual’ log-in, you’ll need to use the IdentityStore class:

$identityStore = Injector::inst()->get(IdentityStore::class);
$identityStore->logIn($member, $rememberMe, $request);

The login() method takes a standard member object at the first (mandatory) argument.

SilverStripe\Security\IdentityStore | SilverStripe API for more info. (Don’t forget the relevant use statements in your code if you’re using namespaces)

(Hat-tip to @kinglozzer for the code snippet!)

4 Likes

Can you expand that example by the MemberAuthenticator. So it would be validation and logging in :slight_smile: