how to Implement logout functionality in laravel - php

without login If I give any URL's in the admin panel it is opening. How to solve it?
while login I am using,
$request->session()->put('userId', $user->userId);
In logout I am using,
$request->session()->forget('userId');
Route file,
Route::get('/addModule', function () {
return view('addModule');
});
How can I redirect to login screen. while am calling any admin panel URL's.

Just write this code in routes.php
Route::get('logout', function(Request $request) {
Auth::logout();
return redirect('/login');
});
Remember to import Auth class

Related

I can't logout with laravel

I'm working on my first laravel project in which I made an admin panel to make admin able to controller the website. I tried to login admin into dashboard and everything was fine, when I tried to log him out nothing works and the page was just reloading and redirect back.
Here is my logout button:
<a class="navbar-brand btn btn-primary btn-lg"
href="{{route('admin.logout'))}}"> log out</a>
My route:
Route::get('logout', 'LoginController#logout')->name('admin.logout');
And my controller:
use Illuminate\support\Facades\Auth;
public function logout(Request $request){
Auth::logout();
return redirect()->route('get.admin.login')
->with(['success'=>'logged out successfully']);
}
I used Auth::logout(); method from laravel official documentation, however it didn't work.
I hope I can find help.
as your using Auth::logout()inside that function
public function logout(Request $request) {
Auth::logout();
return redirect('/login');
}
here Auth:: will be null
so apply middleware to get Auth instance and then you can logout
Route::get('logout', 'LoginController#logout')->name('admin.logout')->middleware('auth');
You can use the following in your controller:
return redirect('login')->with(Auth::logout());
You can use below code, for logout the admin.
Auth::logout();
return redirect('/login');

Laravel redirect to home page

I am using Language switch to switch between English and Arabic. It is working fine in all the links except Home page.
If I have not authenticated it is working fine it is redirecting to http://domain.name/en/login
If I have authenticated or logged in and try to access the url http://domain.name/ it is redirecting to http://domain.name/home instead of a http://domain.name/en/home
I have changed in all the Auth files by adding a function:
public function redirectTo(){
return app()->getLocale().'/home';
}
The solution for this is to make a automatic redirect to the page.
Route::get('/home', function () {
return redirect(app()->getLocale());
});

Laravel does not redirect to login page when I try to access to home page

I try writting the url http://127.0.0.1:8000/home and I'm not logged in and it shows me home page but obviously not loaded correctly, What I want is when I write http://127.0.0.1:8000/home to redirect me to http://127.0.0.1:8000/login page I know this works in fresh projects with make:auth module, I don't know why stopped workin.
In my home I have this
public function __construct()
{
$this->middleware('auth');
}
and my routes file:
Route::get('/', function () {
return view('auth\login');
});
Auth::routes();
Route::post('create','IngresarSolicitud#store');
Route::get('/home','IngresarSolicitud#informacionempleado');
I added ->middleware('auth'); at the end of my routes to redirect to login in this case, thanks

Hesto multi-auth guard is not redirecting where page was before after login

I am on page : http://laravel.dev/lists
which shows me list of item
Item 1
Item 1
Item 1
but above item can only accessible to authorized user
Issue
when i click on Item 1 so if user is not logged in then it goes to http://laravel.dev/login then user proceed to login but after login it must redirect to http://laravel.dev/lists/1 but it is redirecting to http://laravel.dev/home.
I have tried
public function showLoginForm()
{
session()->put('url.intended',url()->previous());
// or
session()->put('url.intended', url()->current());
return view('user.auth.login');
}
but above is not working, it is redirecting to http://laravel.dev/lists
laravel own auth is working fine.
but hesto multi-auth is not working
I am using this Hesto-multi-auth package for multiple authentication like admin, user, employee
is there any solution for this
If you're using the default user Authentication system in Laravel, and if you added auth middleware in your routes, it's automatically done by Laravel.
In your routes:
Route::group(['middleware' => 'auth'], function() {
. . . Your Routes goes Here
});
You can take a look at the docs https://laravel.com/docs/5.4/authentication#protecting-routes
You can use middleware. For example :
Route::get('lists/{id}', function () {
//
})->middleware('auth');
Please refer here : https://laravel.com/docs/5.5/middleware for more information
First of all find the place where you redirect unauthenticated user to the login page (probably in app/Exceptions/Handler.php function unauthenticated) and save to the session current url ("/lists/1") before redirect.
Afrer that add in your Auth controller property $redirectTo and fill it from the session or define method redirectTo (method also nust return url).
Out of the box laravel redirects to /home because of trait /Illuminate/Foundation/Auth/RedirectsUsers.
You can try this idea: After you've successfully logged in, you'll be redirected to the page you want (e.g. http://laravel.dev/lists/1)
Auth::attempt(...);
return redirect()->route('the name of your route here where redirected to http://laravel.dev/lists/1');
What you had tried in the showLoginForm() won't be work, because neither url()->previous() nor url()->current() you specified were not the url you want.
Solution:
Changed in the middleware:
public function handle($request, Closure $next, $guard = 'user')
{
if (!Auth::guard($guard)->check()) {
session()->put('url.intended', \URL::current()); // Add this line
return redirect('user/login');
}
return $next($request);
}

customize redirection logged in user on /login route - laravel

I have a laravel application
login page is in route /login
there is a logged in user and clicks on a login button (or basically open URL /login)
application redirects the user to /home but I want to be redirected to /dashboard
I changed the redirect fields in Auth controllers to /dashboard. results when a user signs in, application redirects him to /dashboard
but what about a logged in user?
I use laravel 5.4, thank you for helping
You should use the RedirectIfAuthenticated middleware that is supplied with Laravel located in App\Http\Middleware\RedirectIfAuthenticated.
Then, in the following block, make sure it's set to /dashboard.
if (Auth::guard($guard)->check()) {
return redirect('/dashboard');
}
Then add the middleware to your login route by either wrapping it in a group:
Route::group(['middleware' => 'guest'], function(){
//Auth routes for non-authenticated users
});
Or you can do it on the route directly:
->middleware('guest');
Goto login controller which is located in
app->Http->Auth->LoginController
Set
protected $redirectTo = '/dashboard'
Hope it works.
Source : https://laravel.com/docs/5.4/authentication#included-authenticating
Since you want to redirect logged in user you can override showLoginForm() method in LoginController like this:
public function showLoginForm()
{
if (auth()->check()) {
return redirect('/dashboard');
}
return view('auth.login');
}
But I guess a better way to solve the problem could be just hiding login button or link from logged in users:
#if (auth()->guest())
{{-- Show register and login links --}}
#endif

Categories