Logout-Function in FrontEnd

silverstripe-4
Tags: #<Tag:0x00007f10c8623fb0>

#1

I want to offer a Logoutbutton in the FrontEnd.

I’d like to do via an ajax call.

The Member::logout is deprecated->Security::setCurrentUser(null)
-> that function has no effect

APi-docs tell me I could use SIlverStripe\Security\Security::logOut()
but if I call that function via URL I get an error…

Uncaught Exception Error: "Using $this when not in object context" at ...\site\vendor\silverstripe\framework\src\Security\Security.php line 726

Does anybody has a working solution for LogOut a User without the standard logout-page?

Thanks in advance
Fabian


#2

I use:

public function out(HTTPRequest $request)
{
	Injector::inst()->get(IdentityStore::class)->logOut($request);

	$this->redirect('/');
}

In a controller


#3

Injector::inst()->get(IdentityStore::class)->logOut();
this can be placed wherever you want (remember to use Injector in the namespaces)

It will just log out the current logged in member, so you may also want to test if the member has logged in
if ($member = Member::currentUser()) { ... }