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');
});
Related
I'm using Laravel 8 and am having trouble with redirecting the user when they are not logged in. I have found several similar questions but they are all in older versions of laravel and none of their solutions work.
I believe I am supposed to use Route::group() to apply the redirect to all my routes. It currently looks like this:
// In web.php
Route::group(, function(){
Route::get('/', [HomeController::class, 'home'])->name('home');
Route::get('/account', [HomeController::class, 'account'])->name('account');
Route::get('/feedback', [HomeController::class, 'feedback'])->name('feedback');
Route::get('/help', [HomeController::class, 'help'])->name('help');
});
I'm not sure what I'm supposed to be using in my first parameter in the group function. I believe that I need it to look through either the session or a cookie to see if there is a logged user?
At the moment, a user is stopped from logging in if they are on the login page and type the wrong user/password so the authentication is working. However, if they manually type a url in, ex. ip:port/help, they will be allowed access.
I want to have all routes on the site redirect to the login page unless the user is authenticated.
My Authentication code looks like this in my LoginController.php:
public function doLogin(Request $request){
DB::connection('mysql');
$args = $request->except('_token');
// attempt to do the login
if (Auth::attempt($args)) {
// validation successful!
return redirect('home');
} else {
// validation not successful, send back to form
return redirect('login');
}
}
My route to get to the doLogin function which is called from an html form action looks like:
Route::post('authenticate', [LoginController::class, 'doLogin'])->middleware('web');
If anyone could give any solutions or lead me on the right path, that'd be amazing. Thanks!
Edit: Solution!
In the parameter I was questioning about I needed to simply add the auth middleware.
Route::group(['middleware' => 'auth'], function(){
Route::get('/', [HomeController::class, 'home'])->name('home');
Route::get('/account', [HomeController::class, 'account'])->name('account');
Route::get('/feedback', [HomeController::class, 'feedback'])->name('feedback');
Route::get('/help', [HomeController::class, 'help'])->name('help');
});
you simpy needo to add the auth middleware, this way:
Route::group(function(){
Route::get('/', [HomeController::class, 'home'])->name('home')->middleware('Auth');
Route::get('/account', [HomeController::class, 'account'])->name('account')->middleware('Auth');
Route::get('/feedback', [HomeController::class, 'feedback'])->name('feedback')->middleware('Auth');
Route::get('/help', [HomeController::class, 'help'])->name('help');
})->middleware('Auth');
I have done laravel auth with is_admin field like:
if(auth()->user()->is_admin == 1){
return $next($request);
}
and defined it in route so if the user is admin, can be redirected to voyager dashboard page.
Route::group(['prefix' => 'admin'], function()
{
Voyager::routes();
Route::get('admin', 'HomeController#admin')->middleware('admin');
});
When I login, it's redirected to specific page but with error message:
Target class [VoyagerController] does not exist.
What should I do to return voyager admin page?
You can use middleware admin.user.
Route::group(['prefix' => 'admin'], function()
{
Voyager::routes();
Route::get('admin', 'HomeController#admin')->middleware('admin.user');
});
Hope this will work.
This will:
Access route only when user is logged in.
Redirect to voyager login page when accessing the restricted route.
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 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');
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.