All that's in the current AuthController is this code:
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers;
}
What exactly do I add now to login
Have you opened up "AuthenticatesAndRegistersUsers" and looked at it?
Yes, still looking for example on how to use that trait in auth controller.
It is used in this line: use AuthenticatesAndRegistersUsers;
. That line includes everything that is in the Trait. So if you create an route to AuthController@getLogin
the getLogin method in the Trait will be resolved.
I use a userid rather than email as part of a login. So I would actually modify:
Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
The actual trait file to use a userid rather than email, correct?
No, you would override the trait method inside your auth controller.
Something like:
public function mylogin(Request $request)
{
$mylog = new AuthenticatesAndRegistersUsers();
$mylog->postLogin(Request $request)
}
No you need to override the postLogin() method so it will look like this
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
use AuthenticatesAndRegistersUsers;
public function postLogin()
{
// Perform the authentication based on your requirements
}
}
In this setup you will use the methods as definde above and for the undefind ones you will use laravels predefined ones.
In previous L5 before the background trait my login looked like:
public function postLogin(LoginRequest $request)
{
$tvar = $request->input('userid');
$pw = $request->input('password');
if ($this->auth->attempt(['userid' => $tvar, 'password' => $pw]))
{
return redirect('petlist');
}
else
{
echo ' not logged in';
}
}
How do I redo that code to work with the trait?
Sorry, I knew about traits, but never used traits.
I got it working, I only changed email to userid in AuthenticatesAndRegistersUsers.php. The only problem I see with this is an update will change it back to it's original, thus I will probably have to make a backup of this file. Any thoughts on this? Also can I just make a regular auth page/controller like L5 had Nov 25, 2014 before the use trait.
And thanks to all of you.
This is all you need to do:
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\LoginRequest;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
use AuthenticatesAndRegistersUsers;
public function postLogin(LoginRequest $request)
{
$tvar = $request->input('userid');
$pw = $request->input('password');
if ($this->auth->attempt(['userid' => $tvar, 'password' => $pw]))
{
return redirect('petlist');
}
echo ' not logged in';
}
}
You should never change code in the vendor directory, this will be overridden.
Check out this Laracast video: https://laracasts.com/lessons/whats-a-trait it will help you understand traits and then you will understand why the above code should work.
Worked, only a minor change
From:
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\LoginRequest;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
use AuthenticatesAndRegistersUsers;
public function postLogin(LoginRequest $request)
{
$tvar = $request->input('userid');
$pw = $request->input('password');
if ($this->auth->attempt(['userid' => $tvar, 'password' => $pw]))
{
return redirect('petlist');
}
echo ' not logged in';
}
}
To:
<?php namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
use AuthenticatesAndRegistersUsers;
public function postLogin(Request $request)
{
$tvar = $request->input('userid');
$pw = $request->input('password');
if ($this->auth->attempt(['userid' => $tvar, 'password' => $pw]))
{
return redirect('petlist');
}
echo ' not logged in';
}
}
Request instead of LoginRequest, the trait has Request now. Thanks a bunch
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community