Reputation: 139
I would like to know how to run a function when a user authenticates,
This is my function for log
public function redirectPath()
{
if (Auth::user()->hasRole('admin')){
$bitacora = TblBitacora::create([
'accion' => 'Inicio de Sesión Admin Exitoso',
'user_id' => Auth::id(),
'ip' => \Request::ip(),
]);
return '/';
}
}
I would like to run this script
@if(Auth::user()->??????????????????????????????????????????
<script>
$(function() {
$('#mostrarmodal').modal('show');
});
</script>
@endif
Upvotes: 0
Views: 701
Reputation: 2602
You can use JS localStorage
to help you to do it:
@if(\Auth::check())
<script>
if (localStorage.getItem('already_connected') !== '1') {
$('#mostrarmodal').modal('show');
localStorage.setItem('already_connected', 1);
}
</script>
@endif
Now, if you refresh the page, unless you clear localStorage data, it will not show run this function again.
Hope it helps.
Upvotes: 0
Reputation: 34678
First Question Answer : You can check is user login in your blade file and add javascript or something like this in your view file :
@if(\Auth::check())
<script>
$(function() {
$('#mostrarmodal').modal('show');
});
</script>
@endif
If Authenticate user is an Admin
@if (auth()->check())
@if (auth()->user()->Admin())
// Put your js files here
@else
// this is for others role
@endif
@endif
Second Question Answer : If you want to run a function if user is authenticate then you can do it like that :
public function check() {
if(Auth::check()){
$this->secondFunc(); // This will run if user is authenticate.
}
else{
return redirect('auth/login');
}
}
public function secondFunc() {
echo "This is second function";
}
Upvotes: 0
Reputation: 686
Laravel has Authentication Directives
The @auth and @guest directives may be used to quickly determine if the current user is authenticated or is a guest:
@auth
// The user is authenticated...
@endauth
@guest
// The user is not authenticated...
@endguest
More information available at https://laravel.com/docs/5.8/blade
Also I wouldn't recommend to log in redirectPath method in your controller. You can use events, an similar to what you want to achieve is provided as an example on Laravel docs. https://laravel.com/docs/5.7/events#event-subscribers
Upvotes: 1