-
-
Save niladam/d086f5b3c2811b419f501af8f690ec8f to your computer and use it in GitHub Desktop.
Laravel, Axios & Logout Over AJAX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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