Laravel 7: How to clear withErrors session - session

I'm using Gate for permissions and redirect the user to the home if he doesn't have enough permission with an error message
if(Gate::denies('manage-users')){
return redirect(route('home'))->withErrors('You don\'t have enough permissions!');
}
But when the user navigates to another route with correct permission the page displays correctly but with an error handler in the view saying the same message "you don't have enough permissions"
How can I clear errors session once the error get displayed in home to hide it from other views?

Don't know if this is the best solution so please correct me. I Changed the validation to be this
if(Gate::denies('manage-users')){
return redirect(route('home'))->withErrors(['permission_error' => 'You don\'t have enough permissions!']);
}
In the home view
#if(session()->has('permission_error'))
{{session('errors')->get('permission_error')}}
#php session()->forget('permission_error') #endphp
#elseif( !session()->has('permission_error') && $errors->any())
<div class="alert alert-danger">
{{ $errors->first() }}
</div>
#endif

Related

Get current logged-in user in Laravel Layout page

I am using laravel auth application in that layout page is present, So to get name of current logged-in user in layout.blade.php I have use {{Auth :: user () -> name}} and it gives name too. But the problem is that when i get logout and try to login then login page gives error Trying to get property 'name' of non-object.
Please help me.
If no user is logged in so Auth::user() will return null,
so in your blade you can use Auth::check() to verify if a user is logged in
#if(Auth::check())
{{ Auth::user()->name }}
#endif
More info can found in
https://laravel.com/docs/7.x/authentication#retrieving-the-authenticated-user
you can use #auth #endauth helper in blade
#auth
{{ Auth::user()->name }}
#endauth
ref link https://laravel.com/docs/8.x/blade#authentication-directives
You will need to check if User is logged in first
try
#if(auth()->check())
{{auth()->user()->name}}
#endif
or in one line
{{auth()->user()->name ?? ''}}

Laravel Forgot Password (Reset Password)

I have a Laravel app, and the password reset feature is needed. How can I flash a session message after the email is sent to the user or after clicking the submit button?
I can't seem to add a flash method to the function that returns the view of the reset password page, because the file is in the vendor folder.
After an hour of tracing of what function will be triggered after the submission of the password reset form, I have found out that:
After submission of the password reset form, the function sendResetLinkEmail will be triggered, can be found in SendsPasswordResetEmails.php.
Then in line 37
return $response == Password::RESET_LINK_SENT
? $this->sendResetLinkResponse($response)
: $this->sendResetLinkFailedResponse($request, $response);
The sendResetLinkResponse function is the one responsible for what happens if the email is sent successfully.
protected function sendResetLinkResponse($response)
{
return back()->with('status', trans($response));
}
By default, laravel auth:make doesn't display the 'status' variable in the front end or the reset password page.
I have to add these lines of code to the reset password template.
#if(session()->has('status'))
<div class="alert-container">
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{ Session::get('status') }}
</div>
</div>
#endif
From the docs:
Sometimes you may wish to store items in the session only for the next request. You may do so using the flash method. Data stored in the session using this method will only be available during the subsequent HTTP request, and then will be deleted. Flash data is primarily useful for short-lived status messages:
$request->session()->flash('status', 'Task was successful!');

Redirect with success or error message without using sessions

How can i redirect with success or error message without using sessions in laravel
Currently I am using the following code for redirect :
return redirect('dashboard')->with('status', 'Profile updated!');
But this code Need session to display the success message.
You can set another variable status type along with the status like below,
return redirect('dashboard')->with(['status'=>'Profile updated!','status_type'=>'success']);
And in your dashboard blade file use
#if(isset($status))
<p class="alert alert-{{ $status_type }}" >{{ $status }}</p>
#endif

Laravel redirect()->with('status','My message') error

I have created a email verification on Laravel 5.2. When a user fill the registration form and is validated they return the user to login form with:
return redirect('/login')->with('status','We send a email verification, please
confirm.');
And this works perfect. The problem is when the user click on link verification in his inbox they activated the account and redirect to admin page but without status message. Here is the code for that in AuthController.php:
public function activateUser(Request $request,$token)
{
if ($user = $this->activationService->activateUser($token)) {
return redirect('/login')->with('status','You account is now activated. Please login.');
}
abort(404);
}
On my login.blade.php:
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
Please, someone know why the session variable is not working?
Thanks.
it should work like this. in your AuthController, just change
return \Redirect::to('/login')->with('status','You account is now activated. Please login.');
and in your login.blade
#if (Session::has('status'))
<div class="alert alert-success" role="alert">
<p>{{ Session::get('status') }}</p>
</div>
#endif
Okey i don't what happend here and why redirect()->with() is not working. I solved doing a new view called activated.blade a copy of login.blade but with a message saying: "You account is now activated. Please login." so i do this in the controller:
return redirect('/activated');

Laravel 4 Login Credentials mismatch does not send back error data

Everything is fine, Login, Validation, and Username/Password combination mismatch, except for the mismatch, it does not send back the data that I'm sending.
This is my code, and I've tried different login techniques, and same issue.
This is my blade page errors section:
<div id="formErrorDiv">
#if ($errors->has())
<h2>* Please check your errors.</h2>
#endif
#if (isset($mismatch))
<h2>* {{ $mismatch }}</h2>
#endif
</div>
while the $errors-has() for the fields' validation works perfectly, the $mismatch variable always turns out empty.
This is my controller login section:
if($auth){
return Redirect::intended('/');
} else {
//dd('auth else');
$mismatch = 'Login Credentials Error.';
return Redirect::route('account-sign-in')
->with('mismatch', $mismatch);
}
although when I use the dd('auth else') it does work, and so it enters the else section but always sends nothing with the redirection.
You need to use Session::get() to retrieve the value you pass with with():
#if(Session::has('mismatch'))
<h2>* {{ Session::get('mismatch') }}</h2>
#endif
errors is the exception here. Because it's very common to pass that to the next view, Laravel built it in so the errors variable from the session is automatically available in your views...

Resources