How to correct? That's all intertwined url?
/admin/* (if auth and id user **!=** 1 redirect '/')
/admin/* (if **NO** auth redirect '/admin/login')
/admin/{page} (if auth and id user **==** 1) redirect /admin/{page}
Make a Route Group which you can call admin and and create the confirmation, then add a prefix to the route for example:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('user', function()
{
// place your routes here
});
});
Reference
Related
I'm new to php and Laravel. I'm using Laravel 9 with Fortify and some custom login ui. They are working correctly. The home page nav bar is showing the Login and Register links if user is not authenticated. The Login and Register links are working fine as well. What I'm trying to do now is to auto route user to the Login page if the user is not authenticated. So below is what I did.
In my web.php, I've this line to route all admin path to routes\admin\adminRoutes.php.
Route::prefix('/admin')->group(__DIR__ . '\admin\adminRoutes.php');
In the routes\admin\adminRoutes.php, I have this:
Route::name('admin.')->group(['middleware' => 'auth'], function () {
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/home', function () {
return redirect()->route('home');
});
});
Basically, I added the ['middleware' => 'auth'] to the group function. But for some reason, this cause the site to failed with this error that I don't understand:
require(auth): Failed to open stream: No such file or directory
What is that means and what did I do wrong?
I have https://tenancyforlaravel.com/ installed in laravel to make multi-tenant and it works fine for the web routes.
My problem is that when I access my APIs then I get a 404 error in tenant domains.
tenancyforlaravel documentation: https://tenancyforlaravel.com/docs/v3/routes
It says that I must put all my APIs inside api.php file and wrap them in a Route group with this middleware so I put all my APIs inside api.php file and all my APIs as below:
Route::middleware('tenancy')->group(function () {
Route::name('api.')->namespace('Api')->group(function () {
Route::post('/login', 'AuthController#login')->name('login');
...
});
and when I access it using sub.local.test/api/login then I get 404 error.
Tested for tenancyforlaravel.com V3 and it works OK.
Route::middleware([
InitializeTenancyByDomain::class,
PreventAccessFromCentralDomains::class
])->prefix('api')->group(function () {
//
Route::name('api.')->namespace('App\Http\Controllers\Api')->group(function () {
Route::post('/login', 'AuthController#login')->name('login');
...
});
Put all your API routes inside api.php as below
use App\Http\Controllers\AuthController;
Route::group(['prefix' => '/{tenant}',
'middleware' => [InitializeTenancyByPath::class],],
function () {
Route::post('/login', [AuthController::class, 'login'])->name('login');
...
});
As you haven't mentioned your tenant identifier, I am using path as identifier, so using InitializeTenancyByPath middleware. Use whatever identifier middleware you want in place of that.
Access your API routes normally as you used to do, with your identifier. As this example uses path as identifier, the endpoint will look like:
sub.local.test/api/{tenant}/login
As the official Laravel 5.1 docs say:
When a user is not successfully authenticated, they will be redirected to the /auth/login URI.
And, also:
When a user is successfully authenticated, they will be redirected to the /home URI, which you will need to register a route to handle.
This is the code I wrote for the /home URI:
// Home
Route::get('/home', function() {
return view('auth/main');
});
So, now, when I visit /home, or auth/main, why I'm not redirected to the login page?
Please help!
If you have main.blade.php file under auth folder try this
Route::get('/home', function() {
return view('auth.main');
});
Ok, thanks to #ujiriukiri.
I wrote this code:
Route::get('/home', ['middleware' => 'auth', function() {
return view('auth.main'); }]);
... and now it works for /home, but not for auth/main. How to solve?
I'm trying to use fetch api with laravel, but the request goes to the login url, not the given url.
for example;
route
//also this route group has auth and admin middlewares (checks user is authenticated and is admin)
Route::delete("media/delete/{id}", ["as" => "admin.posts.media.delete", "uses" => "AdminController#deletePostMedia"]);
ajax
fetch("../../media/delete/" + id, {
method: "DELETE",
headers: new Headers({
"X-CSRF-TOKEN": document.head.querySelector("[name=csrf-token]").content
})
})
it gives me the below error:
DELETE http://commerce.test/login 405 (Method Not Allowed)
as you can see above, the request goes to login url, not given url..
How can I fix this?
Laravel by default includes axios for ajax requests and it's very easy to use, have a look here.
In your example you'd write something like this:
axios.delete('/media/delete/' + id)
.then(response => {
console.log(response.data);
})
.catch(error=> {
console.log(error);
});
I have a situation. I am trying to create an application that will have only one route file, api.php for both the web app (spa) and mobile app.The problem is now that the entire application is stateless (as it should be), I can't even login. Because, routes in api.php expect a token in the request header, which I don't know how to provide.I am using vue in the front-end I have this simple strategy:<button #click="login">Login</button>and the login method looks like this:
login(){
axios.post('login',this.credentials)
.then( window.location = "http://localhost:3000/app" );
}
And my route definition:
Route::group(['middleware' => ['role:admin']], function () {
Route::get('app', function () {
return view('index');
});
});
But it redirects me back to the login page. I wish I could do something like window.header = Bearer myLongToken. I am using JWT, if that helps.
Update:
It looks like there is something else going on. If I remove the role:admin middleware, then I get redirected to desired route, but if I add the middleware, I get redirected back to the login route even if the credentials are valid.
// Route::group(['middleware' => ['role:admin']], function () {
Route::get('app', function () {
return view('index');
});
// });