Redirect to custom tool url in a laravel nova application - laravel

Laravel Version: 7
Nova Version: 3
PHP Version: 7.4
Database Driver & Version:
Operating System and Version: MacOS
Browser type and version: Chrome
Reproduction Repository: https://github.com/###/###
Description:
I am trying to redirect a user after checking for a condition in a middleware, I have tried to use
return redirect('/admin/paddle-subscription');
But it does not work, it just redirects the page and shows it right in the browser url but show a 404 error in the screen, it looks that as nova uses vue.js it is creating some kind of errors.
I have been reading some post in laracast https://laracasts.com/discuss/channels/nova/nova-resource-route-redirect and it is clearer to me that it requires a special way to redirect.
I have tried
return redirect()->action('\Laravel\Nova\Http\Controllers\RouterController#show', ['resource' => '/admin/paddle-subscription']);
And it does not work, how I am supposed to redirect properly?
Thanks
Detailed steps to reproduce the issue on a fresh Nova installation:

You can do this by creating the route in the backend first so that Laravel knows the page exists. Use Nova's RouterController#show action for the route like in your Laracast example and make sure to apply all the Nova middleware to it.
In app/Providers/NovaServiceProvider.php add a custom route to your routes() method:
protected function routes()
{
Nova::routes()
->withAuthenticationRoutes()
->withPasswordResetRoutes()
->register();
Route::domain(config('nova.domain', null))
->middleware(['web'])
->as('nova.')
->prefix(Nova::path())
->group(function () {
Route::get('/admin/paddle-subscription', [\Laravel\Nova\Http\Controllers\RouterController::class, 'show'])
->middleware(config('nova.middleware', []));
});
}
In your middleware you can now redirect to that route and the page will render properly:
public function handle(Request $request, Closure $next)
{
if ($foo === $bar) {
return new \Illuminate\Http\RedirectResponse('/admin/paddle-subscription');
}
return $next($request);
}

try this:
add in your nova action
public function handleRequest(\Laravel\Nova\Http\Requests\ActionRequest $request)
{
parent::handleRequest($request);
return Action::redirect('/admin/paddle-subscription');
}

Related

LImit Access on pages. to prevent access pages without login

as we know when we code on localhost we can go directly to dashboard admin on our website without login first by typing the link. so how we can stop that? so if we want to access the admin dashboard we really have to log in first
use laravel middleware to limit accesses ... you can use auth middleware like:
Route::get('/profile', function () {
//
})->middleware('auth');
for more info visit laravel docs
use laravel middleware in your web.php if you are using a simple function for your route
Route::get('/admin/dashboard',function () {
return view....``
})->middleware('auth');
Or you can use a constructor in your Controller to limit access for all function in this controller
public function __construct()
{
$this->middleware('auth');
}

Google2FALaravel Authenticator always return True in Laravel 6

There is a problem using the Google-Authenticator Module "PragmaRX\Google2FALaravel" in my Laravel6 project.
I installed it following the manual on the github page. Setting up 2FA-Users via QRcode works like a charm, but the authentication middleware always returns "True" for authenticated, regardless if the user has passed the 2fa challenge or not.
public function handle($request, Closure $next)
{
$authenticator = app(Google2FAAuthenticator::class)->boot($request);
if ($authenticator->isAuthenticated()) { **//always returns true**
return $next($request);
}
return $authenticator->makeRequestOneTimePasswordResponse();
}
I assume it has something to do with the "CARTALYST/Sentinel" package i am using (instead of the built-in laravel "Auth" Manager), someone experienced similar behaviour and knows how to fix this?

Laravel nova - redirect from Dashboard

I would like to remove dashboard from my Laravel Nova app.
I found it easy to remove it from sidebar-menu - simply comment /views/dashboard/navigation.blade.php code.
However, I want to add a redirection logic (landing page depends on user role) so when navigating to / user will be redirected to a resource or tool which corresponds him.
(I have already implemented a redirection after login (https://stackoverflow.com/a/54345123/1039488)
I tried to do it with cards, but looks like this is not the right solution.
Any idea where can I place the redirection logic?
Nova 4; You can override the initialPath like so:
class NovaServiceProvider extends NovaApplicationServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
parent::boot();
Nova::initialPath('/resources/users');
}
// ...
}
This way, you get redirected to the Users resource upon logging in.
Pre nova 4 method:
To NovaServiceProvider.php add to boot method:
Nova::script('menuFix', __DIR__.'/../../resources/js/fixMenu.js');
Create file fixMenu.js with following:
if (location.pathname == '/' || location.pathname == '/dashboards/main'){
location.href = '/whereToRedirect'
}
A cleaner and safe way for Nova 3.x or below:
Copy vendor/laravel/nova/resources/views/layout.blade.php to resources/views/vendor/nova/
Now open resources/views/vendor/nova/layout.blade.php and edit it
Replace this line with the code below window.Nova = new CreateNova(config);
window.Nova = new CreateNova(config);
window.Nova.booting((Vue, router, store) => {
/** This fixes showing an empty dashboard. */
router.beforeEach((to, from, next) => {
if (to.name === 'dashboard.custom') {
next({ name: 'index', params: { resourceName: 'users'}});
}
next();
});
});
Replace users with your entity name's plural like products
Now save the file and refresh the nova dashboard and you should see the new page.
The solution was taken from here with clear steps.
The solution may also work for 4.x, but I haven't checked it yet.
Happy Coding :)
Just figured this out myself. In your Routes/web.php file, add a redirect route:
Route::redirect('/','/resources/{resource_name}');
where {resource_name} is the plural form of the resource. For example, '/resources/posts'.
In your case, you may want to redirect to your own control file, where the redirect logic can be placed.
Route::get('/', 'YourController#rootRedirectLogic');
Then in the controller YourController, add the method:
public function rootRedirectLogic(Request $request) {
// some logic here
return redirect()->route('YourRoute');
}
where 'YourRoute' is the name of the route you want to send the user to.
(Found clues to this solution in a comment by dillingham here: https://github.com/laravel/nova-issues/issues/393)
i came across this link : Laravel Nova - Point Nova path to resource page
Not sure it's a permanent solution but editing LoginController.php will do.
public function redirectPath()
{
return Nova::path().'/resources/***<resource_name>***;
}
**change to your own resource name

Trying to get intended url in LoginController in Laravel 5.6

I'm trying to capture the intended url in my LoginController so I can execute some logic in a showLoginForm() method I added to the controller so I can send the user to a specific view based on the intended URL.
I've tried the following and I cannot get it to work:
public function showLoginForm()
{
$intededUrl Session::put('url.intended', URL::full());
// my base application url is http://www.websites.com:8080
if (starts_with($intededUrl, url('/admin'))) // i want all routes that begin with http://www.websites.com:8080/admin to go here
return view('auth.login');
return view('themes.'.env('APP_THEME', 'mango').'.auth.login'); // but it keeps taking me here
}
I'm using Laravels starts_with() method to try and match the start of the url string.
I just figured it out. I needed Session::get('url.intended');

VueJS2 and Laravel Auth (and history mode)

I'm creating a backend system that I want to use VueJS2 for with a Laravel backend. The problem i'm facing at the moment is integrating this all together while keeping the 'tidy' URLS (no hashbangs) utilising Vue's history mode:
const router = new VueRouter({
routes,
mode: 'history'
})
I am a fan of the way Laravel handles Authentication and as such i've tried to use that in this system as an entry point into the SPA but doing so breaks the history mode.
In my routes file i've added a Vue capture route that works to allow hard refreshing of the browser and back button etc and this works fine:
Route::get('/{vue_capture?}', function () {
return view('home');
})->where('vue_capture', '[\/\w\.-]*');
However in order to use Laravel Auth i've added the following check, which works to force you to login before accessing anything else but breaks the history mode:
if (Auth::check()) {
Route::get('/{vue_capture?}', function () {
return view('home');
})->where('vue_capture', '[\/\w\.-]*');
}
else {
// the home controller has the auth middleware in the __construct
Route::get('/', 'HomeController#index');
}
I've tried adding middleware onto the end of the vue capture route but it didn't seem to do anything.
Many thanks!
To get SPA working with any backend, you should use Api authentication based on JWT.
This is Laravel docs about it: https://laravel.com/docs/5.3/passport
This is good package for this purpose: https://github.com/tymondesigns/jwt-auth

Resources