login verifcation

Published 1 month ago by DanielGr

hi guys so i'm trying to make a login process using larave 4.2 and i can't seem to figure out what's wrong with my code since i'm new to laravel. Here's my code

Best Answer (As Selected By DanielGr)
sutherland

If none of the passwords are hashed, that's probably your problem. The passwords should not be in plain text, because Laravel will hash your user's input before checking if it matches the value in the database.

DanielGr
public function store()
    {
        $userdata = array('''
            uname' => Input::get('uname'),
            password' => Input::get('psw')
            );
        
        if (Auth::attempt($userdata)) {
        return "hooray";
        }
        else {
        return "sorry error";
        } } } 

everything seems working fine (even connections and data passing) except that it always go to the else part

it would really be a big help if you could help me with this

sutherland

Use three backticks (`) on the line before and the line after your code to make it display here.

sutherland

The bacticks should be on their own line before and after.

But I do have to ask, if you're just getting started, why 4.2? Are you taking over an existing project?

DanielGr

yes it's a existing project and our instructor advised us to use 4.2 since were new to laravel

sutherland

Wow, 4.2 is over 3 years old and no longer supported, don't know why an instructor would be using it.

Anyways, what does your form look like? Is the username column actually uname in the database? What do you get if you dd($userdata); before the Auth::attempt?

PS - You only need three backticks on their own line once before and once after the block of code.

DanielGr

yes it really is uname, and when i try adding dd($userdata) it displays the info i entered in the form, it seems that " auth::attempt " in my code always fails

sutherland

Did you register the user correctly? Is their username hashed in the database?

DanielGr

nope, uh correct me pls if i'm wrong, since it is already a existing project, i already have an existing database, so what i did is i skipped the registering user part and went straight through the ' login authentication part ' just to see if i can already utilize my existing databse and use it to enter my project homepage

ps. nothings hashed in my database

sutherland

If none of the passwords are hashed, that's probably your problem. The passwords should not be in plain text, because Laravel will hash your user's input before checking if it matches the value in the database.

DanielGr

oh, okay i get in now, i should not have skipped the ' register user part ' because of laravel's hashing right?

sutherland

Yes, you never want passwords stored in plain text. In 4.2 you need to use $password = Hash::make('secret'); when creating a user.

DanielGr

oh okay, i'll start coding the register part now, thank you very much sutherland for shedding some light in my understanding of laravel and for answering my newbie questions

DanielGr

okay so i already did the register part with hashing and it works, great, now i tried logging in using the same account that i created but it seems my ' auth::attempt 'still fails

Here's my code for the account creation

public function postCreate()
    {
        $validation=Validator::make(Input::all(),
        array(
            'f_name'=>'required',
            'l_name'=>'required',
            'u_name'=>'required|max:255',
            'email'=>'required',    
            'pass1'=>'required|alphaNum',
            'pass2'=>'required|alphaNum'
        ));

         if($validation->fails())
            return "validation error";  
        else
        {
            $user=new User();
            $user->fname=Input::get('f_name');
            $user->lname=Input::get('l_name');
            $user->uname=Input::get('u_name');
            $user->email=Input::get('email');
            $user->password=Hash::make(Input::get('pass2'));
            $user->save();
            return "account created";
        }
    } 

and Here's my code for logging in

public function postLogin()
    {   
    $validator = Validator::make(Input::all() , Account::$rules);
        if ($validator->fails())
        {
            return "validation fails";
        }
        else
        {
            $auth=Auth::attempt(array(
            'uname' => Input::get('uname'),
            'password' => Input::get('psw')
            )); 
            
            if ($auth)
            {
                return "sucess";
            }           
            else
            {
                return "auth error";
            }
        }

    }   

Please sign in or create an account to participate in this conversation.