Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to auto login after successfully registered in laravel 5.4.12?

i want to make an auto login after successful registration and redirect to Edit Profile page.. I tried the following code but not working as i want

class RegisterController extends Controller
{

use RegistersUsers;

protected $redirectTo = '/edit_profile';


public function __construct()
{
    $this->middleware('guest');
}

public function createUser(Request $request)
{


    $this->validate($request , [
        'firstName'    => 'required',
        'lastName'     => 'required',
        'displayName'  => 'required',
        'email'        => 'required |email',
        'password'     =>'required ',
        'gender'       =>'required',
        'day'          =>'required|max:2',
        'month'       =>'required|max:2',
        'year'       =>'required|max:4',
    ]);
    $email=$request->input('email');
    $password=$request->input('paasword');
    $dob=$request->input('day').'-'.$request->input('month').'-'.$request->input('year');
    $request->request->add(['dob'=>$dob]);
    $request->request->add(['password'=>bcrypt($request->input('password'))]);
    $data = User::create($request->except(['_token', 'submit', 'day','month','year', 'confirm_password' ,'dayInWeek']));

    Auth::login($data);

 }

}

Routes

    Route::get('/', 'PageController@login');
    Route::get('/home', 'HomeController@index');


    Route::group( ['middleware' => 'auth' ], function()
    {
     Route::get('main', 'PageController@main');

     Route::get('browse', 'PageController@browse');

     Route::get('pickngo', 'PageController@pickngo');

     Route::get('edit_profile', 'PageController@profile');
    }
like image 729
Jamal Ahmad Avatar asked Mar 16 '17 07:03

Jamal Ahmad


People also ask

How do I enable authentication in Laravel?

Just run php artisan make:auth and php artisan migrate in a fresh Laravel application. Then, navigate your browser to http://your-app.test/register or any other URL that is assigned to your application. These two commands will take care of scaffolding your entire authentication system!

How does Laravel default authentication work?

Laravel includes built-in authentication and session services which are typically accessed via the Auth and Session facades. These features provide cookie-based authentication for requests that are initiated from web browsers. They provide methods that allow you to verify a user's credentials and authenticate the user.


2 Answers

Use Laravel loginUsingId($id) function by passing the user id.

$data = User::create($request->except(['_token', 'submit', 'day','month','year', 'confirm_password' ,'dayInWeek']));

Auth::loginUsingId($data->id);
like image 55
Shahid Ahmad Avatar answered Oct 20 '22 04:10

Shahid Ahmad


just modify your auth login just like

if(Auth::login($data))   {
   return redirect('/edit_profile');
   }
   else
  {
   return redirect()->back();
  }
like image 26
Saran Avatar answered Oct 20 '22 03:10

Saran