Where is AuthenticatesUsers on laravel 5.3? - php

I build basic Auth in laravel 5.3 user make:auth. In laravel 5.3 they separate login and register controller in Auth\LoginController and Auth\RegisterController.
Below is my Auth\LoginController
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login / registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
The problem is when I'm try to edit AuthenticatesUsers in Illuminate\Foundation\Auth\AuthenticatesUsers, that not affected at all. I even try to rename the class/trait name on Illuminate\Foundation\Auth\AuthenticatesUsers, but the script still works.
So where is the actual AuthenticatesUsers? Because my sublime can only find one file with that name.
Thank You

There is only one AuthenticatesUsers, and it will be in vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php. If you're editing it and it's not having any effect then you could be editing the file in a different project.
It's worth noting that you should never edit this file anyway. Everything in your vendor/ folder should be left as-is, as when Composer runs it will replace any changes you've made. If you want to make changes you should extend or override the methods you need to.

Related

Can't seem to redirect user on session timeout in Laravel

I am running laravel 8 and this site has been upgraded since laravel 5.2 I believe. Currently I have an admin section and when the session times out it goes to /login. I also have another user section where login is used. I am looking for a way to control where the user is redirected based on the url. If they are /admin/* I want to redirect to /admin/login.
My problem is I can not find where this is controlled. I have tried a variety of ways. Looking at the docs (https://laravel.com/docs/8.x/authentication#redirecting-unauthenticated-users) it says I can change the redirectTo function in /App/http/Middleware/Authenticate.php. Issue is I did not have an Authenticate file. I created one and copied the Authenticate from the vendor src file and just tried to do a dd() on the construct method but it just skips past it so when checking where to send them it does not seem to use this file. If I do the same in the vendor folder version it will dump out.
I know I should not edit any code in the vendor folder as it will get overridden with an upgrade so my question is how/where do I edit this redirectTo function.
I think you should check the App\Http\Kernal.php and confirm if the $routeMiddleware array has "Authenticate" middleware or not?
App\Http\Kernal.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
// ...
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* #var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
// ...
]
}
and you can update the Authenticate as follow where we can check the request URL.
App\Http\Middleware\Authenticate.php
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* #param \Illuminate\Http\Request $request
* #return string|null
*/
protected function redirectTo($request)
{
// Or we can check the particular segment of the URL.
if (str_contains($request->url(), 'admin')) {
return route('admin/login');
} else {
return route('login');
}
}
}

Change app.blade.php destination (Jetstream)

So, I'm exploring the world of Jetstream and Fortify. Problem is that I want to change the default app.blade.php to my folder admin/app.blade.php.
Where is this defines so I can change it? Can't find it anywhere.
Many thanks!
(Ps: Don't know if this is categorised under Laravel of Vue.)
Update: To avoid confusion. I installed a fresh installation of Laravel with Jetstream. I'm using Inertia for developing my CMS. Inertia has a default file in /views. This is called app.blade.php, which calls #inertia. I want to move this file to /views/admin. If I do that, I get this error:
View [app] not found.
Which makes sense, because I moved it. Where do I change the render of app.blade.php to change it to admin/app.blade.php?
UPDATE:
You can register this in your AppServiceProvider.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Inertia\Inertia;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
if (request()->is(['admin', 'admin/*'])) {
Inertia::setRootView('admin.app');
}
Inertia::setRootView('app');
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}```
do like this
YourProjectPath/config/view.php
'paths' => [
resource_path('views/admin'),
],
if in Controller you can do like this
return view('admin.app');

Laravel 5.7 Class App\Http\Controllers\Auth\SendsPasswordResetEmails does not exist

I'm trying to implement the reset password function using the built-in function from Laravel 5.7 as i have defined my routes in my web.php. I tried running php artisan route:list , It gave me an exception
UPDATE
Sorry for the lack of information given. I have already ran the command php artisan make:auth previously and the Auth::routes() has already been defined in web.php I am trying to access function resets in ResetPasswords traits through my ResetPasswordControllerbut it gave an exception
Class App\Http\Controllers\ResetPasswordController does not exist
I am using the pre-defined controller that is located at App\Http\Controllers\Auth\ResetPasswor.php
ResetPasswordController
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
public function reset(Request $request){
$reset = $this->reset($request);
}
/**
* Where to redirect users after resetting their password.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
}
web.php
Auth::routes();
Route::post('password/reset','ResetPasswordController#reset');
SOLUTION
I have figured out where did i do wrong i had to add a Auth\ in my routes
Route::post('password/reset','Auth\ResetPasswordController#reset');

Undefined class Socialite in Spark Laravel 5.4

I'm new to Laravel and Spark trying to figure out how/where I should add my new Controllers and add Socialite.
In my providers I added
Laravel\Socialite\SocialiteServiceProvider::class,
In my aliases I added
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
Here's what my app/HTTP/Controllers/Auth/LoginController class looks like
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Redirect the user to the GitHub authentication page.
*
* #return Response
*/
public function redirectToProvider()
{
return Socialite::with('github')->redirect();
}
/**
* Obtain the user information from GitHub.
*
* #return Response
*/
public function handleProviderCallback()
{
$user = Socialite::driver('github')->user();
// $user->token;
}
}
I get undefined class Socialite when I add use Socialite;
I tried composer dump-autoload and php artisan config:clear but nothing is working. Am I doing something wrong?
I'm using Laravel 5.4 and Socialite 3.0
Thanks!
php artisan clear-compiled
composer dump-autoload
php artisan optimize
This will clear the current compiled files, update the classes it needs and then write them back out so you don't have to do it again.

Laravel 5.1 view composer initiates before middleware

I am developing a simple web application with Laravel 5.1 and my development environment is Homestead.
I have a view composer to pass Auth::user() data to admin panel related views automatically. Most general admin panel pages (Dashboard, Settings etc.) uses AdminController, and it extends Laravel's Controller. Specific admin panel pages (Users, Orders etc.) has their own controllers (Admin\UsersController, Admin\OrdersController respectively), which are extends AdminController.
No any middleware registered in routes for admin panel related routes, instead AdminController loads the auth middleware (which checks if registered user tries to load the page). And no other controller that extends AdminController overrides the constructor.
My problem is that if user is not logged in and tries to load an admin panel page (doesn't matter which one; Dashboard, Settings, Users, Orders - because the view composer called for every single one to pass Auth::user() data) there is no warning says "You are not authorized." or no redirection to login page, just an exception thrown which says Auth::user() is null.
Doesn't the auth middleware called first? If not what should I do to prevent the exception to be thrown (returning from view composer is not an elegant solution for my point of view by the way)?
Thanks in advance.
Addendum
AdminController
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function __construct()
{
// After middlewares
$this->middleware("auth");
$this->middleware("admin");
// Before middlewares
$this->middleware("no-cache");
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return view("admin.index");
}
}
Admin\OrdersController
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Order;
class OrdersController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$orders = Order::with("address")->get();
return view("admin.orders.index")->with("orders", $orders);
}
}
ComposerServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Auth;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
view()->composer("layouts.admin.default", function ($view) {
$admin = Auth::user();
$view->with([
"admin" => $admin,
"picture" => $admin->pictures[0]
]);
});
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Note: ComposerServiceProvider is registered in config/app.php.
Your Admin\OrdersController extends App\Http\Controllers\Controller, when it should extend App\Http\Controllers\Admin\AdminController. That's why you are not getting a redirection.

Categories