1

In RegisterController I wrote

$user=User::create([ ... ]); Auth::login($user); $myid = Auth::id(); return redirect()->route('profile',['id'=>$myid]), 

And I redirect to page with new id.

In web.php I wrote

Route::get('/profile/{id}','ProfileController@show')->name('profile'); 

But in ProfileController show I wrote dump(Auth::user()) and it shows 'null' I tried with sessions. But session are not saving and it also shows 'null'. I tried to change Kernel.php but it didnt help. I tried all routes move to middleware web although Laravel automatically uses this middleware. Of course, it also didnt help. I have this problem.

11
  • Please try dump(Auth::user()) Commented Mar 27, 2017 at 18:25
  • i meant Auth::user() it shows 'null' Commented Mar 27, 2017 at 18:33
  • The standard RegisterController already login a new created user. Where did you wrote the code in the controller? Commented Mar 27, 2017 at 18:44
  • I didnt make:auth. I did manually authentification. I created RegisterController. I save data in database and redirect to page with new id. but then auth::check() is not working. i created new session in RegisterController and tried dd(session('example')) in ProfileController. but it shows null. sessions are not saving. therefore auth is not working Commented Mar 27, 2017 at 18:53
  • I advise you to start simple: use the standard Auth that ships with laravel and make your modifications, step by step. Without all your code its difficult to guess what is wrong. Commented Mar 27, 2017 at 19:02

2 Answers 2

2

You should check it like this:

dd(Auth::user()); 

Or:

dd(auth()->user()); 

And not like this:

dump(Auth::user) 
Sign up to request clarification or add additional context in comments.

1 Comment

i meant Auth::user() it shows 'null'. I tried dd(auth()->user()); Whole day I try other things(
0

I would recommend using the Auth::attempt($credentials); method instead of directly calling the Auth::login().

Auth::attempt($credentials); 

will attempt to login the user with the provided credentials building the session and all and will return true if successful and false if fails. So there you can test your outcome straight away.

Check out the docs: https://laravel.com/docs/5.4/authentication#authenticating-users

You can also specify a guard using Auth::guard([any-guard])->attempt($credentials). In case you don't pass a name to guard() it'd be using the default guard.

Another important thing is that you have to hash your passwords before creating and passing it to a new User. Because all this methods like attempt() will try to match the hashed version of the password. You can hash your passwords like this:

Hash::make(<your-password>); 

Remember to add use Hash; in the header of the file or prepend the class name with a backslash like this: \Hash::make(<your-password>)

PS: This is only for User creation time. Because Auth::attempt() will hash the password automatically. This means you don't have to worryt to hash your password before passing it for authentication.

6 Comments

In LoginContoller I tried many things Class LoginController extends Controller { public function log(){ if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) { // $myid=Auth::id(); // return redirect()->route('profile',['id'=>$myid]); echo 'dfgdfgdfgdfg'; } else{ echo 'no'; } dd(Auth::user()); } } But its not working. I guess that I have problems with sessions. Sessions are not saving.
So, what's the output from this code you pasted? Are you getting the "no" echoed? Remember you have to add the use Auth; above so it's available in your code.
Also, another important thing is, did you "hash" the password before saving it to the database? Cause this attempt() method will hash the password field and test with this hashed version. You have to use Hash::make(<your-password>) when you pass it to the User::create(). Added this response as part of the answer, as it's useful.
i get "no". attempt is not working. i wrote use Auth. i also wrote use Illuminate\Support\Facades\Auth; i tried with bcrypt(password) and without bcrypt. auth is not working.
Try with Hash::make() instead.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.