Laravel 5 Routing with Auth - php

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;
}
?>

Related

Laravel 8 - How to redirect to Login page when not logged in

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');

laravel 5.5 error in routes - InvalidArgumentException Route [login] not defined

i replace my web.php whit this code, same as my code in laravel 5.2, now im using laravel 5.5, i dont have any errors in 5.2 version.
Route::get('/home', function () {
return view('home');
});
Route::get('/register', 'registerController#index');
Route::post('/register', 'registerController#register');
Route::get('/signin', 'signinController#index');
Route::post('/login', 'signinController#login');
Route::get('/logout', ['uses'=>'signinController#logout'])->middleware('auth');
Route::get('/profile', ['uses'=>'profileController#index'])->middleware('auth');
Route::get('/updateprofile', ['uses'=>'profileController#updateprofile'])->middleware('auth');
Route::post('/updateprofile', ['uses'=>'profileController#updateprofilesave'])->middleware('auth');
Route::post('/updateprofiles', ['uses'=>'profileController#updatechannelart'])->middleware('auth');
Route::get('/changepassword', ['uses'=>'profileController#indexpassword'])->middleware('auth');
Route::post('/changepassword', ['uses'=>'profileController#changepassword'])->middleware('auth');
Route::get('/article', 'articleController#index');
Route::get('/searchuser', ['uses'=>'searchController#index']); //Untuk searching user
Route::get('/searchuserpage', ['uses'=>'searchController#searchuser']); //searching user jquery
Route::get('/photos', ['uses'=>'documentationController#indexphoto'])->middleware('auth');
then i try to access url /profile which means need authenticate first, and it show me an error InvalidArgumentException Route [login] not defined. how to solve this problem. thankyou
this is my code for Authenticate.php
public function handle($request, Closure $next)
{
if(Auth::Check()){
return $next($request);
}
else{
return redirect('/signin');
}
}
The issue comes from the fact that somewhere in your code upon instantiation you're referring to a named route called 'login' but it's not defined in your web.php file.
An example of hitting this issue is you may have a redirect pointing to this route somewhere tucked away in one of your controllers, for example:
return redirect()->route('login');
To fix this issue, apply the name to the applicable route.
Route::post('/login', 'signinController#login')->name('login');
when you call a route in your project you must define route name .
such as :
<form action:"{{route('login')}}" method="post">
and in route :
Route::post('/signin', 'signinController#index')->name('login')
This is issue with the named routes. Please make sure which all places the named routes is being used.
Route::get('/signin', 'signinController#index')->name('login')
Here, you can see I named this route login and I can call this route anywhere using route('login') helper method.

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 keeps logging out

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.

Laravel 5 multiple "dashboard" route for admins and managers

(I have asked this question before, and did find a way around which worked for a while, but soon got out of hand. So now I am looking for a perfect solution to achieve this, the Laravel way.)
The problem is pretty simple, when admin goes to http://example.com/dashboard they should see the admin dashboard and when manager goes to the same link he should see the managers dashboard.
The way around that I used before was to call the pagecontroller and then depending on the user role call the relevant admin's or manager's dashboard controller.
// ROUTES.php
Route::group(['middleware' => 'auth'], function () {
Route::get('dashboard', 'PagesController#dashboard');
Route::get('users', 'PagesController#manageUsers');
});
// PagesController
public function dashboard(){
if($this->user->isAdmin()){
return $controller = app()->make('App\Http\Controllers\Admin\dashboard')->index();
}
if($this->user->isManager()){
return $controller = app()->make('App\Http\Controllers\Manager\dashboard')->index();
}
}
Now the problem with this approach is that I can no longer call the middleware on the dashboard controller because the process doesn't involve calling the kernel.
I am pretty sure there must be a way to achieve such a basic feature, I would be really grateful if someone can sort this out.
I think the reason this isn't a Laravel default feature because it kinda goes counter to what routes are supposed to represent. In other words, this isn't the Laravel Way. Why not redirect the user depending on their role?
// ROUTES.php
Route::group(['middleware' => 'auth'], function () {
Route::get('dashboard', function() {
if($this->user->isAdmin())
return redirect('/dashboard/admin');
if($this->user->isManager())
return redirect('/dashboard/manager');
return redirect('/home');
});
Route::get('dashboard/admin', 'Admin\dashboard#index');
Route::get('dashboard/manage', 'Manager\dashboard#index');
Route::get('users', 'PagesController#manageUsers');
});

Categories