I am using Laravel v5.2.39. I want to redirect to dashboard only, if you are logged in. If you change URL manually, it will redirect you to home screen. I am using auth middleware, but it doesn't work. Any help?
My routes.php file:
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::get('dashboard', [
'uses' => 'UserController#getDashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);
My UserController.php:
public function getDashboard(){
return view('dashboard');
}
And auth middleware:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->route('home');
}
}
return $next($request);
}
I don't know, whats the problem with. I have some sign in and sign up too, but i dont think this is problem. If somenone wanna see it, write me.
Have a nice day and thank you.
As mentioned above, have you tried something like this?
Route::group(['middleware' => 'auth'], function () {
Route::get('dashboard', 'UserController#getDashboard')->name('dashboard');
});
Or you can add the middleware in the construct function in your class like so :
public function __construct() {
$this->middleware('auth');
}
Also, see the laravel documentation on using middleware with routes.
Try using 'middleware' => 'auth:web' in place of 'middleware' => 'auth'
Hey im not clear about your problem but this will help you,
when you go for project path localhost/project/ it will redirect to localhost/project/home URL in that URL u can assign any function
Route::get('/', function () {
return redirect('home');
});
Related
I created a middleware to check login authentication, but when I redirected the redirect, I got this error.
middleware/TwoFA.php
public function handle($request, Closure $next)
{
$response = $next($request);
if(Auth::check()){
if(auth()->user()->is_verified == true){
return $response;
}else {
return redirect('/verifyOTP');
}
}
return $response;
}
kernel.php
'web' => [
\App\Http\Middleware\TwoFA::class,
],
web.php
Route::get('verifyOTP', 'VerifyOTPController#showVerifyPage')->name('verify');
Route::post('verifyOTP', 'VerifyOTPController#verify');
Route::group(['middleware'=>'TwoFA'], function (){
Route::get('/home', 'HomeController#doashboard')->name('doashboard');
});
Add
use App\Http\Middleware\TwoFA;
to web.php
and change
Route::group(['middleware' => TwoFA::class], function () {
Route::get('/home', 'HomeController#doashboard')->name('doashboard');
});
I create a multiple Authentication in Laravel. When I login with user, on debug in post login method, after Auth::guard('user')->attempLogin..... I see a user but after redirect to HomeController this return null.
How to resolve? I'm beginner in Laravel.
Thank's!!!
/routes/auth/user.php
Route::prefix('backoffice')->name('user.')->namespace('User')->middleware('user')->group(function () {
Auth::routes();
Route::get('home', 'HomeController#index')->name('home');
});
/routes/web.php
Route::group(['middleware' => 'web'], function() {
require 'auth/user.php';
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('cadastro', 'CadastroController');
});
/app/Controllers/User/Auth/LoginController - #post Login
public function login(Request $request) {
$credentials = [
'username' => $_POST['username'],
'password' => $_POST['password']
];
Auth::guard('user')->attempt($credentials, false);
//dd('auth', Auth::guard('user'));
return redirect()->intended('/backoffice/home');
}
/app/Controllers/User/HomeController
public function __construct()
{
$this->middleware('user');
dd('after middleware', Auth::guard('user'), Auth::guard('user')->user());
}
public function index()
{
return view('user.home');
}
By default, Laravel doesn't ship with auth guard user. Perhaps you meant to use web guard i.e Auth::guard('web'). Auth::guard()->user() should return the logged in user object if a user is logged in.
Also, the default middleware for checking logged in user is auth, not user. So, your route might look like this: Route::prefix('backoffice')->name('user.')->namespace('User')->middleware('auth')->group(function () {});, except you've defined a custom middleware in app/Http/Kernel.php $routeMiddleware array with alias user
so im trying to put my localization as prefix on my website domain so i made a middleware the puts the localization lang on the url ever time you try to open a page heres my middleware
public function handle($request, Closure $next)
{
if ($request->method() === 'GET') {
$segment = $request->segment(1);
if (!in_array($segment, config('app.locales'))) {
$segments = $request->segments();
$fallback = session('locale') ?: config('app.fallback_locale');
$segments = array_prepend($segments, $fallback);
return redirect()->to(implode('/', $segments));
}
session(['locale' => $segment]);
app()->setLocale($segment);
}
return $next($request);
}
and i added the middleware to the routemiddleware
protected $routeMiddleware = [
'Locate' => \App\Http\Middleware\Locale::class,
];
and i did put a prefix and middleware to all my routes like this
Route::prefix('{lang?}')->middleware('Locate')->group(function() {
Route::get('logout', '\App\Http\Controllers\Auth\LoginController#logout')->name('logout');
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin/login', function () {
return view('admin.auth.login');
})->name('AdminLogin');
Route::get('/contact-us', 'ContactUsController#Contactus')->name('ContactUs');
Route::post('/contact-us', 'ContactUsController#PostContactus')->name('PostContactUs');
Route::prefix('auth')->group(function () {
Route::get('/login', function () {
return view('auth.login');
})->name('Login');
Route::post('/login', 'Auth\LoginController#Login')->name('userslogin');
});
Route::prefix('search')->group(function () {
Route::get('/categories', 'search\SearchController#Categories')->name('Categories');
Route::get('/filter/{categoryseo}', 'search\SearchController#filter')->name('InstructorsSearch');
//sending categoryseo to the filter page so i can put it in hidden input in the filter page and use it to get the list
Route::get('/list', 'search\SearchController#InstructorList')->name('InstructorsSearchList');
Route::get('/profile/{userid?}', 'search\SearchController#instructorprofile')->name('InstructorProfile');
});
});
for some reason pages like /home gets the to the middleware and change as i wanted like this /en/home , as for other like search/categories it wont even notice my middleware but i tried to remove the prefix of the localization and just put my middleware it worked and it notice my middleware. using laravel 5.5
How i get all users in laravel 5.3 . i am using barryvdh for cors
this is my RegisterController which is in Auth folder
public function index(){
return Users::all();
}
above code gives all the user data if in route below we do not use middleware
and if we use middleware then i got error unauthenticated . so i want to get all user data using middleware in route . How can i get
Route::get('/users','Auth\RegisterController#index')->middleware('auth');
I think you can try this :
public function index(){
$users = Users::get();
return $users;
}
Hope this work for you!
So if you want to get all data of your user table you simple have to do following:
# Middleware group if user is logged in
Route::group(['middleware' => 'auth'], function ()
{
Route::get('home', ['as' => 'home', 'uses' => 'HomeController#index']);
Route::group(['prefix' => 'user'], function ()
{
Route::get('get', ['as' => 'getUser', 'uses' => 'UserController#getUser']);
});
});
And in your controller you can do something like this:
class UserController extends Controller
{
public function getUser(Request $request)
{
$users = DB::table('users')->get();
return $users;
}
}
If you want add something to that return you probably have to create a relation between your models, and call them in your method and return them.
If you still have any questions or if I understood something wrong feel free to comment on this answer.
Edit:
If you want to return all user with an api route you can do following in your api routes:
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Try this
public function index()
{
$userDetail = Users::get();
dd($userDetails);
}
I am using Laravel 5.4. I want to assign all authority to 'Admin' role. But Employee cannot delete or edit records. I'm using Entrust Package for Role based permission.
I've written routes for this but unfortunately Its not working for employee.
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () {
// Route::resource('plotsize', 'plotSizeController');
Route::group(['middleware' => 'role:Employee'], function () {
Route::resource('plotsize', 'plotSizeController', ['except' => 'edit', 'update', 'delete']);
});
Route::group(['middleware' => 'role:Admin'], function () {
Route::resource('plotsize', 'plotSizeController');
}); });
This is another approach that I had tried but unfortunately Its also not working.
protected $user;
public function __construct()
{
$this->middleware(function ($request, $next) {
if(Auth::user()->hasRole('Admin')){
$this->middleware('role:Admin');
return $next($request);
}
if(Auth::user()->hasRole('Employee')){
$this->middleware('role:Employee', ['only' => ['edit', 'update', 'destroy']]);
return $next($request);
}
});
}
If you're giving me suggestion to use Policy in Laravel. I've tried that but I'm not getting what's the issue on this. Can you provide me any example for this? Thanks