Search code examples
laravellaravel-5laravel-5.3

how to login with username in laravel


Hi this is my users model structure,

'name' => 'required|max:255',
'phone' => 'required|numeric',
'email' => 'required|email|max:255|unique:users',

And this is Profile model structure

'username' => 'required|max:255',
'password' => 'required|min:6|confirmed',

I need keep up on this structure and login with username and password , because some users can not have login and cant take user and password .

I change Auth to this

public function create(array $data)
{
     $user = User::create([
         'email' => $data['email'],
         'phone' => $data['phone'],
     ]);
     $user->profile()->create(['name' => $data['name'],'password' =>bcrypt($data['password'])]);
     return $user;
}

and now how can i do login :| ? I know need to change login method on this file laravel\vendor\laravel\framework\src\Illuminate\Foundation\A‌​uth\AuthenticatesUse‌​rs.php But i don't know what's the changes


Solution

  • If you need to login right after registering, just do this:

    Auth::attempt($data['name'], $data['password']);
    

    If you're just want to login users, you could override Illuminate\Auth\SessionGuard@attempt method, but you could just use Auth::login

    if ($request->login == $loginFromDB && Hash::check($request->password, $passwordFromDB)) {
        Auth::login($user, true); // User must be an User model instance
    }