I'm attempting to make a User System from Laravel, and I have a plan to give certain users "BETA Tester" role so that they can access the beta side of the site.
However, I am unsure if this is even possible and how I would even go about doing it.
The sort of plan i'm looking for is 'Navigation To Beta Section Of The Site > Big Log In [SKIP IF ALREADY LOGGED IN] > Check If The User Has The "BETA Tester" role > If Yes Send Them To The Beta Site / If No Tell Them They Do Not Have Access'
Is this possible?
Create a custom middleware class that checks the user's role:
<?php
namespace App\Http\Middleware;
use Closure;
class Checkrole
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
// hasRole being a function defined on your User model that checks
// a user's assigned role(s).
if (auth()->check() && auth()->user()->hasRole('BETA Tester')) {
return $next($request);
}
abort(401, 'You are not allowed to access this page');
}
}
Related
I am trying to make the login redirect when loggin in successful not to home when a variable called estado = 2 but to the reset password view, not because the login fails but because when the variable estado of the user equals 2 that means that it has the default password and I want to make the first time the user logs to change his password because of security reasons. (Default password is the same as the username thats why I plan to make mandatory to change the password the first time you log in).
I'm new in Laravel so not sure where I need to do the changes to manage to do that but I think its in the middleware RedirectIfAuthenticated.
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param string|null ...$guards
* #return mixed
*/
public function handle(Request $request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
}
return $next($request);
}
}
If I didn't make clear with what I need I can explain it better, english is not my main language so sorry for any issues. After I change the password with the forgot me functionality I make the estado variable change to 1 and I plan to resue it for this so that in future logs in of the user it can log in like normal and go to home.
The line:
return redirect(RouteServiceProvider::HOME);
Home is a constant /home
You can add if condition on this line:
If Auth::user()->estado == 2
return redirect(“your reset password route”);
However a better way to approach this is to create a new middleware and put this condition in it and protect all the authenticated routes with this middleware.
I want to create a system login, that direct the regular users to their profiles, and direct the admin to the dashboard, using the same logging form is that possible in laravel??
i was looking for a solution for that problem, if any one have any idea how to do it??
Assuming you have used Laravel's default auth scaffolding, you can customize how the user is redirected after logging in by modifying the authenticated method in your LoginController :
/**
* The user has been authenticated.
*
* #param \Illuminate\Http\Request $request
* #param mixed $user
* #return mixed
*/
protected function authenticated(Request $request, $user)
{
if ($user->hasRole('admin')) { // If using spatie/laravel-permissions
return redirect()->route('dashboard');
}
return redirect()->route('profile', ['user' => $user]);
}
I am new to laravel and trying to make a panel for food delivery
I have used Laravel default Registration and Login for User Category--Restaurant
and then after user login , the user can Add restaurant details using route (/add_details)
once the user has added restaurant details the user should not be able to go to that route (/add_details)
this will depend on a column in restaurant table (is_verified)
how do i check that
I was thinking of using a Laravel middleware
but then i was stuck how laravel middleware $request variable works
how can i get column value in middleware and verify it
or if any other simple but effective solution
as
i will be using it in sidebar.blade.php as well
so that i can hide the menu
I made a middleware and added it to kernel.php and is using it in routes
Its working fine
but i want to ask is this the right way i have done it
Route::get('/manage_cuisines', 'RestaurantCuisineController#create')->name('manage-cuisines')->middleware('restaurant_verified');
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
use \App\User;
use \App\Restaurant;
class CheckRestaurantVerification
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$restaurant = Restaurant::find(User::find(Auth::id())->restaurant_id);
if($restaurant->is_verified == 0)
{
return redirect('home');
}
return $next($request);
}
}
I want my website visitors' to redirect to the intended url after login. Suppose, I've a protected page dashboard. If any guest tries to access to dashboard, he get redirected to login page. Now, Instead of login he visited other pages and then returned to login page intentionally. How can I redirect him to the previously intended dashboard page after this login?
It would depend on how you're handling the login action, for example if using the RedirectIfAuthenticated middleware you can make the following change:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param string|null $guard
* #return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
// return redirect('/home');
return redirect()->intended('dashboard');
}
return $next($request);
}
}
This will redirect the user to whatever page they intended on accessing and send them to /dashboard by default. You could further extend this by using a different fallback URL depending on the users role in case guests or administrators need to land at a different page after login.
I'm quite new to Laravel and have been stumped on a problem for 2 days - I'd be grateful for some guidance.
I'm using the default out-of-the-box User authentication system with Laravel 5.3. A new user is created automatically behind the scenes by an existing Admin user - I will in time hide the user registration page. I have also successfully set up middleware to check if a user is newly registered (by looking for a null 'last_logged_in_date' that I've added to the migration).
All I want to happen is for a new registered user to be redirected to the password reset screen that ships with Laravel (again, in time I will create a dedicated page). I would like this to happen within the middleware file. So far, my middleware looks like this:
<?php
namespace App\Http\Middleware;
use Closure;
use App\Http\Controllers\Auth;
class CheckIfNewUser
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$user = $request->user();
if (! is_null($user->last_logged_in_date )) {
return $next($request);
}
// This is where I'm stuck!!!
}
}
I'm not sure what code to enter at the location indicated by the comments above. I've tried sendResetLinkEmail($request); etc and have imported what I though were the correct classes but I always end up with a Call to undefined function App\Http\Middleware\sendResetLinkEmail() message irregardless of what I 'use' at the top of my class.
Where am I going wrong? Thanks!
Well that happens because you have not defined your sendResetLinkEmail($request) function yet. You can do it like this, or you can create a new class with that and then call the class.
Call the trait SendsPasswordResetEmails and then access it with $this since traits are not classes and you cannot access their members directly.
<?php
namespace App\Http\Middleware;
use Closure;
use App\Http\Controllers\Auth;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
class CheckIfNewUser
{
use SendsPasswordResetEmails;
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$user = $request->user();
if (! is_null($user->last_logged_in_date )) {
return $next($request);
}
// This is where I'm stuck!!!
//EDIT
//return $this->SendsPasswordResetEmails->sendResetLinkEmail($request);
return $this->sendResetLinkEmail($request);
}
}