Laravel login with more custom fields - laravel

I want a user to login using his/her id_number(school id number) only. Or if he forgets his id number, I want him to login using his Name, course and year graduated.
Is it possible to implement this using Laravel auth?
I am really new to laravel. I would appreciate if you could help me.
Thank you

After you use php artisan make:auth
You can see in LoginController, there is a trait AuthenticatesUsers. The default auth is on AuthenticatesUsers. It can be modify by your own function by overriding validateLogin, attemptLogin, credentials , etc...
for example
protected function validateLogin(Request $request)
{
$this->validate($request, [
'username' => 'required|string',
'ic_number' => 'required|string',
]);
}
protected function credentials(Request $request)
{
return $request->only('username', 'ic_number');
}

Related

Problem with using laravel Auth::logoutOtherDevices

I want to use Laravel Auth::logoutOtherDevices. I added \Illuminate\Session\Middleware\AuthenticateSession::class to $middlewareGroups => web in kernal.
When I use this in login function :
public function login(Request $request)
{
Auth::attempt([
'email' => $request->input('email'),
'password' => $request->input('password')
], $request->has('remember'));
Auth::logoutOtherDevices($request->input('password'));
}
All passwords in my database change and I can't login with other accounts. :| Where is the problem?
Before getting started, you should make sure that the Illuminate\Session\Middleware\AuthenticateSession middleware is present and un-commented in your App\Http\Kernel class' web middleware group:
'web' => [
// ...
\Illuminate\Session\Middleware\AuthenticateSession::class,
// ...
],
Then, you may use the logoutOtherDevices method provided by the Auth facade. This method requires the user to confirm their current password, which your application should accept through an input form
use Illuminate\Support\Facades\Auth;
Auth::logoutOtherDevices($currentPassword);
You should have sendLoginResponse method after attempt as it is inside of AuthenticateUsers trait, because sendLoginResponse implements $request->session()->regenerate(); in it.
A clean implementation of your purpose is to leave trait's login method intact and create authenticated method in the controller that has AuthenticateUsers trait and add below code it in.
protected function authenticated(Request $request, $user)
{
Auth::logoutOtherDevices($request->get('password'));
}

remove validation from email parameter in laravel 5.6. I am using authenticate() method

I am trying to authenticate the using using ajax and laravel 5.6 authenticate() method. If I pass 'email' and 'password' parameters then it works fine but If I change the email parameter to user_email then it gives me validation that "Email field is required".
So I need help to authenticate the user using different parameters (like user_email etc.) instead of email and password.
Here is the code file path app\Http\Controllers\AUth\LoginController.php
protected function authenticate(Request $request, $user){
if(!request->ajax()){
return response()->json([
'auth' => auth()->check(),
'user' => $user,
'intented' => $this->redirectPath()
]);
}
}
Thank you
Paste this on your login controller
public function username()
{
return 'user_email';
}

i'm getting a null user id when i create a new artisan in a laravel and vue project

Here is my controller that holds the store function
public function store(Request $request)
{
$artisan = Artisan::create($request->all() + ['user_id' => Auth::user()]);
return $artisan;
}
Can figure out why Auth::user() is pushing a null value to the db instead of picking the current authenticated user id.
please help guys.
Auth::user() returns a instance of the User class (if the user is logged in).
You have to pick the id from it, like this:
$user_id = Auth::user()->id;
To get user data from Auth::user(), you have to follow this.
You have to use Auth class for login.
if (Auth::attempt(['email' => $email, 'password' => $password])) {}
You have to include all the routes under web middleware where you want to access authenticated user data.
Route::group(['middleware' => ['web']], function () {
Route::post('/dashboard', 'Controller#dashboard');
}
As the value comes from Session and web middleware has Session
activated.

Laravel authentication using mobile number

I'm currently using Laravel 5.5, as you know Laravel by default offers authentication by email, but in my case I want to change its behaviour and allow the user to login using the mobile number. so Where would I have to do the modifications?
From the docs:
By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController:
public function username()
{
return 'mobile_number';
}
https://laravel.com/docs/5.5/authentication#included-authenticating
LoginController
public function username()
{
return 'mobile';
}
also can validate
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string',
]);
}

changing the authentication parameters in Laravel 5.3 with Hesto multi Auth

I wan't to change the login parameters in authentication. I'm using the Hesto Multi Auth package.
By default Hesto package gives a multi authentication with email and a password. Instead what i wan't to do is change it to a local identity number and the password.
Hesto_Package
It is quite simple.
All i had to do is override the login() function in the required model's LoginController Class. If the Modeis admin then in adminAuth -> LoginController.
public function login(Request $request)
{
echo $request->NIC;
if (Auth::guard('admin')->attempt(['NIC' => $request->NIC, 'password' => $request->password])) {
//
}
}
then also add the field in Models class file.4
protected $fillable = [
'NIC', 'email', 'password',
];

Resources