Sending a verification email when registring from the api - laravel

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

Related

Authorize Channel in Laravel Package

I am developing a laravel package where i need to authorize a private channel.
In my normal laravel project(not the package) everything is working fine:
Event SendMessage:
public function broadcastOn()
{
return new PrivateChannel('chat');
}
and in my route/channel.php i do as follows:
Broadcast::channel('chat', function ($user) {
return Auth::check();
});
But how can i authorize my chat channel inside my laravel package?
In my laravel package the service provider has boot and register functions, as with routes/web.php i know we can register it but what to do with the channels? Can we register them? i find no documentation regarding that Please help.
I am using pusher server and with laravel echo.
Hopefully you can understand my question. I just wanna know how can i authroize channel in my package.
The answer to my own question is that We must uncomment a line from config/app.php which is :
App\Providers\BroadcastServiceProvider::class,
I was struggling with the channels and it comes to the point that , we do not need to register the channels or broadcasting services in our package.
They are by default globally enabled.
So everything remains the same and just by uncommenting that i was able to authorize the private channel.
Hoepfully nobody gets into such trouble.
Cheers.

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.

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
}

register a public api route in laravel nova without authentication

I am developing a card for Laravel nova.
As part of this, I want an API route that can be posted, but I don't want to have to authenticate against it.
I have registered my route in the card's api.php
Route::post('/endpoint/{id}', function (Request $request, $id) {)
This works if I call it with an already authenticated session.
But if I try to call it from postman I get
HTTP 419 Sorry, your session has expired. Please refresh and try again.
I can see that the card service provider is registering the route as so
Route::middleware(['nova'])
->prefix('nova-vendor/NovaPusherCard')
->group(__DIR__.'/../routes/api.php');
So I guess that Nova is putting some authenticated in front of the route.
Is there a way I can register the route without adding authentication?
ok so I worked it out.
I just needed to update the middleware to api instead of nova.

Laravel 5.6 - Authenticate without password, using email address only

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

Resources