Laravel provides artisan command to create a register and login features in the project. Run PHP artisan make auth in the terminal.Login Signup with Laravel
Read our More Tutorials
How to generate barcode in ReactJS
Learn more about Laravel :https://laravel.com/
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.
If you want customization( Adding username)
- Run PHP artisan make auth in the terminal.
- Open create_users_table.php migration file and add the following line
$table->string(‘username’,292)->nullable()->unique(); - Run php artisan migrate in terminal
- Now open RegisterController.php and add the following line.
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']),
]);
}
Now save this file. Open User.php model and insert the following code and save it
protected $fillable = [
'name', 'email', 'password','username',
];
Open resources/views/auth/register.blade.php file , add following code just below the name field and save it.
<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>
Now you open LoginController.php file add following code and save it.
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,
];
}
Open login.blade.php file add the following code and save it
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>
pages you want to access only after login then add the following code in respective controller files
public function __construct()
{
$this->middleware('auth');
}
If you want to set custom session add following code
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());
}
nice sir g
Thank you and keep visiting us dear