subfission / cas

Simple CAS Authentication for Laravel 5 - 10.
MIT License
151 stars 70 forks source link

How to implement logout using Inertia-VueJS - Laravel #99

Closed arhen closed 1 year ago

arhen commented 3 years ago

Im trying to implement logout via request because my application is SPA. But got CORS-Policy using function logoutWithRedirectService in the backend. Is there any how-to for SPA apps?

subfission commented 3 years ago

I'd recommend checking with https://github.com/apereo/cas since this package leverages and abstracts Apereo CAS. If you find out, please post it back to help others.

coolsam726 commented 2 years ago

I use the package in many Laravel-Jetstream-InertiaJS-VueJS Apps. I could share the workflow if interested.

alfarioekaputra commented 2 years ago

i have same problem, can you share your workflow @coolsam726 ?

thanks

coolsam726 commented 2 years ago

Here is the setup that I use for my Inertia.js setup: NB: cas.logout is configured in the web guard, not api. Background:

Logout With the above login logic in place, here is how you logout:

    public function casLogout(Request $request): \Illuminate\Http\Response
    {
        $this->guard->logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        // cas()->logout();
        return Inertia::location('MY_CAS_LOGOUT_URL_HERE'); // <------- Important in order for the client to redirect to the logout service and logout the cas session.
    }

Calling logout route from the Inertia SPA client

<template>
      <button @click.prevent='logout'>LOGOUT</button>
</template>
<script>
export default {
    methods: {
         logout()  {
            const vm = this;
            const win = window.open(this.route("cas.logout"), "_blank");
            setTimeout(function () {
                win.close();
                vm.$inertia.reload();
            }, 1000);
         }
    }
}
</script>

What will happen: Once you click the button, the system will logout your local session, then open a separate window for 1000ms (You can adjust this to your preference), long enough for it to redirect to the CAS logout page and logout the cas session, then close the window automatically.

If you need any clarifications, do reach out.