From bdb10bed32c5f37df2f0872c3cb354e9b7a293bd Mon Sep 17 00:00:00 2001 From: tokumeikoi Date: Mon, 27 Jun 2022 01:47:18 +0800 Subject: [PATCH] update: add login with mail link api --- app/Console/Commands/Test.php | 8 - .../Controllers/Passport/AuthController.php | 54 +++++ app/Http/Routes/PassportRoute.php | 1 + app/Utils/CacheKey.php | 3 +- resources/lang/en-US.json | 4 +- resources/lang/zh-CN.json | 4 +- resources/views/mail/classic/login.blade.php | 195 ++++++++++++++++++ resources/views/mail/default/login.blade.php | 43 ++++ 8 files changed, 301 insertions(+), 11 deletions(-) create mode 100644 resources/views/mail/classic/login.blade.php create mode 100644 resources/views/mail/default/login.blade.php diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index d6fe0ffd..667e616c 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -2,15 +2,7 @@ namespace App\Console\Commands; -use App\Models\Order; -use App\Models\User; -use App\Utils\CacheKey; -use App\Utils\Helper; use Illuminate\Console\Command; -use Illuminate\Filesystem\Filesystem; -use Illuminate\Foundation\Console\ConfigCacheCommand; -use Illuminate\Support\Facades\Cache; -use Matriphe\Larinfo; class Test extends Command { diff --git a/app/Http/Controllers/Passport/AuthController.php b/app/Http/Controllers/Passport/AuthController.php index 9a0fff6d..22b6b224 100644 --- a/app/Http/Controllers/Passport/AuthController.php +++ b/app/Http/Controllers/Passport/AuthController.php @@ -6,6 +6,7 @@ use App\Http\Controllers\Controller; use App\Http\Requests\Passport\AuthRegister; use App\Http\Requests\Passport\AuthForget; use App\Http\Requests\Passport\AuthLogin; +use App\Jobs\SendEmailJob; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use App\Models\Plan; @@ -18,6 +19,59 @@ use ReCaptcha\ReCaptcha; class AuthController extends Controller { + public function loginWithMailLink(Request $request) + { + if (!(int)config('v2board.login_with_mail_link_enable')) { + abort(404); + } + $params = $request->validate([ + 'email' => 'required|email', + 'redirect' => 'nullable' + ]); + + if (Cache::get(CacheKey::get('LAST_SEND_LOGIN_WITH_MAIL_LINK_TIMESTAMP', $params['email']))) { + abort(500, __('Sending frequently, please try again later')); + } + + $user = User::where('email', $params['email'])->first(); + if (!$user) { + return response([ + 'data' => true + ]); + } + + $code = Helper::guid(); + $key = CacheKey::get('TEMP_TOKEN', $code); + Cache::put($key, $user->id, 300); + Cache::put(CacheKey::get('LAST_SEND_LOGIN_WITH_MAIL_LINK_TIMESTAMP', $params['email']), time(), 60); + + + $redirect = '/#/login?verify=' . $code . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard'); + if (config('v2board.app_url')) { + $link = config('v2board.app_url') . $redirect; + } else { + $link = url($redirect); + } + + SendEmailJob::dispatch([ + 'email' => $user->email, + 'subject' => __('Login to :name', [ + 'name' => config('v2board.app_name', 'V2Board') + ]), + 'template_name' => 'login', + 'template_value' => [ + 'name' => config('v2board.app_name', 'V2Board'), + 'link' => $link, + 'url' => config('v2board.app_url') + ] + ]); + + return response([ + 'data' => $link + ]); + + } + public function register(AuthRegister $request) { if ((int)config('v2board.register_limit_by_ip_enable', 0)) { diff --git a/app/Http/Routes/PassportRoute.php b/app/Http/Routes/PassportRoute.php index 2dba8fcc..76c8cfd2 100644 --- a/app/Http/Routes/PassportRoute.php +++ b/app/Http/Routes/PassportRoute.php @@ -18,6 +18,7 @@ class PassportRoute $router->post('/auth/forget', 'Passport\\AuthController@forget'); $router->post('/auth/getTempToken', 'Passport\\AuthController@getTempToken'); $router->post('/auth/getQuickLoginUrl', 'Passport\\AuthController@getQuickLoginUrl'); + $router->post('/auth/loginWithMailLink', 'Passport\\AuthController@loginWithMailLink'); // Comm $router->get ('/comm/config', 'Passport\\CommController@config'); $router->post('/comm/sendEmailVerify', 'Passport\\CommController@sendEmailVerify'); diff --git a/app/Utils/CacheKey.php b/app/Utils/CacheKey.php index 8babf579..de7cb593 100644 --- a/app/Utils/CacheKey.php +++ b/app/Utils/CacheKey.php @@ -19,7 +19,8 @@ class CacheKey 'TEMP_TOKEN' => '临时令牌', 'LAST_SEND_EMAIL_REMIND_TRAFFIC' => '最后发送流量邮件提醒', 'SCHEDULE_LAST_CHECK_AT' => '计划任务最后检查时间', - 'REGISTER_IP_RATE_LIMIT' => '注册频率限制' + 'REGISTER_IP_RATE_LIMIT' => '注册频率限制', + 'LAST_SEND_LOGIN_WITH_MAIL_LINK_TIMESTAMP' => '最后一次发送登入链接时间' ]; public static function get(string $key, $uniqueValue) diff --git a/resources/lang/en-US.json b/resources/lang/en-US.json index 1cbe6bb6..387f5519 100644 --- a/resources/lang/en-US.json +++ b/resources/lang/en-US.json @@ -90,5 +90,7 @@ "Request failed, please try again later": "Request failed, please try again later", "Register frequently, please try again after 1 hour": "Register frequently, please try again after 1 hour", "Uh-oh, we've had some problems, we're working on it.": "Uh-oh, we've had some problems, we're working on it", - "This subscription reset package does not apply to your subscription": "This subscription reset package does not apply to your subscription" + "This subscription reset package does not apply to your subscription": "This subscription reset package does not apply to your subscription", + "Login to :name": "Login to :name", + "Sending frequently, please try again later": "Sending frequently, please try again later" } diff --git a/resources/lang/zh-CN.json b/resources/lang/zh-CN.json index c80d2f8b..239a0e3e 100644 --- a/resources/lang/zh-CN.json +++ b/resources/lang/zh-CN.json @@ -90,5 +90,7 @@ "Request failed, please try again later": "请求失败,请稍后再试", "Register frequently, please try again after 1 hour": "注册频繁,请等待1小时后再次尝试", "Uh-oh, we've had some problems, we're working on it.": "遇到了些问题,我们正在进行处理", - "This subscription reset package does not apply to your subscription": "该订阅重置包不适用于你的订阅" + "This subscription reset package does not apply to your subscription": "该订阅重置包不适用于你的订阅", + "Login to :name": "登入到 :name", + "Sending frequently, please try again later": "发送频繁,请稍后再试" } diff --git a/resources/views/mail/classic/login.blade.php b/resources/views/mail/classic/login.blade.php new file mode 100644 index 00000000..91f916b5 --- /dev/null +++ b/resources/views/mail/classic/login.blade.php @@ -0,0 +1,195 @@ + + + + + + 邮箱验证码 + + + + + + + + + + +
+ +
+ + + + + + + +
+ {{$name}} +
+ + + + + + + + + + + + + + + + +
+ Dear Customer +
+ 您正在登入到{{$name}}, 请在 5 分钟内点击下方链接进行登入。如果您未授权该登入请求,请无视。 +
+ {{$link}} +
+ (本邮件由系统自动发出,请勿直接回复) +
+ 登录 {{$name}} +
+
+ +
+
+
+ + + diff --git a/resources/views/mail/default/login.blade.php b/resources/views/mail/default/login.blade.php new file mode 100644 index 00000000..5047acde --- /dev/null +++ b/resources/views/mail/default/login.blade.php @@ -0,0 +1,43 @@ +
+ + + + + + +
+
+ + + + + + + + + + + + + + + + +
{{$name}}
登入到{{$name}}
+ 尊敬的用户您好! +
+
+ 您正在登入到{{$name}}, 请在 5 分钟内点击下方链接进行登入。如果您未授权该登入请求,请无视。 + {{$link}} +
+
+
+ + + + + + +
返回{{$name}}
+
+