Search code examples
phplaravellaravel-4restful-authenticationuser-accounts

How to make a Sign-in function in Laravel 4?


enter image description here

I have 3 user types :

  1. Admin
  2. Distributor
  3. Internal

I have a problem sign in as user type. ( Internal )

I can sign in when my user type is Admin. I can sign in when my user type is Distributor.

BUT I can’t sign in when my user type is internal. Wired ????

I look through every single line of my code in my sign-in functions in my AccountController.php. I didn’t notice any bug there. If you guys notice any bugs there -- please kindly let me know.

That will be a huge help for me.

Here is my Sign-In Functions

GET

public function getSignIn(){
    return View::make('account.signin');
}

POST

public function postSignIn() {
    $validator = Validator::make( Input::only('username','password'),
        array(
            'username'       =>'required',
            'password'       =>'required'
            )
        );
    if ($validator->fails()) {
        return Redirect::route('account-sign-in-post')
        ->with('error','Username/Password Wrong or account has not been activated !')
        ->withErrors($validator);
    }

        // Remember Me
    $remember = (Input::has('remember')) ? true : false ;
    $auth = Auth::attempt(array(
        'username' => strtolower(Input::get('username')),
        'password' => Input::get('password'),
        'active' => 1),
    $remember);


    // Keep track on log-in information of the user.
    if(Auth::check()){
        $user = Auth::user();
        $user->last_logged_in = Input::get('created_at');
        $user->logged_in_count = $user->logged_in_count + 1 ;
        $user->is_online = '1';
        $user->save();
    }



    if($auth) {
        return Redirect::to('/')
        ->with('success','You have been successfully logged in.')
        ;
    } 
    else {
        return Redirect::route('account-sign-in')
        ->with('error','Username/Password Wrong or account has not been activated !')
        ->withInput(Input::except('password'))
        ->withErrors($validator);
    }

}

VIEW

@extends ('layouts.form')

@section('content')

<style type="text/css">
    .signin{
        text-align: center;
    }
    #forgetpassword{
        /*color:#5cb85c;*/
        color:silver;

    }
</style>

<form class="form-horizontal" role="form" action=" {{ URL::route('account-sign-in-post')}}" method="post" >


 @if ($errors->has('username')){{ $errors->first('username')}} @endif
 <div class="form-group">
    <label for=""> Email </label>
    <input placeholder="Email"  type="text" class="form-control" required name="username" {{ ( Input::old('username')) ? 'value="'.e(Input::old('username')).'"' : '' }}>
</div><br>



@if ($errors->has('password')){{ $errors->first('password')}} @endif
<div class="form-group">
    <label for=""> Password </label>
    <input placeholder="Password"  type="password" class="form-control" required name="password">
</div><br>


<br>


<button type="submit" class="btn btn-success btn-sm btn-block ">Sign In </button>


    {{ Form::token() }}


</form><br>


<div class="col-lg-12 text-center">

    <a id="forgetpassword" href="{{ URL::route('account-forgot-password') }}"> Forget Password </a> <br> 


</div>



@stop  

I am sure that I typed in the right username and password because I double check with my database.

It keep redirecting me back to my sign-in page. with('error','Username/Password Wrong or account has not been activated !')

Can someone please tell me, if I did anything that I’m not suppose to ?


Solution

  • In your situation, you should check your auth variable in your Sign_In Function.

    According to your code,

    $auth = Auth::attempt(array(
                'username' => strtolower(Input::get('username')),
                'password' => Input::get('password'),
                'active' => 1),
            $remember);
    

    Keep in mind that, these are things need to make sure

    • username must match the database
    • password must match the database
    • user active must be 1

    If any of these fail, therefore, it STOP you from signing in.

    • Since, you're so sure about username and password, what about user active ?
    • Did you check to if it's 1 ?

    If Not

    on your set-password function or anywhere, where you normally set your user active.

    just do this :

    $user->active = '1';
    $user->save();
    

    Let me know if this work!!