Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel, Axios & Logout Over AJAX
import React from 'react';
import ReactDOM from 'react-dom';
const handleLogout = () => {
axios.post('/logout')
.then(() => location.href = '/home')
};
function Example() {
return (
<div className="container">
<div className="row justify-content-center">
<div className="col-md-8">
<div className="card">
<div className="card-header">Example Component</div>
<div className="card-body">
<p className="card-text">I'm an example component!</p>
<button className="btn btn-light" onClick={handleLogout}>Logout</button>
</div>
</div>
</div>
</div>
</div>
);
}
export default Example;
if (document.getElementById('example')) {
ReactDOM.render(<Example />, document.getElementById('example'));
}
<template>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Example Component</div>
<div class="card-body">
<p class="card-text">I'm an example component.</p>
<a class="btn btn-light" @click="logout">Logout</a>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
methods: {
logout() {
axios.post('/logout')
.then(() => location.href = '/home')
}
},
mounted() {
console.log('Component mounted.')
}
}
</script>
<?php
namespace App\Http\Controllers\Auth;
use ...
class LoginController extends Controller
{
...
/**
* The user has logged out of the application.
*
* @param \Illuminate\Http\Request $request
* @return mixed
*/
protected function loggedOut(Request $request)
{
if ($request->wantsJson()) {
return response()->json([], 204);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment