I search for a way to create a Logout link from outside of OctoberCMS.
So basically this here in a normal HTML page:
<a data-request="onLogout" data-request-data="redirect: 'https://my-october-page.com'">Logout</a>
I tried it together with the import of framework-extras and jquery but didnt have success.
Maybe someone has an idea of how I could get a solution for this problem.
Use {% do ajaxHandler('onLogout') %}
1 Like
Hm wait, but I try to do this outside of OctoberCMS on a normal HTML page.
So basically Twig is not even compiled here…
Make a dedicated logout CMS page, that logs out when you make a GET call to it.
If the normal page is on the same host, you can call the URL using a basic AJAX call.
Or you make a redirect page
{% do ajaxHandler('onLogout') %}
{% do redirect('back/to/my/page.html') %}
1 Like
uoh… ok this is a great solution!
Only open question: The onLogout generate a flash message which would be displayed on the /logout page if there wouldnt be a redirect. How to pass the message to the redirected page or how to add a custom flash message to this redirect page?
Please read the documentation link I gave.
The following properties can be expected in the resulting object.
flash flash messages set by the handler.
Sorry at that point - I did read it but this here is all I could do:
{% set result = ajaxHandler('onLogout') %}
{{ result.flash.success }}
This here stores the result of the handler, but the handler does not logout anymore, so… its kinda usless
{% do ajaxHandler('onLogout') %}
{% do redirect('/') %}
This here works, but the logout mssage is not displayed on the start page.
url = "/logout"
layout = "default"
title = "Logout"
==
function onEnd()
{
Flash::success('You have been successfully logged out!');
return Redirect::to('/');
}
==
{% do ajaxHandler('onLogout') %}
This here works as expected with the logout message on the startpage which you’re redirected to.
I just hoped there’s a combination of all to call the handler, then get the message from the handler somehow and pass this message as flash message to the redirect - the redirect with flash in twig is my problem, not the call of the handler.
The solution from the last post still had bugs. This here is the final solution:
url = "/logout"
layout = "none"
title = "Logout"
==
function onStart()
{
# Not logged in
if(Auth::getUser() === null) {
return Cms::redirect('/login');
}
# Logged in, call logout handler and redirect. Message comes from handler.
else {
$sessionComponent = new RainLab\User\Components\Session;
$sessionComponent->onLogout();
return Cms::redirect('/login/');
}
}
==
So if you call the /logout from anywhere in your website now, there’s a redirect if the user is not logged in and if there’s a user logged in, he’s logged out.
I got a Layout called “none” btw… which is very empty obviously…
1 Like