There is quite a major change between laravel 5.2 from 5.1
My side doesnt redirect to the auth page when the page was trying to be directly accessed.
Route::group(['middleware' => ['web']], function () {
Route::auth();
Route::get('/', 'HomeController#index');
Route::get('projects', 'ProjectController#index');
Route::post('projects', 'ProjectController#create');
Route::get('todo', 'ToDoController#index');
Route::post('todo', 'ToDoController#create');
});
Add auth middleware to your route
Route::get('projects', 'ProjectController#index')->middleware(['auth']);
Or you can add middleware in your controller
public function __construct()
{
$this->middleware('auth');
}
Related
i have a project that uses API and Web routes
web.php:
Route::middleware(['auth:sanctum',config('jetstream.auth_session'),'verified'])->group(function () {
Route::get('/admindashboard', function () { return view('dashboard');})->name('dashboard');
});
the above is working fine and i can call auth()->user() inside the blade
but here in
api.php:
Route::prefix('orders')->as('orders.')->controller(OrderController::class)->group(function(){
Route::get('index', 'index')->name('index');
});
there is no middleware and i can't use auth()->user() inside its blade
how can i wrap the above routes (in api.php) with middleware so i can use auth in balde?
Actually, it's the same middleware that you can use in apis as well.
Route::middleware('auth:sanctum')->group(function () {
// Authenticated Routes in api.php
Route::get('/user', function ()
{
return auth()->user();
});
});
How do I set the homepage (/) to the login screen in Laravel 5.3?
I have a routes file :
Route::get('/', function () {
return view('welcome');
});
I have set up the basic auth scaffolding with the command php artisan make:auth and have set up my db tables too.
But I'm struggling to understand how to set the homepage to always go to the login screen if the user is not authenticated? Surely this is just me being stupid right?
I just needed to specify the middleware('auth') for my route:
Route::get('/', function () {
return view('home');
})->middleware('auth');
Route::get('/home', 'HomeController#index');
This way if you're not logged in it will redirect to login automatically.
You can do it like this:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
return view('welcome');
});
});
Just put all the routes that needed authentication inside that middleware group.
in laravel in general you can change the url view path to what you want as example
Route::get('/', function () {
return view('auth.login');
});
In laravel 5.4 you can modify the route as
Route::get('/', 'Auth\LoginController#showLoginForm');
Per laravel doc, I can add the auth middleware as follows:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
I've also seen middleware added as follows:
Route::group(['middleware' => ['web']], function() {
// Uses all Middleware $middlewareGroups['web'] located in /app/Http/kernel.php?
Route::resource('blog','BlogController'); //Make a CRUD controller
});
How can I do both?
PS. Any comments providing insight on what the bottom four lines of code are doing would be appreciated
To assign middleware to a route you can use either single middleware (first code snippet) or middleware groups (second code snippet). With middleware groups you are assigning multiple middleware to a route at once. You can find more details about middleware groups in the docs.
To use both (single middleware & middleware group) you can try this:
Route::group(['middleware' => ['auth', 'web']], function() {
// uses 'auth' middleware plus all middleware from $middlewareGroups['web']
Route::resource('blog','BlogController'); //Make a CRUD controller
});
You may also assign multiple middleware to the route:
Route::get('/', function () {
//
})->middleware('first', 'second');
Reference
You could also do the following using the middleware static method of the Route facade:
Route::middleware(['middleware1', 'middlware2'])
->group(function () {
// Your defined routes go here
});
The middleware method accepts a single string for one middleware, or an array of strings
for a group of middleware.
Route::middleware(['auth:api'])->middleware(['second:middleware'])
->prefix('yourPrefix')->group(function () {
//Add your routes here
});
Laravel 5.3 newb here. I want going to localhost:8000 to go to the login page generated by php artisan make:auth.
My routes web.php looks like this by default:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
This of course brings it to the default Laravel welcome page.
I change it to this:
Route::get('/', function () {
return view('home');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
Which brings up the dashboard saying You are logged in! when no authentication has been done, so that isn't working.
I change it to this and nothing loads at all:
Route::get('/', function () {
Auth::routes();
Route::get('/home', 'HomeController#index');
});
Have tried a few other things and nothing seems to work. Would also like the URI to just be localhost:8000 and not localhost:8000/login or anything like that if possible.
Any suggestions?
About 5 minutes after posting this, I realized I needed to do this:
Route::get('/', function () {
return view('auth.login');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
I'm trying to work out if it's possible to use the regular Laravel Authentication routes with blade/views for basic Auth then load the SPA (Vue.js with it's own router) and make calls to the API via Dingo?
At the moment I have this at this top of my routes.php, which works:
// All routes through web middleware
Route::group(['middleware' => 'web'], function () {
// Authentication
Route::auth();
// Authenticated routes
Route::group(['middleware' => 'auth'], function () {
// Load main SPA
Route::get('/', 'AppController#spa');
});
});
app.domian.com/ is protected with Auth and thats the route which the SPA uses. I use the standard, built in, Laravel Auth pages (non SPA) so when the user is logged in or registered it allows access to the home route and loads the SPA.
What I would like to do is use Dingo from this point onwards. So calls to app.domian.com/api/* are all handled by Dingo.
I've added this to the same routes file:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function($api) {
$api->group(['middleware' => 'auth'], function ($api) {
// API prefix: api
$api->get('user', function($api) {
return Auth::user();
});
});
});
This doesn't seem to work.
It is even possible to use Dingo in this way or do I have to forfeit the built in Auth for something like JWT. I'm hoping to do that in the future, but for now I just need to get this working.
you need to replace auth with api.auth in middleware.
$api->group(['middleware' => 'api.auth'], function ($api) {
// API prefix: api
$api->get('user', function($api) {
return Auth::user();
});
});