Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use other table than user for login authentication

For my project I have used the student table for authentication and storing information. For student registration all the datas are inserted in the student tale fine unlike login authentication. The attributes of the student table are [id(primary key and auto incremented), username, password, stdname, stdage, created_at, updated_at].

Registration

public function post_register()
{
    $input = Input::all();
    $rules = array(
        'email' => 'required|email|Between:3, 64|unique:student,username',
        'studentPass' => 'required',
        'studentName' => 'required',
        'studentAge'  => 'integer|Min:18|Max:45');
    $messages = array(
        'email.required' => 'You forgot to enter your email id!',
        'email.between'  => 'Email must be between 3 to 64 character!',
        'studentPass.required' => 'You forgot to enter your password!',
        'studentName.required' => 'You forgot to enter student name!',
        'studentAge.integer'   => 'Student age must be a integer value!',
        'studentAge.min'   => 'Student age must be a greater than 18 years!',
        'studentAge.max'   => 'Student age must be a less than value 45 years!'

    ); /* Add your custom messages here */
    $validator = Validator::make($input, $rules, $messages);
    if($validator->fails())
    {
        return Redirect::to('register')->withErrors($validator);
    }
    else
    {
        $student = new student();
        $student->username = $input['email'];
        $student->password = $input['studentPass'];
        $student->stdname  = $input['studentName'];
        $student->stdage   = $input['studentAge'];
        $student->save();
        return Redirect::to('login');
    }
}

Login

public function post_index()
{
    $input = Input::all();
    $rules = array(
        'email' => 'required|email|Between:3, 64',
        'studentPass' => 'required');

    $messages = array(
        'email.required' => 'You forgot to enter your email id!',
        'email.between'  => 'Email must be between 3 to 64 character!',
        'studentPass.required' => 'You forgot to enter your password!'
    ); /* Add your custom messages here */

    $validator = Validator::make($input, $rules, $messages);

    if($validator->fails())
    {
        return Redirect::to('login')->withErrors($validator);
    }
    else
    {
        $credential = array('username' => $input['email'], 'password' => $input['studentPass']);
        $auth = Auth::attempt($credential);
        if(Auth::attempt($credential))
            return Redirect::to('index');
        else
            return Redirect::to('/');
    }
}
like image 295
Banshidhari Avatar asked Jun 25 '13 06:06

Banshidhari


1 Answers

In your registration page insert your password using HASH. Write the code as

public function post_register()
{
    $input = Input::all();
    $rules = array(
        'email' => 'required|email|Between:3, 64|unique:student,username',
        'studentPass' => 'required',
        'studentName' => 'required',
        'studentAge'  => 'integer|Min:18|Max:45');
    $messages = array(
        'email.required' => 'You forgot to enter your email id!',
        'email.between'  => 'Email must be between 3 to 64 character!',
        'studentPass.required' => 'You forgot to enter your password!',
        'studentName.required' => 'You forgot to enter student name!',
        'studentAge.integer'   => 'Student age must be a integer value!',
        'studentAge.min'   => 'Student age must be a greater than 18 years!',
        'studentAge.max'   => 'Student age must be a less than value 45 years!'

    );//Add your custom messages here
    $validator = Validator::make($input, $rules, $messages);
    if($validator->fails())
    {
        return Redirect::to('register')->withErrors($validator);
    }
    else
    {
        $password = Hash::make($input['studentPass']);
        $student = new student();
        $student->username = $input['email'];
        $student->password = $password;
        $student->stdname  = $input['studentName'];
        $student->stdage   = $input['studentAge'];
        $student->save();
        return Redirect::to('login');
    }
}

In Laravel password must be in HASH. I think after replacing the function post_register() like that your login authentication will work fine.

like image 165
Banshi Avatar answered Nov 08 '22 21:11

Banshi