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('/');
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With