laravel 5 or 5.1?
How can i modify laravel 5 login system and add on registration a username or another field
hello i am using laravel 5 and on its sign up default it has no phone number or username i want to add this feature but i am not sure where to start please can you help me?
it is laravel 5.0
Have a look at the below files.
/laravel/database/migrations/2014_10_12_000000_create_users_table.php
/resources/views/auth/register.blade.php
/app/User.php
really this is not giving me the permision to add new field on the form i mean where do i need to controll that input i want to add on /resources/views/auth/register.blade.php
I just had to do this, because I can't require email address for login.
I had to copy the Trait AuthenticatesAndRegistersUsers to my App area and renamed it to AuthenticatesAndRegistersUsersByUsername. Then I could change which fields are required to register.
On about line 29 of AuthContoller is where you change the trait that the AuthController uses.
That file (AuthenticatesAndRegistersUsersByUsername.php) looks like this for me now:
<?php
namespace App\Traits;
use Illuminate\Foundation\Auth\RegistersUsers;
trait AuthenticatesAndRegistersUsersByUsername
{
use AuthenticatesUsersByUsername, RegistersUsers {
AuthenticatesUsersByUsername::redirectPath insteadof RegistersUsers;
}
}
Notice this calls in another trait (AuthenticatesUsersByUsername). I did the same thing with that file (copied from AuthenticatesUsers.php and changed references of 'email' to 'username'.
<?php
namespace App\Traits;
use Illuminate\Foundation\Auth\RedirectsUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
trait AuthenticatesUsersByUsername
{
use RedirectsUsers;
/**
* Show the application login form.
*
* @return \Illuminate\Http\Response
*/
public function getLogin()
{
return view('auth.login');
}
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postLogin(Request $request)
{
$this->validate($request, [
'username' => 'required', 'password' => 'required',
]);
$credentials = $this->getCredentials($request);
if (Auth::attempt($credentials, $request->has('remember'))) {
return redirect()->intended($this->redirectPath());
}
return redirect($this->loginPath())
->withInput($request->only('username', 'remember'))
->withErrors([
'username' => $this->getFailedLoginMessage(),
]);
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function getCredentials(Request $request)
{
return $request->only('username', 'password');
}
/**
* Get the failed login message.
*
* @return string
*/
protected function getFailedLoginMessage()
{
return 'These credentials do not match our records.';
}
/**
* Log the user out of the application.
*
* @return \Illuminate\Http\Response
*/
public function getLogout()
{
Auth::logout();
return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}
/**
* Get the path to the login route.
*
* @return string
*/
public function loginPath()
{
return property_exists($this, 'loginPath') ? $this->loginPath : '/auth/login';
}
}
I also changed Services/Registrar.php to this:
<?php namespace App\Services;
use App\User;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
class Registrar implements RegistrarContract {
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
// 'email' => 'required|max:255|unique:users',
'username' => 'required|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
public function create(array $data)
{
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'username' => $data['username'],
'user_type' => $data['user_type'],
'password' => bcrypt($data['password']),
]);
}
}
Then you have to change the views for the relevant files for login, register, etc. But I think that was it. It seems to be working great so far. You will have to tweak this for your own needs.
Oh. I am sorry. This solution works for 5.1, but it should be similar for 5.0. I originally did it in 5.0 and had to immediately update it to 5.1.
thanks you so much this is exactly what i wanted big thanks also.
@jeffdavis You don’t need to copy and rename the traits, you could have just overridden the methods with your username-related code instead.
simple.. listen here..
if u want to add new column into existing table
steps 1: php artisan make:migration <new_folder_name> --create=<existing_table name> 2: open <new_folder_name> from "database/migration" 3: add new columns in up(); 4: save 5: return to command prompt 5.1 : php artisan migrate
-----enjoy laravel---
any example on how to override the methods with my preferred fields and logics?
edit: will going to try this one, https://laracasts.com/discuss/channels/general-discussion/laravel-5-custom-login-and-registration I think this is how it is done.
Please or to participate in this conversation.