Laravel 5.3 keeps logging out - php

I'm working on a new app with laravel 5.3(.18) and for some reason the session keeps getting lost. After logging in (even with remember token) I get logged out as soon as I go to any other location, and therefore redirected to the login page again.
I have the following in routes/web.php
Auth::routes();
Route::get('/', function () {
if( Auth::guest() ){
return view('auth.login');
}else{
return view('welcome');
}
});
Route::group(['middleware' => 'auth'], function () {
Route::resource('orders', 'OrderController');
Route::resource('products', 'ProductController');
Route::get('/dashboard', 'DashboardController#show');
});
I already switched the session to 'database' in my env file and migrated.
Any ideas?

Anyone who still encountering this problem can check if they are using Auth::logout() in their views. Was the issue in my case.

Related

how can manage laravel 5 login page after the logging?

I have created laravel 5 login and registration form using make:auth command. but I have both home.blade.php and front.blade.php in My layouts folder. My problem is this I need front.blade.php file when I visit to My web app as My index page(opening page of the site). and then I need home.blade.php file when I logging with the system. (to do cms works). I thing target place is routes.php
This is routes.php
Route::auth();
Route::get('/', function () {
return view('front');
});
Route::get('/home', function () {
return view('home');
});
Route::get('/about',function(){
return view('about');
});
Route::get('/bod',function(){
return view('bod');
});
Route::get('/st',function(){
return view('st');
});
Route::get('/services',function(){
return view('services');
});
Route::get('/contact',function(){
return view('contact');
});
Route::get('/welcome',function(){
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
but when I use above routes My web app always redirect to front.blade.php file after the logging with the system. I need redirect with home.blade.php after the logging with the system.
You can add a check inside the front route and redirect to home if the user is logged in.
if (Auth::check()) {
return redirect('home');
}

Laravel 5.3 set homepage as login screen

How do I set the homepage (/) to the login screen in Laravel 5.3?
I have a routes file :
Route::get('/', function () {
return view('welcome');
});
I have set up the basic auth scaffolding with the command php artisan make:auth and have set up my db tables too.
But I'm struggling to understand how to set the homepage to always go to the login screen if the user is not authenticated? Surely this is just me being stupid right?
I just needed to specify the middleware('auth') for my route:
Route::get('/', function () {
return view('home');
})->middleware('auth');
Route::get('/home', 'HomeController#index');
This way if you're not logged in it will redirect to login automatically.
You can do it like this:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
return view('welcome');
});
});
Just put all the routes that needed authentication inside that middleware group.
in laravel in general you can change the url view path to what you want as example
Route::get('/', function () {
return view('auth.login');
});
In laravel 5.4 you can modify the route as
Route::get('/', 'Auth\LoginController#showLoginForm');

Laravel 5.3 Direct Home URI to Auth in Routes?

Laravel 5.3 newb here. I want going to localhost:8000 to go to the login page generated by php artisan make:auth.
My routes web.php looks like this by default:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
This of course brings it to the default Laravel welcome page.
I change it to this:
Route::get('/', function () {
return view('home');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
Which brings up the dashboard saying You are logged in! when no authentication has been done, so that isn't working.
I change it to this and nothing loads at all:
Route::get('/', function () {
Auth::routes();
Route::get('/home', 'HomeController#index');
});
Have tried a few other things and nothing seems to work. Would also like the URI to just be localhost:8000 and not localhost:8000/login or anything like that if possible.
Any suggestions?
About 5 minutes after posting this, I realized I needed to do this:
Route::get('/', function () {
return view('auth.login');
});
Auth::routes();
Route::get('/home', 'HomeController#index');

Laravel Auth::guest() doesn't work

I have installed authentication using
php artisan make:auth
Before and after loggingin menu shows login and Register link appears even user has logged in. I checked the code below and I found it was correct, there may be problem in Auth::guest(). check the code below
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
I referred some of the questions and they have given some dirty fix. example below.
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'HomeController#index');
});
Actually what this fix does,is force user to login by showing login page.Actually when the user logged in it should hide and show the user name. When I click on home link, it hides and shows username.
Make sure you have the view on the web middleware, use the following code.
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () { return view('welcome'); });
Route::get('/home', 'HomeController#index');
});

Laravel 5 Routing with Auth

Hello!
I'm trying to learning laravel, and i started with doing a page that only logged people can access ( I don't have login yet ) , but i'm just testing, so far i have:
Routes.php:
<?php
Route::get('/', ['as' => 'index', function () {
return view('index');
}]);
Route::get('/home', function () {
return view('home');
});
Ok, this file contains the routes , i searched how to give a name to the route, so i can redirect user to that route when he don't have access.
My other file is home.blade.php:
<?php
if (!Auth::check()){
return Redirect::route('index');
}
?>
So is that the best way to check if user is logged? I gave a name to the route, index , so if user is not logged i want to redirect back to index, is not working tho, i want to know if this is the best option to achieve my goal.
Thank you.
The best way, as said at documentation is:
Route::get('/home', ['middleware' => 'auth', function () {
// here will be only authorized access
return view('home');
}]);
Auth middleware defined by default.
use Auth; loading facades
if (Auth::check()){
if(Auth::Guest()):
return Redirect::route('index');
endif;
}
?>

Categories