Add helper function generate_random_token

This commit is contained in:
printempw 2018-07-27 18:46:32 +08:00
parent 459a232c26
commit e2575f8a9a
3 changed files with 15 additions and 6 deletions

View File

@ -112,11 +112,7 @@ class UserController extends Controller
return json(trans('user.verification.verified'), 1);
}
$key = config('app.key');
$key = starts_with($key, 'base64:') ? base64_decode(substr($key, 7)) : $key;
$token = hash_hmac('sha256', str_random(40), $key);
$token = generate_random_token();
$this->user->verification_token = $token;
$this->user->save();

View File

@ -518,3 +518,16 @@ if (! function_exists('can_moderate_texture')) {
return ($texture->uploader == $user->uid || $user->isAdmin());
}
}
if (! function_exists('generate_random_token')) {
function generate_random_token($key = null) {
if (is_null($key)) {
$key = config('app.key');
$key = starts_with($key, 'base64:') ? base64_decode(substr($key, 7)) : $key;
}
return hash_hmac('sha256', str_random(40), $key);
}
}

View File

@ -29,7 +29,7 @@ $factory->defineAs(User::class, 'unverified', function (Faker\Generator $faker)
'last_sign_at' => $faker->dateTime,
'register_at' => $faker->dateTime,
'verified' => 0,
'verification_token' => hash_hmac('sha256', str_random(40), 'key')
'verification_token' => generate_random_token()
];
});