用于用户注册的 OTP 是否应该存储在 laravel 中的会话或数据库中?
Are OTP for user registration supposed to store in session or datbase in laravel?
OTP 应该存储在会话或数据库中。谁能告诉OTP的流程。据我所知,当用户提交必要的字段时,用户详细信息和 otp 将存储在数据库中,注册后打开另一个表单输入 otp,然后注册最终成功。但我不明白实际的逻辑。要存储 otp,我们需要将所有数据存储在数据库中,所有数据都被存储(用户信息),然后我们才能验证 otp。我正在使用会话,但我不确定代码是否正确,
public function otpVerify(Request $request)
{
$data = $request->validate([
'verification_code' => ['required', 'numeric'],
'phone_number' => ['required', 'string'],
]);
$otp = $request->session()->get('otp');
$enteredOtp = $request->session()->get('otp');
if ($otp == $enteredOtp) {
$user = tap(User::where('phone_number', $data['phone_number']));
// ->update(['isVerified' => true]);
return success([
$success,
$otp
], __('User created successfully'));
} else {
return problem([], 500, 'OTP Doesnt Match');
}
public function register(RegisterUserRequest $request)
{
$user = new User($request->validated());
$otp = rand(10000, 99999);
$otp_expires_time = Carbon::now()->addSeconds(20);
if (!env('APP_ENV') === 'local') {
$sms = AWS::createClient('sns');
$sms->publish([
'Message' => 'Your OTP code is:' + $otp,
'PhoneNumber' => $user->phone_number,
'MessageAttributes' => [
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Transactional',
]
],
]);
} else {
Log::channel('otplog')->info('Your OTP code is:'. $otp);
}
$status = $user->save();
$user->roles()->attach($request->role_id);
$user->brands()->attach($request->brand_id);
$user->appliances()->attach($request->appliance_id);
$success['token'] = $user->createToken('MyAuthApp')->plainTextToken;
$success['name'] = $user->name;
Session::put('OTP', $otp, 'expiry_time',$otp_expires_time);
if ($status) {
return success([
$success,
$otp_expires_time,
$otp
], __('User created successfully'));
} else {
return problem([], 500, 'USER_REGISTER_FAIL');
}
}
存储在数据库中是一个不错的选择
OTP 应该存储在会话或数据库中。谁能告诉OTP的流程。据我所知,当用户提交必要的字段时,用户详细信息和 otp 将存储在数据库中,注册后打开另一个表单输入 otp,然后注册最终成功。但我不明白实际的逻辑。要存储 otp,我们需要将所有数据存储在数据库中,所有数据都被存储(用户信息),然后我们才能验证 otp。我正在使用会话,但我不确定代码是否正确,
public function otpVerify(Request $request)
{
$data = $request->validate([
'verification_code' => ['required', 'numeric'],
'phone_number' => ['required', 'string'],
]);
$otp = $request->session()->get('otp');
$enteredOtp = $request->session()->get('otp');
if ($otp == $enteredOtp) {
$user = tap(User::where('phone_number', $data['phone_number']));
// ->update(['isVerified' => true]);
return success([
$success,
$otp
], __('User created successfully'));
} else {
return problem([], 500, 'OTP Doesnt Match');
}
public function register(RegisterUserRequest $request)
{
$user = new User($request->validated());
$otp = rand(10000, 99999);
$otp_expires_time = Carbon::now()->addSeconds(20);
if (!env('APP_ENV') === 'local') {
$sms = AWS::createClient('sns');
$sms->publish([
'Message' => 'Your OTP code is:' + $otp,
'PhoneNumber' => $user->phone_number,
'MessageAttributes' => [
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Transactional',
]
],
]);
} else {
Log::channel('otplog')->info('Your OTP code is:'. $otp);
}
$status = $user->save();
$user->roles()->attach($request->role_id);
$user->brands()->attach($request->brand_id);
$user->appliances()->attach($request->appliance_id);
$success['token'] = $user->createToken('MyAuthApp')->plainTextToken;
$success['name'] = $user->name;
Session::put('OTP', $otp, 'expiry_time',$otp_expires_time);
if ($status) {
return success([
$success,
$otp_expires_time,
$otp
], __('User created successfully'));
} else {
return problem([], 500, 'USER_REGISTER_FAIL');
}
}
存储在数据库中是一个不错的选择