add routes after login page for user - laravel

I use backpack 4 for laravel(laravel 8)and can't write a route for the user page. After login user has to get /user/dashboard or /dashboard, but the user is redirected to /admin/dashboard. How can I solve this problem?
web.php
Route::get('/', function() {
return redirect()->route('backpack.auth.login');
});
Route::get('/user/dashboard', [DashboardUserController::class, 'index'])->middleware(['web','admin']);

If your users won't ever need to use Backpack and should only able to view the dashboard, you could add a new middleware to Backpack's set of middlewares that checks if your user is an admin or a normal user to redirect them if they are not an admin.
Because Backpack registers the login routes itself, you can't just add your middleware to the route on registration. Instead you'll need to add your middleware to Backpack's admin middleware group. That's the one that gets added to all admin routes.
Steps:
Create the new middleware:
php artisan make:middleware RedirectToDashboard
Open the new middleware at app/Http/Middleware/RedirectToDashboard.php
Change the handle method to look something like this:
public function handle(Request $request, Closure $next)
{
if($request->user()->isNormalUser()) { // You need to change this to the check if the user is a normal user, that's specific to your application
return redirect()->to('dashboard'); // 'dashboard' is the name of your route, you may need to change it.
}
return $next($request);
}
Add the new middleware to Backpack's default set of middlewares in config/backpack/base.php. Open the file and look for the middleware_class entry (most likely somewhere around line 220). Add \App\Http\Middleware\RedirectToDashboard::class to the array.
Log into Backpack as a normal user and check if you get redirected.

The /admin prefix comes from the file: config/backpack/base.php.
Remove route_prefix from the file.
Your users will be redirected to /dashboard.

Related

Trying to show the current signed in users profile. What am i doing wrong | Laravel 9

Trying to show the current signed in user's profile. What am i doing wrong.This is the function on the controller. I'm using Laravel 9
public function show(User $user)
{
return view('users.index', with('user', $user));
}
This is the routes
Route::resource('users', UsersController::class)->middleware('auth');
And my generic layout page
<x-dropdown-item href="/users/{{ auth()->user()->username }}" >Account</x-dropdown-item>
When i click the link i get user not found.
You're utilising route model binding which unless configured otherwise, requires you to provide a route with a model id. You're providing it with a username, so Laravel is throwing a 404 because it can't locate the relevant record in the database.
If you replace username with id, the binding should work.
<x-dropdown-item href="/users/{{ auth()->user()->id }}">
Account
</x-dropdown-item>
The code is fine. Are you sure you have a route with username, resource routes are using id.
To access model like that you need to specify id in the route like:
/users/{id}
However what you are trying to do here is access the logged in user model which you can access with the facade Auth::user();
You can access the user any time any where in the code and there is no need to pass it to an anchor link unless sending to an external system.

How to redirect in laravel jetstream?

I am using laravel 8 with jetstream I want to know that how can I direct different users like admin or user to different routes?
RouteServiceProvider has Public const Home = '';
but it only direct to a one route
I hope this can help this is how I solve the problem
I created a new view for admin "admin/index.blade.php"
Then I assign the role of a developer to the admin, then I use this in my route
Route::middleware(['role:developer'])->get('/admin', function () {
return view('admin.index');
})->name('admin');

Laravel guest middleware

I have a page in my web app that should be blocked from logged in users. Similar to how a login or register page should not be accessible to already logged in users. I accomplished this by using guest middleware in controller constructor.
$this->middleware("guest")->only("page_action"); // like this
In this setup, if logged in user tries to visit that page they get redirected to home page. But I need to show a 404 not found page instead of redirecting. How can I do that?
In short, how can I make a page accessible to guest only and make it look like it does not exist to logged in users?
The guest logic is inside inside App\Http\Middleware\RedirectIfAuthenticated.
if (Auth::guard($guard)->check()) {
abort(404)
}
return $next($request);
Otherwise, you need to create a new middleware like #Atiqur suggested.
In your method just check if the user is loggedIn, if then abort to 404 like below...
if(\Illuminate\Support\Facades\Auth::check()) {
return abort(404);
}
#Rest of the code is for guest user.....
#

Laravel 5.2 Session not passing

I have a simple success message on store to DB.
\Session::flash('info', 'Success! Words created');
now if I var_dump the session and return that, great.
As soon as I move to another view. Session info is gone!
I've tried all sorts, I'm on laravel 5.1.
looked into the middleware groups but i just get blank pages when adding routes in here..
This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errors variable available to all your views is not being utilized because it was moved from the global middleware to the web middleware group.
There are two ways to fix this:
In your kernel.php file(app/Http/Kernel.php), you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.
Wrap all your web routes with a route group and apply the web middleware to them:
Route::group(['middleware' => 'web'], function() {
// Place all your web routes here...(Cut all `Route` which are define in `Route file`, paste here)
});

How to use intended function in Laravel

I need to register in a tournament with the URL:
http://laravel.dev/tournaments/1/register/
This URL is in the middleware 'auth', so if the user is not logged, he is redirected to login / page.
What I need is to redirect to
http://laravel.dev/tournaments/1/register/
After login.
In my routes.php, I have:
Route::get('tournaments/{tournamentId}/register', 'TournamentController#register');
I was told to use
redirect()->intended
but I don't know how to do it.
In the general case, User will be redirected to /admin, but in this case, I want him to keep doing his main action ( Register tournament)...
I'm using the built in trait for login, so I checked what system do when login and it is already using this function:
protected function handleUserWasAuthenticated(Request $request, $throttles)
{
if ($throttles) {
$this->clearLoginAttempts($request);
}
if (method_exists($this, 'authenticated')) {
return $this->authenticated($request, Auth::guard($this->getGuard())->user());
}
return redirect()->intended($this->redirectPath());
}
Thing is it will redirect me to a default path, not a dynamic one...
You should to know different between Redirect To and Redirect Intended
Redirect Intended: redirects the user to where they were originally going
Redirect To: Redirect the user to the page **YOU** specify them to go.
Things to check:
Use your browser's inspect element network feature to trace redirects - there may be several, which could help clear up confusion.
The intended() method requires a call to guest() when redirecting previous to the former. This happens in the Authenticate middleware, but
if you're using some other middleware (such as a middleware to catch and redirect admins to an admin area), this might be triggering first and redirecting without using guest().
Does your controller using the AuthenticatesUsers trait implement the authenticated method? If so this will be returned instead of redirect()->intended().

Resources