Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make auto login after registration in laravel

Tags:

php

laravel

I have encounterd a problem while regiserting user in laravel, $user suppose to be array which contains all array element while auto login the following code results false. The password save in the database is hash::make('password').

$user_id = $this->user_model->addUser($user);
$post = array('password' => $pass_for_auth, 'email' => $email);
var_dump(Auth::attempt($post,true));die;

Can any one tell me what is the correct problem

like image 952
Harshit Sethi Avatar asked Apr 27 '16 08:04

Harshit Sethi


2 Answers

You can try to login the user through his $user_id. So your code will be:

$user_id = $this->user_model->addUser($user);
$post = array('password' => $pass_for_auth, 'email' => $email);
Auth::loginUsingId($user_id);

You created the user so it returns an user_id, with the user_id you can login the user.

Hope this works!

More information at: https://laravel.com/docs/5.2/authentication#other-authentication-methods

like image 87
Robin Dirksen Avatar answered Nov 05 '22 23:11

Robin Dirksen


This would be the standard way to do user registration - you create a new user model instance and pass it to Auth::login() :

// do not forget validation!
$this->validate($request, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);

$data = $request->all();

$user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

Auth::login($user);
like image 36
Tadas Paplauskas Avatar answered Nov 05 '22 22:11

Tadas Paplauskas