WelcomeController
class WelcomeController extends Controller
{
public function ajaxRequestPost(Request $request){
$request->session()->put('data', $request->all());
return redirect('dashboard');
}
public function readSession(Request $request){
$email = $request->session()->get('data');
return $email;
}
}
Routes (Web.php)
return view('welcome');
});
Route::post('/', [App\Http\Controllers\WelcomeController::class, 'ajaxRequestPost'])->name('ajaxRequest.post');
Route::get('/dashboard/read', [App\Http\Controllers\WelcomeController::class]);
Route::post('/dashboard/read', [App\Http\Controllers\WelcomeController::class, 'readSession']);
Route::get('/dashboard', function () {
$email = session()->has('data');
if(!session()->has('data')){
return redirect('/');
}
return view('dashboard', ['name'=> $email]);
});
Route::get('/logout', function () {
session()->forget('data');
return redirect('/');
});
Welcome.blade For popuplogin from remote API
<div class="hidden fixed top-0 right-0 px-6 py-4 sm:block">
<!-- insert at the bottom part of body -->
<script src="provided.js"></script>
<!-- add button to show login window -->
<button id="btnShowLogin" type="button">Login</button>
<form id="login" action="{{ url('/') }}" method="POST">
@csrf
</form>
<script type='application/javascript'>
//create new instance
var api = new API("Givenapikey");
//initialize api
api.init(function(){
//attach click event to the button
$("#btnShowLogin").click(function(){
//show login window
api.login({type:'student'},function(result){
//check result
if(result.error==0){
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrftoken"]').attr('content')
},
url: "{{ route('ajaxRequest.post') }}",
data: { username: result.details.usr_name} ,
type: 'POST',
async: false,
success: function(result){
console.log(result);
document.getElementById('login').submit();
}
});
}
});
});
});
</script>
dahsboard.blade My target blade to display my username and other details. Snippet of the code from dashboard.blade.
<span class="info">{{ $name }}</span>
I don't know if I am doing right. Routing and controllers I think I am doing it wrong especially in routing. The output in dashboard.blade is 1. I don't know why.