如果otp已验证,如何在注册页面上重定向,否则重定向到登录Laravel? [英] How to redirect on register page if otp verified otherwise redirect to login Laravel?

查看:69
本文介绍了如果otp已验证,如何在注册页面上重定向,否则重定向到登录Laravel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 8和Firebase来通过OTP验证移动设备.现在,我希望如果OTP已验证,那么我可以访问注册页面(如果OTP未验证),然后重定向到登录页面.现在,我正在使用默认的Laravel注册表格,当OTP验证后,我将其存储在cookie中.我已经创建了中间件,但是如果未设置cookie,那么它就无法工作,那么我也可以访问注册,但我希望它不可访问.中间件代码是

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);
}

内核代码是

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,
];

网络文件是

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.

推荐答案

您在 web.php 文件中定义的使用 otpVerify 中间件的路由正在被覆盖通过 laravel/ui 包中的 Auth :: routes();

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();

使用 php artisan route:list 检查您的路线.

您需要将 register 路由定义移至 Auth :: routes()下.

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

Auth::routes();

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

这篇关于如果otp已验证,如何在注册页面上重定向,否则重定向到登录Laravel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆