Laravel 5.4 session doesn't seem to persist after Auth::login - laravel

Despite many people with this problem, the solutions I've found are not working. I'm just trying to use Augh::login($user) and then redirect. I've verified immediately after calling login() that the user is being logged in, however, once I redirect, I get bounced back to the login scree. Here are my routes:
Route::group(['middleware' => ['web']], function() {
Auth::routes();
Route::get('/', function () {
return view('welcome');
});
Route::get('/dashboard', 'admin\adminRootController#dashboard')->middleware('auth');
});
I've spent about 5 hours on different solutions to absolutely no end, someone please help!!!!!!!

Refactor your routes to this:
Auth::routes();
Route::group(['middleware' => ['web']], function() {
Route::get('/', function () {
return view('welcome');
});
});
Route::group(['middleware' => ['web', 'auth']], function() {
Route::get('/dashboard', 'admin\adminRootController#dashboard')
});
Auth::routes() need not be placed in any route groups as it is configured by the framework already. If you want to protect any routes, make sure to use auth middleware in your routes.

Related

Laravel 6: disable all routes for guest except home and login

I need to disable all routes for guests in Laravel except '/' and 'login' pages.
Does that possible to implement it routes/web.php ?
Yes. In your routes/web.php file, make sure to define your protected routes under the auth middleware group.
routes/web.php
Route::get('/', function() {
// / route
});
Route::get('/login', function() {
// login page
});
Route::middleware(['auth'])->group(function () {
// define your routes here
// they'll be protected
});
Official documentation
Since Laravel 7.7 you can use excluded_middleware property eg:
Route::group([
'excluded_middleware' => ['auth'],
], function () {
Route::get('/', 'HomeController#index');
...
});

How to make exception to my Route in laravel, for my spa app?

I've developed an app for my homework and It's base on Vue.js, Laravel, base on tutorials, my problem is base on tutorial I've learned, I've wrote this app and now i can't access any route except my app.
I want to create a about page, and when i add route to route it's going to my default page of spa app, which, I've did it base on tutorials to prevent people to type nonsense in URL like url.com/asdasdqwe, so how to add exception to the line that's preventing me to access other routes?
Here's the code:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// I know this line make my app to force it to don't access other routes like bottom('/about')
// Route::get('{path}',"HomeController#index")->where( 'path', '([A-z\d-\/_.]+)?' );
// Because of top code i can't access other views that's using ('/etcroute')
Route::get('/about', function () {
return view('welcome');
});
So when i remove {path} line i'll get some problem in SPA app. so I'm looking for a way to add exception or force it let these routes work with {Path} line.
Welcome to StackOverflow :)
To resolve this, simply place your '/about' route above the one with the regex match.
Laravel processes routes in order that they are listed, so your '/about' route is not being seen because the other one is matching it first.
For example:
Auth::routes();
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('welcome');
});
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
Route::get('{path}', 'HomeController#index')->where('path', '([A-z\d-\/_.]+)?');
Route::get('dashboard/{any}', [HomeController::class, 'index'])->where(['any' => '.*']);
Change your routings sort like this:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// Because of top code i can't access other views that's using ('/etcroute')
Route::get('/about', function () {
return view('welcome');
});
Route::get('{path}',"HomeController#index")->where( 'path', '([A-z\d-\/_.]+)?' );
Laravel routing begins from the top of web.php files and checks the routings one by one.
So you have to add regex at the end of your static routes.

Laravel: Whats the difference between these two route api approach

This
Route::middleware(['cors'])->group(function () {
Route::post('/login', 'AuthController#APIstore');
Route::middleware(['auth:api'])->group(function () {
Route::post('/logout', 'AuthController#APIdestroy');
Route::get('/projects', 'ProjectController#getAll');
});
});
And this
Route::group(['middleware' => 'cors'], function() {
Route::post('/login', 'AuthController#APIstore');
Route::group(['middleware' => 'auth:api'], function() {
Route::post('/logout', 'AuthController#APIdestroy');
Route::get('/projects', 'ProjectController#getAll');
});
});
On the first code, CORS middleware works with /login but does not work for /logout and /projects
On the second code, the CORS middleware does not work at all
is there a reason behind this?
So, as per the Laravel Routing Doc, the top level middleware is applied to all groups in the group. So using Route::middleware(['cors']) will mean this middleware will be applied to Route::middleware(['auth:api']).
However Route::group(['middleware' => 'cors'] is a group route not a middleware route, so the middle is not applied to child groups.

Laravel on Ubuntu with Apache2 not working

i tried to configure laravel for apache2.
But if I open http://localhost/ it redirect me to http://localhost/login and there is nothing to display.
If I try http://localhost/index.php/login I get the view blade to login. But how can I remove the /index.php/
The Apache vHost config
And the "main" route
Route::get('/home', function() {
return Redirect::to('login');
});
Route::group([], function() {
Route::get('/', function() {
return Redirect::to('dashboard');
Route::get('galleries', 'GalleryController#getGalleries');
Route::get('galleries/{GID}', 'GalleryController#getPicuteGallery');
Route::get('news', 'NewsController#index');
Route::get('dashboard', 'DashboardController#index');
Route::get('search', 'Search\SearchController#index');
Route::get('calendar', 'CalendarController#index');
Route::get('symposium', 'SymposiumController#index');
Route::get('conference', 'ConferenceController#index');
Route::get('publication', function() {
return view('publication');
});
});
The Auth routes are predefined by AdminLTE installer.
/**
* Define the AdminLTETemplate routes.
*/
protected function defineRoutes()
{
if (!$this->app->routesAreCached()) {
$router = app('router');
$router->group(['namespace' => $this->getAppNamespace().'Http\Controllers'], function () {
require __DIR__.'/../Http/routes.php';
});
}
}
And the AdminLTE Router
<?php
/*
* Same configuration as Laravel 5.2:
* See https://github.com/laravel/framework/blob/5.2/src/Illuminate /Auth/Console/stubs/make/routes.stub
*/
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get('/', function () {
return view('welcome');
});
});
---UPDATE---
I found out that the mod_rewrite wasnt enabled. Now the web site works fine.
But I need to add the 'middleware' => 'web' class
Remove ['middleware' => 'web'] from you routes.php, because since Laravel 5.2.27 this middleware applies automatically to all your routes and if you're adding it manually, it may give you broken app.
UPDATE:
You have a function which redirects you to 'dashboard' (why?) and also the function is not closed. So try to remove these lines:
Route::get('/', function() {
return Redirect::to('dashboard');

Protect routes with middleware Laravel

I have implemented middleware roles and permissions control in my app, but I cannot figure out why it only allows me to define one '/' route. The second one is still pointing to '/home' even though I override AuthController redirectTo variable.
My routes:
Route::group(['middleware' => 'role:user'], function()
{
Route::get('/', 'ScoresController#user');
});
Route::group(['middleware' => 'role:admin'], function()
{
Route::get('/', 'PagesController#home');
});
In any case after authentication user with user role redirecting to '/home'.
Like Simon says your second route will override the first one what you could do is load another controller wich redirects you to another page via redirect()
or write it as a route itself.
Could look like this:
Route::get('/', function(){
$user = Auth::user();
if($user->is('admin') {
return redirect('admin');
}else{
return redirect('user');
}
});
Route::get('admin', ['middleware' => 'role:admin', 'uses'=> 'PagesController#home']);
This is just one of many possibilities hope it helps you.

Resources