0

Following the guide to implement the native mail verification of laravel. Brings me an error.

Note please that i use MongoDB, therefore i'm using Jensseger/laravel-mongodb package

This is the error: Class App\User contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\MustVerifyEmail::hasVerifiedEmail, Illuminate\Contracts\Auth\MustVerifyEmail::markEmailAsVerified, Illuminate\Contracts\Auth\MustVerifyEmail::sendEmailVerificationNotification

I've already try to implement the methods inside my model and they seem to solve the problem. But it won't send any emails.

Here's what i've implemented im my User.php model

    * Determine if the user has verified their email address.
    *
    * @return bool
    */
    public function hasVerifiedEmail()
    {}

    /**
    * Mark the given user's email as verified.
    *
    * @return bool
    */
    public function markEmailAsVerified()
    {}

    /**
    * Send the email verification notification.
    *
    * @return void
    */
    public function sendEmailVerificationNotification()
    {}

Here's my User.php model

namespace App;

use App\Company;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    protected $connection = 'mongodb';

Here's my web.php route file.

Route::get('/', function () {
    return view('welcome');
});

Auth::routes(['verify' => true]);

Route::get('/home', 'HomeController@index')->name('home');

And here's my HomeController.php

    public function __construct()
    {
        $this->middleware(['auth','verified']);
    }

Here's my env file

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=xxxxxxxxxxx
MAIL_PASSWORD=xxxxxxxxxxxx
MAIL_ENCRYPTION=tls

Like this the project work but it wont send emails. Do i need to put the logic inside the three method inside the User.php? If yes what should i put in it? I've no idea because if it's native and work like charm with SQL i don't really know how to get it work on my project Hope someone has a solution for this. Thanks

Hichem BOUSSETTA
  • 1,791
  • 1
  • 21
  • 27
McMazalf
  • 59
  • 1
  • 9

1 Answers1

1

Easiest solution is to implement trait Illuminate\Auth\MustVerifyEmail which should be there, however it is not mentioned in the Laravel documentation. You can also override these methods by defining them in the model as you did. But hasVerifiedEmail and markEmailAsVerified methods should have some verification logic and return bool based on the API.

Edit: I also forgot to mention that method sendEmailVerificationNotification should contain $this->notify(new Notifications\VerifyEmail); otherwise it won't use the Notifiable trait and thus not send any email. For more details take a look at the method in Laravel framework repository,

Silencesys
  • 545
  • 5
  • 12
  • Hi @Silencesys, thanks for answering. If using Contracts\Auth\ etc\MustverifyEmail and the one that you mentioned (illuminate\Auth\MistVerifyEmail) which one should I implement? Anyway if you go to the file that you mentioned, it is an interface and not a trait, so it’s impossible to use it in the model. Do you have suggestion how to complete the logic methods ? – McMazalf May 18 '19 at 13:23
  • Hello, both files that I mentioned are traits - I checked them again. I would suggest implementing both, the trait and the interface. You can import the trait like `use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait` and then use it in your User model. Or if you don't like this idea, you can implement or rather copy-paste content of the MustVerifyEmail trait to your model [see the trait](https://github.com/laravel/framework/blob/5.8/src/Illuminate/Auth/MustVerifyEmail.php) on Laravel Github. – Silencesys May 18 '19 at 13:58
  • Thank you very much my friend. It worked with only implementing those two files in my model :) – McMazalf May 20 '19 at 12:43