Middleware not working i am making a page which i want that nobody can access that page without login also not through url so i am using middleware for that but its not working it will not redirecting me to the page which url i passed
can anybody solve my problem
It is always good if you share the code that you tried so someone can guide you.
okk
here's my route code
Route::middleware('newmiddleware')->group(function(){
Route::get('/dashboard', function(){
return view('dashboard');
});
});
and middleware
protected function redirectTo($request)
{
$user = User::where('name',$request->name)->first();
if($user = ''){
return route('login');
}
}
@laksh One thing is this should be:
if($user == ''){
return route('login');
}
Using $user = '' is setting $user to ''
You could use:
if(Auth::guest()){
return route('login');
}
it is also not working login page not redirecting to dashboard
@laksh You need to add else to the condition to send them to the dashboard. Right now, your just checking to see if they are a guest and if not, you're not doing anything.
if($user == ''){
return route('login');
}
return redirect()->route('dashboard');
Assuming you've named your route 'dashboard'
You should post all your code properly formatted for any help other than what has been given here.
If the user is not found, it will not return an empty string, it will return null
if (!$user) {
return redirect()->route('login');
}
return $next($request);
But there is a built in middleware for this named auth that you can just use
getting error after this
Error : Header may not contain more than a single header, new line detected
@laksh Can you show the complete middleware file?
namespace App\Http\Middleware;
use App;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class NewMiddle extends Middleware
{
protected function redirectTo($request)
{
$user = User::where('name',$request->name)->first();
if(Auth::check()){
return route('login');
}
return view('dashboard');
}
@laksh Return a redirect
return redirect()->route('login');
And dont return views from middleware. Either return $next($request); or a redirect. Views are for controllers.
Too few arguments to function App\Http\Middleware\NewMiddle::redirectTo(), 1 passed in D:\xampp\htdocs\demo\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php on line 83 and exactly 2 expected
getting error after use redirect()
@laksh Ah ok didnt see you had that. Try returning the route name then, but remove the view like I suggested.
your concept is messed up. Read the docs regarding authentication and authorisation
you are expecting the url to contain a name, and then provided the database contains that name then you are happy?
What are you trying to do exactly????
i want to pass the name of route on which middleware is using
i just wanted to do that i made a page and just wanted that the page cannot accessed by anybody before logging in
@laksh And that is what the auth middleware is for
But auth middleware doesn't redirecting me to dashboard infact it is redirecting me to login again after login
@laksh then you have messed up the standard authentication
Are you sure you are actually logged in?
@laksh If you are logged in, it will redirect you to the page you are trying to visit. If not, it will redirect you to login
here is my login code
can you please tell what's wrong in this
public function show_login_form(){
return view('maincontent.login');
}
public function process_login(Request $request)
{
$request->validate([
'name' => 'required',
'password' => 'required'
]);
$credentials = $request->except(['_token']);
$user = User::where('name',$request->name)->first();
if ($user != '') {
auth()->attempt($credentials);
return response([ [1] ]);
}else{
return response([ [0] ]);
}
}
@laksh you are doing that compare user to an empty string again...
If you are not confident in your PHP skills, the last thing you should be doing is writing your own authentication - especially as it is handled out of the box for you
how do i use standard authentication for login
@laksh if you have not done much with the project, install one of the starter kits such as breeze
public function handle(Request $request, Closure $next)
{
$user = $request->user('admin');
if (!$user) {
return redirect()->route('admin.login');
}
if (!in_array($user->role, [Roles::ADMINISTRATOR->value, Roles::STAFF->value])) {
return redirect()->route('admin.home');
}
return $next($request);
}
This is my middleware for admin page. I think you just make it looks more complicated
@huyvuong your doesn't really make any sense either. Middleware should be used as a guard with the exception causing perhaps a redirect, and normal flow through the middleware.
@Snapey
Thank you for your advice. I think I have to fix mine too.
Please sign in or create an account to participate in this conversation.