Answer the question
In order to leave comments, you need to log in
How to set non-standard fields on authorization checks in laravel?
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
protected function validator(array $data)
{
return Validator::make($data, [
'login' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'pass' => 'required|confirmed|min:6',
]);
}
protected function create(array $data)
{
$user=new User;
$user->login= $data['login'];
$user->email=$data['email'];
$user->pass=bcrypt($data['pass']);
$user->save();
}
public function authenticate(Request $request)
{
$validator = Validator::make($request->all(), [
'login'=>'required',
'pass'=>'required'
]);
$credentials = $this->getCredentials($request);
if ($validator->fails()) {
return Redirect::to('auth/login')
->withErrors($validator)
->withInput();
} else {
if (Auth::attempt($credentials)) {
return redirect()->intended('home');
}
return Redirect::to('auth/login')
->withErrors('Wrong login/pass');
}
}
protected function getCredentials(Request $request)
{
return $request->only('login', 'pass');
}
}
Answer the question
In order to leave comments, you need to log in
Anything can be passed to the attempt
method , as long as there is a password field .
It will figure out how to validate it.
PS
If you are very interested in how it works, then you can open the Illuminate\Auth\EloquentUserProvider class and look at the methods: retrieveByCredentials and validateCredentials
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question