如何在laravel8的otp中设置过期时间?

问题描述

当我试图使 otp 到期时,它不起作用,但我不知道我做错了什么。我使用 Carbon 设置了到期时间,但仍然无法正常工作。我应该怎么办?。它需要在 app/config 或其他东西中进行配置吗?

这是我的控制器代码的一部分

            if(!$device_serial_number)
            {
              return ('error');
            } else {
              $otp = rand(100000,999999);
  //            Cache::put([$otp],Now()->addSeconds(20));

              $otp_expires_time = Carbon::Now()->addSeconds(20);
              Cache::put(['otp_expires_time'],$otp_expires_time);
              Log::info("otp = ".$otp);

              $user = User::where('phonenumber',$request->phonenumber)->update(['otp' => $otp]);
              $token = auth()->user()->createtoken('Laravel Password Grant Client')->accesstoken;
      //        Log::info($request);
      //        $user = User::where([['phonenumber','=',request('phonenumber')],['otp',request('otp')]])->first();

              return response()->json(array(
                'otp_expires_at'=> $otp_expires_time,'otp' => $otp,'token' => $token));

这是我的中间件代码的一部分

  Log::info($request);
  $user = User::where([['phonenumber',request('otp')],['otp_expires_time',request('otp_expires_time')]])->first();
  $otp_expires_time = Carbon::Now()->addSeconds(20);
  if($user)
  {
    if(!$otp_expires_time > Carbon::Now())
    {
      return response('the time expired');
    } else {

      Auth::login($user,true);

      return response()->json(array(
      'message' => 'You are logged in',['user' => auth()->user()]));

      return response('You are logged in');

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)