How to redirect on register page if otp verified otherwise redirect to login Laravel?

本小妞迷上赌 提交于 2021-01-05 07:25:48

问题


I am using Laravel 8 and Firebase to verify mobile with OTP. Now I want that if OTP verified then i can access register page if OTP is not verified then redirect to login page. Now I am using default Laravel registration form and when OTP verified i stored in cookie. I have created middleware but its not work if cookie not set then also i access registration but i want it not accessable. Middle-ware code is,

public function handle(Request $request, Closure $next)
{
    if (Cookie::get('otpVerified')){
        return redirect()->route('register');
    }
    return $next($request);
}

Kernel code is,

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    'otpVerify' =>  \App\Http\Middleware\OTPVarification::class,
];

web file is,

Route::get('language/{key}', [SwitchLanguageController::class,  'switchLanguage'])->name('language');


Route::get('register', [RegisterController::class, 'showRegistrationForm'])->name('register')->middleware(['otpVerify']);

Auth::routes();
Route::group(['middleware' => 'auth'], function (){

Route::get('/404', [\App\Http\Controllers\ErrorController::class, 'notFound'])->name('404');
Route::get('/', [\App\Http\Controllers\Dashboard\DashboardController::class, 'index'])->name('dashboard');
});

how can i resolve this issue.


回答1:


The route you are defining in your web.php file that uses your otpVerify middleware is being overwritten by the laravel/ui package routes in Auth::routes();

Check your routes using php artisan route:list.

You need to move your register route definition below Auth::routes().



来源:https://stackoverflow.com/questions/65503121/how-to-redirect-on-register-page-if-otp-verified-otherwise-redirect-to-login-lar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!