2

I have some post about auth attempt failure but my case seems to be different. Still in dev phase so my password is in plain text. I try to login but i keep getting false and so redirected back to login page.

The error message says username/password does not match but dd reveals that both email and password are correct.

What could be responsible for this failure? PS: it's my first time working with laravel

web.php

Route::post('/login', 'AuthController@authenticate'); Route::get('/', 'PostController@index'); 

AuthController

public function auth() { //dd($request); // attempt to login the user if (! auth()->attempt(request(['email', 'password']))) { return back()->withErrors([ 'message' => 'Username/Password does not macth' ]); } return redirect('/'); } 

PostController

public function index() { $posts = Post::latest()->limit(3)->get(); return view('post.index', compact('posts')); } 
2
  • Is your password saved has a hash in the database or is visible? Commented Feb 23, 2017 at 19:55
  • Password isn't hashed. It's in plain text Commented Feb 23, 2017 at 21:31

2 Answers 2

6

Use this code in your User model and the password will be hashed automatically only if it needs:

public function setPasswordAttribute($value) { if( \Hash::needsRehash($value) ) { $value = \Hash::make($value); } $this->attributes['password'] = $value; } 

and change your password after, so you have the hashed password in the database

Sign up to request clarification or add additional context in comments.

3 Comments

many thanks...that piece of code worked the magic. I'll learn how to authenticate using make:auth in another test project.
Im using an alternate model to user model, one that refers to a preexisting table, and have both the model and alternate table defined in config/auth.php in the default users section. How would I leverage this specified method on that new model, since my passwords are not yet hashed?
Better if you post a new question, referring to this one, comments are not the right place to examine your case.
0

Not sure i understand... but if you are using the Laravel Authentication (php artisan make:auth) you will not be storing the password in plain text... so if you are setting the password directly in your db it will not work as it will check the password field in the db with the assumption that it is stored with a dbcrypt hash...

So if you are using the default auth that comes with laravel, use the registration form to create your user account

Since it's your first time working with laravel i would recommend taking a look at https://laracasts.com/series/laravel-from-scratch-2017/episodes/17 as it covers the auth concept and gives you a quick walkthrough on setting up user auth

1 Comment

Thanks. I think the issue is with the password field which is currently in plain text. By the way I didn't use the make:auth since I was following the laracast tutorial. I'll look into it some more just if I can find away around it. Next time I will use the make:auth

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.