Call to a member function set() on null - laravel-5

I am trying to use oauth for google in laravel 5 but i am getting the error. Can any one help me to sort out this problem.
Followings are my files please check out
.env
GOOGLE_ID = 'mygoogleId'
GOOGLE_SECRET = 'mysecretID'
GOOGLE_REDIRECT = http://localhost:8090/users
services.php
'google' => [
'client_id' => env('GOOGLE_ID'),
'client_secret' => env('GOOGLE_SECRET'),
'redirect' => env('GOOGLE_REDIRECT'),
],
AuthController
public function redirectToProvider() {
return Socialite::driver('google')->redirect();
}
public function handleProviderCallback() {
$user = Socialite::driver('google')->user();
console.log($user);
}
routes.php
Route::get('google', 'Auth\AuthController#redirectToProvider');
Route::get('google/callback', 'Auth\AuthController#handleProviderCallback');
//I have set the providers and aliases in app.php.
Here is the code where i am getting an error
//on set() method
public function redirect()
{
$state = str::random(40);
if ($this->usesState()) {
$this->request->getSession()->set('state', $state);
}
return new RedirectResponse($this->getAuthUrl($state));
}
Thanks in advance..

Hey If you are using laravel 5.2, this is worked for me.
Put your controllers in 'web' middleware. like,
Route::group(['middleware' => 'web'], function() {
Route::get('google', 'Auth\AuthController#redirectToProvider');
Route::get('google/callback', 'Auth\AuthController#handleProviderCallback');
});
and Make sure Kernel file has middleware classes registered.
/**
* The application's route middleware groups.
*
* #var array
*/
protected $middlewareGroups = [
'web' => [
\Perkweb\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Perkweb\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];

Related

custom auth of two different users using two tables. i don't want to use make:auth

The first auth system works very fine its code is below and needed to have to different users using two different tables am using laravel 5.5
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Auth;
class StudentController extends Controller
{
public function Register(Request $request)
{
$firstname = $request['firstname'];
$othername = $request['othername'];
$email = $request['email'];
$password = $request['password'];
$user = new User();
$user->firstname = $firstname;
$user->othername = $othername;
$user->email = $email;
$user->password = $password;
$user->save();
Auth::login($user);
return redirect()->route('studentDashboard');
}
public function Login(Request $request)
{
if(Auth::attempt(['email'=> $request['email'], 'password'=>
$request['password']]))
{
return redirect()->route('studentDashboard');
}
return redirect()->back();
}
}
i duplicated the above to create auth for a different user.The registration works but the login does not work even if the login data is right it returns the redirect back after the if statement
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Employer;
use Illuminate\Support\Facades\Auth;
class EmployerController extends Controller
{
public function createEmployerAccount(Request $request)
{
$companyName = $request['companyname'];
$companyEmail = $request['email'];
$companyPasword = $request['password'];
$Employer = new Employer();
$Employer->companyname = $companyName;
$Employer->email = $companyEmail;
$Employer->password = $companyPasword;
$Employer->save();
Auth::login($Employer);
return redirect()->route('employersDashboard');
}
public function signInEmployer(Request $request)
{
if(Auth::attempt(['email'=>$request['email'],
'password'=>$request['password']]))
{
return redirect()->route('employersDashboard');
}
return redirect()->back();
}
}
when i try to change the 'email' to 'emails' an error is shown->the select query is from the users table not employers table that i need to get data from and also when i change 'password' to 'passwords' an error "undefined index password" is shown
this is the route file content
Route::get('/',function(){
return view('pages.index');
})->name('home');
Route::post('/signup',[
'uses'=>'StudentController#Register',
'as'=> 'signup'
]);
Route::post('/signin',[
'uses'=>'StudentController#Login',
'as'=>'signin'
]);
Route::get('/employers',[
'uses'=>'PageController#employersPage',
'as'=>'employers'
]);
Route::get('/studentDashboard',[
'uses'=>'PageController#getStudentDashboard',
'as'=> 'studentDashboard'
]);
Route::post('/createcompany',[
'uses'=>'EmployerController#createEmployerAccount',
'as'=>'createcompany'
]);
Route::post('/signInEmployer',[
'uses'=>'EmployerController#signInEmployer',
'as'=>'signInEmployer'
]);
Route::get('/employersDashboard',[
'uses'=>'PageController#getEmployersDashboard',
'as'=> 'employersDashboard',
'middleware'=>'auth:employer'
]);
Route::post('/createPost',[
'uses'=>'PostController#postCreatePost',
'as'=> 'createPost'
]);
You need to tell Auth to use different Guard for authentication at time of Employer login. To define guards for Employer change like this in your config/auth.php.
Look for guards section in auth.php and add your new guard
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'employer' => [
'driver' => 'session',
'provider' => 'employers',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
Now in the same file there is a providers section. You need to add employers provider
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
//Employer provider
'employers' => [
'driver' => 'eloquent',
'model' => App\Employer::class,
],
],
Create a custom Auth middleware
namespace App\Http\Middleware;
use Closure;
use Auth;
class AuthenticateEmployer
{
public function handle($request, Closure $next)
{
//If request does not comes from logged in employer
//then he shall be redirected to employer Login page
if (!Auth::guard('employer')->check()) {
return redirect('/signInEmployer');
}
return $next($request);
}
}
Register custom auth middleware in Kernal.php in routeMiddleware
'employerAuth' => \App\Http\Middleware\AuthenticateEmployer::class,
Now we have setup our custom guard and custom middleware employerAuth
EmployerController
class EmployerController extends Controller
{
//either you have to define this or you can use `Auth::guard('employer')->attempt($credentials)` in login
protected function guard()
{
return Auth::guard('employer');
}
public function signInEmployer(Request $request)
{
if(Auth::attempt(['email'=>$request['email'],
'password'=>$request['password']]))
{
return redirect()->route('employersDashboard');
}
return redirect()->back();
}
}
For all the routes protected by Employer auth, you either need to add middleware employerAuth in routes or add employerAuth in each controller construct like this
public function __construct()
{
$this->middleware('employerAuth');
}
Hope it may help you. For details you can check this https://laravel.com/docs/5.6/authentication#authenticating-users
Check this nice sample app for multi auth application https://github.com/yskoverride/Various2.0/tree/master/app

Laravel implementing simple MiddleWare doesn't work

In my web application i'm trying to check local in URLs, for example:
In this URL as http://www.sample.com/aboutUs we don't have any locale such as en,ru or etc like: http://www.sample.com/en/aboutUs.
In my web app I implemented simple middleware to check that and fix URL when URLs don't have them:
class language
{
public function handle($request, Closure $next)
{
dd($request);
$locale = $request->segment(1);
if (!array_key_exists($locale, config('app.locales'))) {
$segments = $request->segments();
$segments[0] = config('app.fallback_locale');
return redirect(implode('/', $segments));
}
app()->setLocale($locale);
return $next($request);
}
}
Which that registered into:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\Language::class,
...
],
'api' => [
'throttle:60,1',
'bindings',
],
];
protected $routeMiddleware = [
'language' => \App\Http\Middleware\Language::class,
...
];
This middleware only work when we have locale in URL and my code as:
Route::get('/showContent/aboutUs', ['middleware' => 'language', function()
{
dd('asda');
//
}]);
This doesn't work and I don't see any dd output
1:
Route::group(['middleware' => 'web'], function () {
Route::get('/showContent/aboutUs', 'HomeController#aboutUs');
});
2:
Route::group(['middleware' => 'language'], function () {
Route::get('/showContent/aboutUs', 'HomeController#aboutUs');
});
for all my solutions i get this output:
Sorry, the page you are looking for could not be found.

Laravel not creating authenticated session for user

I am doing user authentication using a custom table. I am able to login successfully as when I do in AuthController.php
public function authenticated(Request $request, $user)
{
dd($user);
}
I am getting the user details.
But when I access some other route and in the respective controller when I do
dd(Auth::user()) returns null
dd(session()->all()) returns _token
dd(Auth::check()) returns false
I am using public_users table for authentication and public_email , public_password fields for doing the authentication so I have changed the App\User.php file as follows:
class User extends Authenticatable
{
protected $table = 'public_users';
protected $primaryKey = 'public_users_id';
protected $fillable = [
'public_email',
'public_password'
];
protected $hidden = [
'public_password'
];
// Override required, Otherwise existing Authentication system will not match credentials
public function getAuthPassword()
{
return $this->public_password;
}
}
and in app/Http/Controllers/Auth/AuthController.php I have added the below code
public function loginUsername()
{
return property_exists($this, 'username') ? $this->username : 'public_email';
}
public function authenticated(Request $request, $user)
{
dd($user);
}
and my config/auth.php has
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
]
]
What am I doing wrong? How could I create user session?
Any help appreciated!
Try auth()->user() after login
Solved!
I removed dd($user) from app/Http/Controllers/Auth/AuthController.php which was closing the Request LifeCycle and creating a new token each time. All I needed was to redirect which is default in handleUserWasAuthenticated method in /vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php
protected function authenticated(Request $request, $user)
{
return redirect()->intended($this->redirectPath());
}

Auth::guard('user')->user() is null in Laravel 5.3

I'm trying to pass to all templates current user object like this:
class Controller extends BaseController
{
public function __construct()
{
view()->share('usr', Auth::guard('user'));
}
}
Every controller is extended by Controller. But if i try to dump Auth::guard('user')->user() Laravel returns null, although I am logged in. Moreover when i pass this variable into template, {{ $usr->user() }} returns current user. What I've done wrong?
my config/auth.php
'defaults' => [
'guard' => 'user',
'passwords' => 'users',
],
'guards' => [
'user' => [
'driver' => 'session',
'provider' => 'user',
],
],
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
Kernel.php
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* #var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
//\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* #var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
my own function to log in:
public function authorizes(Request $request)
{
$this->validate($request, [
'login' => 'required|max:50',
'password' => 'required|max:50'
]);
$credentials = $request->only(['login', 'password' ]);
$remember = $request->get('remember', false) == 1 ? true : false;
if ($this->guard->attempt( $credentials, $remember)) {
$user = $this->guard->user();
$user->last_login = date('Y-m-d H:i:s');
$user->save();
return redirect()->route( 'homepage' )->withSuccess(trans('app.login.success'));
}
return redirect()->back()->withErrors(trans('app.wrong.credentials'));
}
In Laravel 5.3 you should change your controller constructor like so to make this work (assuming you use at least Laravel 5.3.4):
public function __construct()
{
$this->middleware(function ($request, $next) {
view()->share('usr', Auth::guard('user'));
return $next($request);
});
}
You can see this change described in Upgrade guide:
In previous versions of Laravel, you could access session variables or
the authenticated user in your controller's constructor. This was
never intended to be an explicit feature of the framework. In Laravel
5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.
As an alternative, you may define a Closure based middleware directly
in your controller's constructor. Before using this feature, make sure
that your application is running Laravel 5.3.4
Try to do this:
view()->share('usr', Auth::user());
Or this:
view()->share('usr', auth()->user());
Frist check if user is logged or not, then share the current user as:
public function __construct()
{
if (auth()->check()) {
$this->currentUser = auth()->user();
View::share('currentUser', $this->currentUser);
} else {
// you can redirect the user to login page.
}
}
In your case there are two things to consider
To get actual user model you should do Auth::guard('user')->user()
Auth::user() is actually not yet initialized when view()->share() is called, see https://github.com/laravel/framework/issues/6130
Therefore you could rather use view composer. In boot method of your AppServiceProvider add:
\View::composer('*', function ($view) {
$view->with('usr', \Auth::guard('user')->user());
});

Laravel 5 Add another parameter to check on login

I'm using Laravel 5.3 and I used the make:auth to scaffold the registration/login system. I was wondering how do I add another parameter to check on the login, besides checking if the username and password correspond to the matching DB row?
EDIT: I tried looking online and I found I could change it in the Auth::Attempt line but those are all for Laravel 5.2 and below. The LoginController has this:
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
From what I understand, that redirects to the middleware RedirectIfAuthenticated, but then that middleware is like this:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
I'm almost sure I have to add a custom guard, but even at the docs I looked online, it was kinda confusing.. Anybody have a link that explains this really well? Or maybe a sample code?
Yes, you could do it by overriding a AuthenticatesUsers's credentials() method in your LoginController.php
use Illuminate\Http\Request;
protected function credentials(Request $request)
{
$credentials = $request->only($this->username(), 'password');
$credentials['active'] = 1;
return $returnArray;
}
This example is of most common use case where user can login only if his account is active.
If you want to use other table instead of users, you need to customize guard
Add one in config/auth.php
'guards' => [
'user' => [
...
],
'customer' => [
'driver' => 'session',
'provider' => 'customers',
],
],
...
'providers' => [
'users' => [
...
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
],
And then, add this in your LoginController.php
use Illuminate\Support\Facades\Auth;
protected function guard()
{
return Auth::guard('customer');
}
As specified in docs, you can use the same in RegisterController and ResetPasswordController as well.

Resources