So I have the back end in Laravel and applied Laravel for tenancy into it. the front end is my nuxt. whenever I register, I want it should send an email for email verification which is working perfectly fine !! so this is my register method
public function register(Request $request) {
$user = [];
$tenant = Tenant::create(['name' => $request->first_name . 'Team']);
$tenant->run(function () use ($request){
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder']);
$user = User::create([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'email' => $request->email,
'password' => Hash::make($request->password),
'phone_number' => $request->phone_number,
// 'verification_token' => Str::random(60),
]);
//return $user;
event(new Registered($user));
});
$tenant->domains()->create([
'domain' => $request->domain_name,
]);
// $tenant->users()->attach($user->id);
return response()->json(['message' => 'Successfully created - Check your email to verify!']);
}
whenever I click on Verify Email address button in my email, it navigates to http://domain1.localhost:8000/email/verify/2/2022b4a0a511f6ec4652cb178731383316eedf89?expires=1715858060&signature=d6fbdc6d1f5eeae04850be5215287f90f38601fdcc7dd87fd120b49d32a5890f because my server is running on domain1.localhost:8000
So my first link to send notice of email is working perfectly fine —
Route::middleware([
'web',
InitializeTenancyByDomain::class,
PreventAccessFromCentralDomains::class,
])->group(function () {
Route::get('/email/verify', function () {
return view('auth.verify-email');
})->name('verification.notice');
/* Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
$request->fulfill();
return redirect('/home');
})->name('verification.verify'); */
Route::get('/email/verify/{id}/{hash}', [VerifyEmailController::class, '__invoke'])
->middleware(['signed', 'throttle:6,1'])
->name('verification.verify');
// Route::get('/verification/{token}', [AuthController::class, 'verify_email']);
But then at the click of the verify button, it required to sign and auth two middleware as per documentation which I removed in the above link. I don’t want it should be logged in, so I have created my own controller named VerifyEmailController
. which has the following code –
<?php
namespace AppHttpControllers;
use AppModelsUser;
use IlluminateHttpRequest;
use IlluminateAuthEventsVerified;
use IlluminateAuthAccessAuthorizationException;
use LaravelFortifyContractsVerifyEmailResponse;
class VerifyEmailController extends Controller
{
public function __invoke(Request $request)
{
$user = User::findOrFail($request->route('id'));
auth()->login($user);
if (! hash_equals(sha1($user->getEmailForVerification()), (string) $request->route('hash'))) {
throw new AuthorizationException;
}
if ($user->hasVerifiedEmail()) {
return app(VerifyEmailResponse::class);
}
if ($user->markEmailAsVerified()) {
event(new Verified($user));
}
return app(VerifyEmailResponse::class);
}
}
still, whenever I execute, an email is being sent, and then on the click of the verify button, it displays
Method IlluminateAuthRequestGuard::login does not exist.
In USER Modal, I have done this –
class User extends Authenticatable implements MustVerifyEmail{
use Notifiable;
Features::emailVerification(), is listed. Please help me verify the email and set the verified column in the database as per tenant.Below are my routes for api.php –
Route::post('/login', [AuthController::class, 'login']);
Route::post('/register', [AuthController::class, 'register']);
Route::get('/csrf', [CsrfCookieController::class, 'get'])->name('csrf');
Route::middleware([
'api',
InitializeTenancyByDomain::class,
PreventAccessFromCentralDomains::class,
])->group(function () {
Route::get('/user', function (Request $request) {
$user = $request->user();
if ($user->hasRole('Admin')) {
$user['scope'] = array('Admin');
}
if ($user->hasRole('Driver')) {
$user['scope'] = array('Driver');
}
if ($user->hasRole('Employee')) {
$user['scope'] = array('Employee');
}
$data = [
'user' => $user
];
return $data;
});
});
Route::prefix('admin')->middleware([
'api',
InitializeTenancyByDomain::class,
PreventAccessFromCentralDomains::class,
])->group(function () {
/**
* Bookings
*/
//Route::resource('/tenants', TenantController::class);
Route::get('/email/verify/{id}/{hash}', [VerifyEmailController::class, '__invoke'])
->middleware(['signed', 'throttle:6,1'])
->name('verification.verify');
Route::post('/bookings/change-recall-status', [BookingController::class, 'change_recall_status']);
});
Route::prefix('driver')->middleware('auth:sanctum')->group(function () {
Route::get('/trips/recall', [TripController::class , 'get_recall_rides']);
});
In my auth – i have
'defaults' => [
'guard' => 'sanctum',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'sanctum' => [
'driver' => 'sanctum',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppModelsUser::class,
],
2
Answers
To customize email notification for email verification based on the current route prefix in a multi-tenant Laravel application, you can use the VerifyEmail::toMailUsing() method. Here’s a breakdown of the steps involved:
Define Routes: Ensure you have separate routes for email verification for different user types or tenants, such as /admin/email/verify and /staff-portal/email/verify.
Customize Email Notification: Utilize the VerifyEmail::toMailUsing() method to customize the email notification for email verification. Determine the route and URL dynamically based on the current route prefix.
View Creation: Create a view for the email verification email template (emails.verify-email), and pass the verification URL to it.
Email Sending: Laravel’s built-in VerifyEmail notification will be triggered when a user registers and needs to verify their email. Laravel will automatically send the email using the defined notification method.
Ensure you have defined the appropriate routes for email verification for each user type or tenant in your web.php routes file. By dynamically determining the route and URL for email verification based on the current route prefix, you can ensure that the email verification email notification works correctly for each user type or tenant in your multi-tenant Laravel application.
add the customization of email notification for email verification in your multi-tenant Laravel application within the AppServiceProvider, you can use the boot() method.
Have you added middleware guest to registration controller