@digitalwing And what happens when two students have the same date of birth…?
Laravel login using date of birth instead of password
I am using Laravel 9x I have built a student service application
I want to allow users to login using their Register number and Date of Birth instead of email and password. Such users are treated as guest users and will have limited previlages. I have added a guest_user guard in congig/auth.php
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'guest_user' => [ 'driver' => 'session', 'provider' => 'guest_users', ], ],
In LoginController I wrote
function guestLogin(Request $request) { $this->validate($request, [ 'regno' => 'required|max:10', 'dob' => 'required|date' ]);
$regno=strtoupper($request->input('regno'));
$dob=$request->input('dob');
if (Auth::guard('guest_user')->attempt(['regno' => $regno, 'password' => $dob], $request->get('remember')))
{
return redirect()->intended('/guestlogin');
}
return back()->withInput($request->only('regno', 'remember'));
}
In the model Student(Which is the provider for guest_user) I have added
namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Auth\User as Authenticatable;
class student extends Authenticatable { protected $connection='my_connection'; protected $table = 'student;
public function getAuthPassword()
{
return $this->dob;
}
}
Still when I try to login in returns to the same page with Register Number i.e. return back()->withInput($request->only('regno', 'remember')); in LoginController Might be it is doing an encryption on the dob entered (A wild guess!!) before comparing with that id table.
How can I use dob instead of password to login?
One way to accomplish this is by logging in the student yourself.
<?php
$student = Student::query()
->where('regno', '=', $request->input('regno'))
->where('dob', '=', $request->input('dob'))
->first();
if ($student === null) {
// incorrect credentials
}
Auth::guard('guest_user')->login($student);
But I agree with @martinbean that you should be careful and always take security seriously, especially with personal information - depending what students get access to.
Please or to participate in this conversation.