Laravel 5.6 - Authenticate without password, using email address only - laravel-5.6

I would like to authenticate users providing their email address only, using Laravel 5.6.
Is there a way to achieve that with the Auth::login()?
Thanks

Try this...
$user = User::where('email', $email)->first();
Auth::login($user);
The given User object must be an implementation of the Illuminate\Contracts\Auth\Authenticatable contract. Of course, the App\User model included with Laravel already implements this interface:
official doc link

Related

laravel authentication without using email field

I am new to laravel. 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. However I want to authenticate users based on their registration number. How do I achieve this.

Laravel how to get auth user details in apiResources controller

How can I get auth user details in api controller in laravel
I am using laravel version 5.8
This is my apiResources route
Route::apiResources([
'employeeapi' => 'API\EmployeeController',
]);
and my controller is
class EmployeeController extends Controller {
public function store(Request $request)
{
}
}
How can I access Auth::user() inside the store function?
You could use the following middleware on your api routes to use laravel sessions if the user was logged in to a session in one of your web routes.
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
However you probably want your api to use some kind of token based authentication. laravel passport has great tools for that. Because using sessions in an api might become messy.

Sending a verification email when registring from the api

I'm trying to get Lavarel to send a verification email when you register through the API. Is this possible?
I've added the MustVerifyEmail implement to the User class and added Auth::routes(['verify' => true]); to almost the top of my api.php file (Where my routes are). Also the SMTP server is configured correctly.
I think I might be doing something wrong. Hope to hear from you :)
P.S. I've started using Laravel for the first time today. So it might just be a really simple mistake.
If you are using a custom RegisterController you should be able to call
$user->sendEmailVerificationNotification();
this in turn calls the
$this->notify(new Notifications\VerifyEmail);
This is because your user implements MustVerifyEmail in the Illuminate\Foundation\Auth\User.
EDIT----
You can also use the event new Registered which should be registered in the EventServiceProvider this will call the SendEmailVerificationNotification which in turn will call $event->user->sendEmailVerificationNotification();
event(new Registered($user = $this->create($request->all())));
Note: this works in laravel 5.7

Laravel authentication lifecycle

I'm trying to understand how an authentication request is handled by laravel in order to be able to write my own authentication method that allows me to check data from multiple tables before authenticating (basically there are some columns that I need to check to understand if a user is able to login).
I'm actually quietly confused about the relation between guards, middleware, provider and driver.
Can someone please explain me the lifecycle of a login request?
Thank you
EDIT: i'm working with laravel 5.7, standard Auth scaffolding that is available using make:auth
To make a custom authentication follow this steps
1.go to routes/web.php and make a get route to handle login view and post login route to handle login logic
Route::get('login','LoginController#show')
Route::post('login','LoginController#login')
2. Make a controller called LoginController
php artisan make:controller LoginController
4.inside LoginController make a function called login to handle login logic like this
public function login(){
$input = $this->validate(request(),['username'=>'required','password'=>'required']);
$model = CustomUsersModel::where('username',$input['username'])
->where('password',bcrypt($input['password']))->first();
if($model){
//user exist and valid login information
auth()->login($model);//login user via model
//now user loggedin
}
//handle wrong login information
}

Laravel get user info by Jwt token

I'm using laravel as my backend.
Once the user has logged into my app, all I have is just a token without any information regards the username or any other user details.
Once I retrive the JWT Token and store it in my frontend (angular2), how I can get the user details?
For the record, i'm using the following laravel library:
https://github.com/tymondesigns/jwt-auth
To get the user information
First get the token from JWTAuth like this:
$token = JWTAuth::getToken();
Then get the user from the token as follows:
$user = JWTAuth::toUser($token);
you can get user information like this way :
$user = JWTAuth::toUser(your_token_here);
I have tested just
$user = JWTAuth::toUser();
Without any parameter and it works as well
I am using Laravel 7.3 with tymon/jwt-auth 1.0
JWTAuth::parseToken()->authenticate();

Resources