Laravel provides artisan command to create register and login feature in project.
Run php artisan make:auth in terminal.
php artisan make:auth
The above command create all related files and routes.
You may customize the user table. Open migration file and Add/change the field and according to that change in RegisterController.php and resources/views/auth/register.blade.php files.
Run php artisan migrate in terminal.
php artisan migrate
Now Register login feature is integrated in your project.
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'username' => 'required|string|max:191|unique:users',
'email' => 'required|string|email|max:191|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
save this file.
protected $fillable = [
'name', 'email', 'password','username',
];
<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
<label for="username" class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required>
@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
</div>
import it above the class
use Illuminate\Http\Request;
add the code just below the constructor function
protected function credentials(Request $request)
{
/// this method is overriden form Illuminate\Foundation\Auth\AuthenticatesUsers; class
$field = filter_var($request->get($this->username()), FILTER_VALIDATE_EMAIL)
? $this->username()
: 'username';
return [
$field => $request->get($this->username()),
'password' => $request->password,
];
}
Only change the type of Email field from email to text and change label
<label for="email" class="col-md-4 control-label">E-Mail/Username</label>
<input id="email" type="text" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
public function __construct()
{
$this->middleware('auth');
}
protected function authenticated(Request $request, $user)
{
// overriden from Illuminate\Foundation\Auth\AuthenticatesUsers; class
//set session
$request->session()->put('admin', 'admin');
//redirect
return redirect()->intended($this->redirectPath());
}