Adding Route prefix in Laravel JetStream - laravel

How do I add a route prefix to authentication routes exposed by Laravel JetStream? For example, I want to move the default /login route to /api/login (and similarly /register and /logout routes). I could do this Passport through a config option, but no such things appears to be there in JetStream.

there is a simple solution but its undocumented. you just have to go to your fortify.php config file and add a path. like:
return [
.
.
.
'path' => 'api',
// rest of your config
];

Related

Default named routes relating to the route url (Laravel 9)

I was wondering if there was some default functionality in the Laravel routes file (web.php or any other route file) where you could do the following:
Route::get(
'pages/books/categories',
'App\\Http\\Controllers\\Page\\Book\\PageBookCategoryController#index'
);
And named route for the above would be pages/books/categories.
At the moment, I have to do the following for it to work as expected:
Route::get(
'pages/books/categories',
'App\\Http\\Controllers\\Page\\Book\\PageBookCategoryController#index'
)
->name('pages/books/categories');
Of course, this gives me the named route as pages/books/categories. The only reason why I want to do the above is that I have quite a few routes, and it's pretty tedious to add a named route if the URL for the route is the same.

defining route with 'auth:web' middleware in laravel service provider boot() method

im using a package called https://github.com/garygreen/pretty-routes
there is line in its service provider boot() method (here the code)
it is defining a get route with middlewares from its config file(link to the code) I just added 'auth:web' to its config file but it seems the 'auth:web' middleware is called as soon as code reaches the line before Laravel bootstraps its session and etc. when the auth('web')->user() is yet null
What I can not understand is that I do the same exact thing (here the code)with laravel/telescope but it works. why ???
also changing :
Route::get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
to :
$this->app['router']->get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
in service provider seems to solve the problem and make this code behave like the way telescope package use 'auth:web' as middleware.
what's happening ?
You need to have the web middleware applied to any routes you need sessions for, which is what the default authentication system is using. When you apply the auth middleware without this it can't possibly resolve a user since there is no session to be authenticated against.
You need to apply the web middleware and then what ever other middleware you want:
'middlewares' => [
'web', 'auth:web',
],
If you look at the telescope example you provided you will see they also add the web middleware. So you didn't quite "do the same exact thing" as the telescope config.

404 Not Found on sanctum/csrf-cookie path

So I've been building a projet based on laravel. I'm building on a SPA foundation with sanctum as my authorization package. It works perfectly. Then I deploy the project to the server and everytime I try to login there is a 404 error on /sanctum/csrf-cookie.
How could this happen? Is it because the SanctumServiceProvider not working.
The problem is when you define sanctum prefix, the route become something else like this:
you can check your routes with : php artisan route:list
as you can see the /sanctum/ is removed and when you check the route /sanctum/csrf-cookie it will not be and throws 404 error. So you have two options:
add this prefix: 'prefix' => 'api/v1/sanctum'
or
change GET call to api/csrf-cookie
You need to check if you're setting correct axios defaults for your /sanctum/csrf-cookie api call.
You can set it as follows
axios.defaults.withCredentials = true;
axios.defaults.baseURL = "http://localhost"; //Before sending get request
axios.get("/sanctum/csrf-cookie").then(async () => {
//Logic to handle login
});
If defaults are not set properly url becomes http::localhost::8080/sanctum/crf-cookie which is where frontend is serving but instead it has to be http::localhost/sanctum/csrf-cookie
Note: localhost example is just for explanation. For production server make sure your url is correct and api call is on backend.
I solved this issue by adding:
AllowOverride All
to the apache directory config
add in last line inside config/sanctum.php
'routes' => false,
Add in config/cors.php
'paths' => ['*']

Laravel sanctum change csrf cookie route

How could I change laravel sanctum csrf cookie route to /api/sanctum/csrf-cookie ?
I tried adding this to api.php routes:
use Laravel\Sanctum\Http\Controllers\CsrfCookieController;
Route::get('/sanctum/csrf-cookie', CsrfCookieController::class . '#show')->middleware('web');
But it looks for this controller under app/http/controllers where it doesn't exist.
So if anyone wondering, there should be prefix within config file that is default set to 'sanctum' within a package service provider.
So if you want to change it to API routes you should go to config/sanctum.php and add 'prefix' => 'api'.
you can create a controller CsrfController and make it extends
(Laravel\Sanctum\Http\Controllers\CsrfCookieController)
use Laravel\Sanctum\Http\Controllers\CsrfCookieController
class CsrfController extends CsrfCookieController {}
and then you can link your route
Route::get('/sanctum/csrf-cookie', 'CsrfController#show')->middleware('web');
another flexible solution is to set
'routes' => false
in config/sanctum.php ,then define a new route in routes\api.php
use Laravel\Sanctum\Http\Controllers\CsrfCookieController;
Route::get('/csrf', [CsrfCookieController::class, 'get'])->name('csrf');
like so, you can choose the proper route to suit your needs

Laravel Passport Password Reset API route

I'm all set up with Passport in 5.5 and have the auto generated Auth\ForgotPasswordController and Auth\ResetPasswordController controllers.
However whereas /oauth/token was provided magically for me, there don't appear to be such routes for password reset when using the API.
What should my API routes look like?
Currently I've experimented with
Route::group(['prefix' => 'password'], function () {
Route::post('/email', 'Auth\ForgotPasswordController#sendResetLinkEmail');
Route::post('/reset', 'Auth\ResetPasswordController#reset');
});
but I found these in the vendor files when looking at the traits and aren't sure if this is the correct way.
The /password/email route also fails with "message": "Route [password.reset] not defined."
since you don't see any route other then 2 custom, therefore i am assumin you havn't run artisan auth command. First run that. it will add lot of routes in ur project.
Then set api driver to passport.

Resources