2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
namespace App\Http\Controllers;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-11-17 17:32:12 +08:00
|
|
|
use App\Events;
|
2019-12-14 11:10:37 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
|
|
|
use App\Mail\ForgotPassword;
|
2018-08-17 17:03:38 +08:00
|
|
|
use App\Models\Player;
|
2019-12-14 11:10:37 +08:00
|
|
|
use App\Models\User;
|
2019-12-22 11:50:39 +08:00
|
|
|
use App\Rules;
|
2019-12-14 11:10:37 +08:00
|
|
|
use Auth;
|
2020-03-02 15:02:39 +08:00
|
|
|
use Blessing\Filter;
|
2019-12-14 11:10:37 +08:00
|
|
|
use Cache;
|
2019-12-21 15:50:29 +08:00
|
|
|
use Carbon\Carbon;
|
2019-12-24 17:09:30 +08:00
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
2016-09-04 15:35:12 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-12-15 11:19:10 +08:00
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
2019-12-14 11:10:37 +08:00
|
|
|
use Mail;
|
|
|
|
use Session;
|
|
|
|
use URL;
|
2019-12-24 23:59:25 +08:00
|
|
|
use Vectorface\Whip\Whip;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-03 23:50:55 +08:00
|
|
|
class AuthController extends Controller
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2020-03-02 15:02:39 +08:00
|
|
|
public function login(Filter $filter)
|
2019-09-18 23:06:48 +08:00
|
|
|
{
|
2019-12-24 23:59:25 +08:00
|
|
|
$whip = new Whip();
|
|
|
|
$ip = $whip->getValidIpAddress();
|
2020-03-02 15:02:39 +08:00
|
|
|
$ip = $filter->apply('client_ip', $ip);
|
2019-12-24 23:59:25 +08:00
|
|
|
|
2019-09-18 23:06:48 +08:00
|
|
|
return view('auth.login', [
|
|
|
|
'extra' => [
|
2019-12-24 23:59:25 +08:00
|
|
|
'tooManyFails' => cache(sha1('login_fails_'.$ip)) > 3,
|
2019-09-18 23:06:48 +08:00
|
|
|
'recaptcha' => option('recaptcha_sitekey'),
|
|
|
|
'invisible' => (bool) option('recaptcha_invisible'),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
public function handleLogin(
|
|
|
|
Request $request,
|
|
|
|
Rules\Captcha $captcha,
|
2020-03-02 15:02:39 +08:00
|
|
|
Dispatcher $dispatcher,
|
|
|
|
Filter $filter
|
2019-12-24 17:09:30 +08:00
|
|
|
) {
|
2016-09-03 23:50:55 +08:00
|
|
|
$this->validate($request, [
|
2016-10-02 20:30:27 +08:00
|
|
|
'identification' => 'required',
|
2019-12-14 11:10:37 +08:00
|
|
|
'password' => 'required|min:6|max:32',
|
2016-09-03 23:50:55 +08:00
|
|
|
]);
|
|
|
|
|
2016-10-02 20:30:27 +08:00
|
|
|
$identification = $request->input('identification');
|
2019-12-24 17:09:30 +08:00
|
|
|
$password = $request->input('password');
|
2018-02-16 17:31:04 +08:00
|
|
|
// Guess type of identification
|
2019-03-23 11:06:36 +08:00
|
|
|
$authType = filter_var($identification, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
|
2016-09-03 23:50:55 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.login.attempt', [$identification, $password, $authType]);
|
2018-02-16 17:31:04 +08:00
|
|
|
event(new Events\UserTryToLogin($identification, $authType));
|
2016-10-17 17:51:51 +08:00
|
|
|
|
2019-03-23 11:06:36 +08:00
|
|
|
if ($authType == 'email') {
|
2019-08-24 10:22:26 +08:00
|
|
|
$user = User::where('email', $identification)->first();
|
2019-03-23 11:06:36 +08:00
|
|
|
} else {
|
|
|
|
$player = Player::where('name', $identification)->first();
|
2019-12-24 17:09:30 +08:00
|
|
|
$user = optional($player)->user;
|
2019-03-23 11:06:36 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2018-08-16 17:57:24 +08:00
|
|
|
// Require CAPTCHA if user fails to login more than 3 times
|
2019-12-24 23:59:25 +08:00
|
|
|
$whip = new Whip();
|
|
|
|
$ip = $whip->getValidIpAddress();
|
2020-03-02 15:02:39 +08:00
|
|
|
$ip = $filter->apply('client_ip', $ip);
|
2019-12-24 23:59:25 +08:00
|
|
|
$loginFailsCacheKey = sha1('login_fails_'.$ip);
|
2018-08-16 17:57:24 +08:00
|
|
|
$loginFails = (int) Cache::get($loginFailsCacheKey, 0);
|
|
|
|
|
|
|
|
if ($loginFails > 3) {
|
2019-04-04 11:04:13 +08:00
|
|
|
$this->validate($request, ['captcha' => ['required', $captcha]]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 11:10:37 +08:00
|
|
|
if (!$user) {
|
2016-09-15 09:20:02 +08:00
|
|
|
return json(trans('auth.validation.user'), 2);
|
2019-12-24 17:09:30 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.login.ready', [$user]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
if ($user->verifyPassword($request->input('password'))) {
|
|
|
|
Session::forget('login_fails');
|
|
|
|
Cache::forget($loginFailsCacheKey);
|
2016-10-17 17:51:51 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
Auth::login($user, $request->input('keep'));
|
2017-06-28 20:42:51 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.login.succeeded', [$user]);
|
|
|
|
event(new Events\UserLoggedIn($user));
|
2016-08-16 13:27:06 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
return json(trans('auth.login.success'), 0, [
|
|
|
|
'redirectTo' => $request->session()->pull('last_requested_path', url('/user')),
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$loginFails++;
|
|
|
|
Cache::put($loginFailsCacheKey, $loginFails, 3600);
|
|
|
|
$dispatcher->dispatch('auth.login.failed', [$user, $loginFails]);
|
|
|
|
|
|
|
|
return json(trans('auth.validation.password'), 1, [
|
|
|
|
'login_fails' => $loginFails,
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
public function logout(Dispatcher $dispatcher)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2019-12-24 17:09:30 +08:00
|
|
|
$user = Auth::user();
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.logout.before', [$user]);
|
|
|
|
Auth::logout();
|
|
|
|
$dispatcher->dispatch('auth.logout.after', [$user]);
|
|
|
|
|
|
|
|
return json(trans('auth.logout.success'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function register()
|
|
|
|
{
|
2016-10-23 11:41:52 +08:00
|
|
|
if (option('user_can_register')) {
|
2019-03-24 09:58:37 +08:00
|
|
|
return view('auth.register', [
|
2019-09-18 23:06:48 +08:00
|
|
|
'site_name' => option_localized('site_name'),
|
2019-03-24 09:58:37 +08:00
|
|
|
'extra' => [
|
2019-03-31 16:07:36 +08:00
|
|
|
'player' => (bool) option('register_with_player_name'),
|
2019-03-24 09:58:37 +08:00
|
|
|
'recaptcha' => option('recaptcha_sitekey'),
|
2019-03-24 15:45:50 +08:00
|
|
|
'invisible' => (bool) option('recaptcha_invisible'),
|
2019-04-19 19:36:36 +08:00
|
|
|
],
|
2019-03-24 09:58:37 +08:00
|
|
|
]);
|
2016-07-27 18:31:59 +08:00
|
|
|
} else {
|
2016-09-15 09:20:02 +08:00
|
|
|
throw new PrettyPageException(trans('auth.register.close'), 7);
|
2016-07-27 18:31:59 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
public function handleRegister(
|
|
|
|
Request $request,
|
|
|
|
Rules\Captcha $captcha,
|
2020-03-02 15:02:39 +08:00
|
|
|
Dispatcher $dispatcher,
|
|
|
|
Filter $filter
|
2019-12-24 17:09:30 +08:00
|
|
|
) {
|
2019-12-14 11:10:37 +08:00
|
|
|
if (!option('user_can_register')) {
|
2018-08-17 17:03:38 +08:00
|
|
|
return json(trans('auth.register.close'), 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
$rule = option('register_with_player_name') ?
|
2019-12-22 11:50:39 +08:00
|
|
|
['player_name' => [
|
|
|
|
'required',
|
|
|
|
new Rules\PlayerName(),
|
|
|
|
'min:'.option('player_name_length_min'),
|
|
|
|
'max:'.option('player_name_length_max'),
|
|
|
|
]] :
|
2019-12-22 10:46:10 +08:00
|
|
|
['nickname' => 'required|max:255'];
|
2018-08-17 17:03:38 +08:00
|
|
|
$data = $this->validate($request, array_merge([
|
2019-12-14 11:10:37 +08:00
|
|
|
'email' => 'required|email|unique:users',
|
2018-02-24 16:05:07 +08:00
|
|
|
'password' => 'required|min:8|max:32',
|
2019-12-14 11:10:37 +08:00
|
|
|
'captcha' => ['required', $captcha],
|
2018-08-17 17:03:38 +08:00
|
|
|
], $rule));
|
2016-09-03 23:50:55 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.registration.attempt', [$data]);
|
|
|
|
|
2020-01-12 12:09:58 +08:00
|
|
|
if (
|
|
|
|
option('register_with_player_name') &&
|
|
|
|
Player::where('name', $request->input('player_name'))->count() > 0
|
|
|
|
) {
|
|
|
|
return json(trans('user.player.add.repeated'), 2);
|
2016-10-23 11:41:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If amount of registered accounts of IP is more than allowed amounts,
|
2018-08-17 22:54:26 +08:00
|
|
|
// then reject the register.
|
2019-12-24 23:59:25 +08:00
|
|
|
$whip = new Whip();
|
|
|
|
$ip = $whip->getValidIpAddress();
|
2020-03-02 15:02:39 +08:00
|
|
|
$ip = $filter->apply('client_ip', $ip);
|
2019-12-24 23:59:25 +08:00
|
|
|
if (User::where('ip', $ip)->count() >= option('regs_per_ip')) {
|
2018-08-17 22:54:26 +08:00
|
|
|
return json(trans('auth.register.max', ['regs' => option('regs_per_ip')]), 7);
|
|
|
|
}
|
2018-08-17 17:03:38 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.registration.ready', [$data]);
|
|
|
|
|
2019-12-14 11:10:37 +08:00
|
|
|
$user = new User();
|
2018-08-17 22:54:26 +08:00
|
|
|
$user->email = $data['email'];
|
|
|
|
$user->nickname = $data[option('register_with_player_name') ? 'player_name' : 'nickname'];
|
|
|
|
$user->score = option('user_initial_score');
|
|
|
|
$user->avatar = 0;
|
2019-07-30 15:12:31 +08:00
|
|
|
$user->password = $user->getEncryptedPwdFromEvent($data['password'])
|
2018-08-17 22:54:26 +08:00
|
|
|
?: app('cipher')->hash($data['password'], config('secure.salt'));
|
2019-12-24 23:59:25 +08:00
|
|
|
$user->ip = $ip;
|
2018-08-17 22:54:26 +08:00
|
|
|
$user->permission = User::NORMAL;
|
2019-12-21 15:50:29 +08:00
|
|
|
$user->register_at = Carbon::now();
|
|
|
|
$user->last_sign_at = Carbon::now()->subDay();
|
2018-07-20 14:42:43 +08:00
|
|
|
|
2018-08-17 22:54:26 +08:00
|
|
|
$user->save();
|
2016-10-23 11:41:52 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.registration.completed', [$user]);
|
2018-08-17 22:54:26 +08:00
|
|
|
event(new Events\UserRegistered($user));
|
|
|
|
|
|
|
|
if (option('register_with_player_name')) {
|
2019-12-14 11:10:37 +08:00
|
|
|
$player = new Player();
|
2019-03-02 22:58:37 +08:00
|
|
|
$player->uid = $user->uid;
|
2019-03-13 13:16:51 +08:00
|
|
|
$player->name = $request->get('player_name');
|
2019-03-02 22:58:37 +08:00
|
|
|
$player->tid_skin = 0;
|
2018-08-17 22:54:26 +08:00
|
|
|
$player->save();
|
|
|
|
|
|
|
|
event(new Events\PlayerWasAdded($player));
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
2018-08-17 22:54:26 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.login.ready', [$user]);
|
2018-08-17 22:54:26 +08:00
|
|
|
Auth::login($user);
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.login.succeeded', [$user]);
|
2018-08-17 22:54:26 +08:00
|
|
|
|
2019-04-23 19:14:41 +08:00
|
|
|
return json(trans('auth.register.success'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function forgot()
|
|
|
|
{
|
2020-03-09 12:29:00 +08:00
|
|
|
if (config('mail.default') != '') {
|
2019-03-27 11:07:04 +08:00
|
|
|
return view('auth.forgot', [
|
|
|
|
'extra' => [
|
|
|
|
'recaptcha' => option('recaptcha_sitekey'),
|
|
|
|
'invisible' => (bool) option('recaptcha_invisible'),
|
2019-04-19 19:36:36 +08:00
|
|
|
],
|
2019-03-27 11:07:04 +08:00
|
|
|
]);
|
2016-08-06 19:12:39 +08:00
|
|
|
} else {
|
2018-08-17 12:32:44 +08:00
|
|
|
throw new PrettyPageException(trans('auth.forgot.disabled'), 8);
|
2016-08-06 19:12:39 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
public function handleForgot(
|
|
|
|
Request $request,
|
|
|
|
Rules\Captcha $captcha,
|
2020-03-02 15:02:39 +08:00
|
|
|
Dispatcher $dispatcher,
|
|
|
|
Filter $filter
|
2019-12-24 17:09:30 +08:00
|
|
|
) {
|
|
|
|
$data = $this->validate($request, [
|
|
|
|
'email' => 'required|email',
|
2019-04-04 11:04:13 +08:00
|
|
|
'captcha' => ['required', $captcha],
|
2018-08-12 16:00:21 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2020-03-09 12:29:00 +08:00
|
|
|
if (!config('mail.default')) {
|
2018-08-17 12:32:44 +08:00
|
|
|
return json(trans('auth.forgot.disabled'), 1);
|
|
|
|
}
|
2016-08-06 19:12:39 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$email = $data['email'];
|
|
|
|
$dispatcher->dispatch('auth.forgot.attempt', [$email]);
|
|
|
|
|
2018-08-17 16:07:24 +08:00
|
|
|
$rateLimit = 180;
|
2019-12-24 23:59:25 +08:00
|
|
|
$whip = new Whip();
|
|
|
|
$ip = $whip->getValidIpAddress();
|
2020-03-02 15:02:39 +08:00
|
|
|
$ip = $filter->apply('client_ip', $ip);
|
2019-12-24 23:59:25 +08:00
|
|
|
$lastMailCacheKey = sha1('last_mail_'.$ip);
|
2018-08-17 16:07:24 +08:00
|
|
|
$remain = $rateLimit + Cache::get($lastMailCacheKey, 0) - time();
|
|
|
|
if ($remain > 0) {
|
2019-04-23 19:14:41 +08:00
|
|
|
return json(trans('auth.forgot.frequent-mail'), 2);
|
2018-08-17 16:07:24 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$user = User::where('email', $email)->first();
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-12-14 11:10:37 +08:00
|
|
|
if (!$user) {
|
2016-09-15 09:20:02 +08:00
|
|
|
return json(trans('auth.forgot.unregistered'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.forgot.ready', [$user]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2020-03-28 22:36:27 +08:00
|
|
|
$url = URL::temporarySignedRoute(
|
|
|
|
'auth.reset',
|
|
|
|
now()->addHour(),
|
|
|
|
['uid' => $user->uid],
|
|
|
|
false
|
|
|
|
);
|
2016-09-04 16:15:11 +08:00
|
|
|
try {
|
2019-12-24 17:09:30 +08:00
|
|
|
Mail::to($email)->send(new ForgotPassword($url));
|
2018-07-15 18:15:55 +08:00
|
|
|
} catch (\Exception $e) {
|
2018-07-22 11:36:00 +08:00
|
|
|
report($e);
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.forgot.failed', [$user, $url]);
|
2018-07-15 18:15:55 +08:00
|
|
|
|
2018-08-16 18:10:09 +08:00
|
|
|
return json(trans('auth.forgot.failed', ['msg' => $e->getMessage()]), 2);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.forgot.sent', [$user, $url]);
|
2019-02-27 23:44:50 +08:00
|
|
|
Cache::put($lastMailCacheKey, time(), 3600);
|
2016-09-04 16:15:11 +08:00
|
|
|
|
2018-08-16 18:10:09 +08:00
|
|
|
return json(trans('auth.forgot.success'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-09-07 11:15:23 +08:00
|
|
|
public function reset($uid)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2019-08-24 10:22:26 +08:00
|
|
|
return view('auth.reset')->with('user', User::find($uid));
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
public function handleReset(Dispatcher $dispatcher, Request $request, $uid)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2020-03-28 22:36:27 +08:00
|
|
|
abort_unless($request->hasValidSignature(false), 403, trans('auth.reset.invalid'));
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
['password' => $password] = $this->validate($request, [
|
2018-02-24 16:05:07 +08:00
|
|
|
'password' => 'required|min:8|max:32',
|
2016-09-03 23:50:55 +08:00
|
|
|
]);
|
2019-12-24 17:09:30 +08:00
|
|
|
$user = User::find($uid);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.reset.before', [$user, $password]);
|
|
|
|
$user->changePassword($password);
|
|
|
|
$dispatcher->dispatch('auth.reset.after', [$user, $password]);
|
2016-11-07 22:34:34 +08:00
|
|
|
|
2016-09-15 09:20:02 +08:00
|
|
|
return json(trans('auth.reset.success'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 12:23:46 +08:00
|
|
|
public function captcha(\Gregwar\Captcha\CaptchaBuilder $builder)
|
|
|
|
{
|
|
|
|
$builder->build(100, 34);
|
|
|
|
session(['captcha' => $builder->getPhrase()]);
|
2019-09-07 11:00:35 +08:00
|
|
|
|
2019-09-05 12:23:46 +08:00
|
|
|
return response($builder->output(), 200, [
|
|
|
|
'Content-Type' => 'image/jpeg',
|
|
|
|
'Cache-Control' => 'no-store',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:01:39 +08:00
|
|
|
public function fillEmail(Request $request)
|
|
|
|
{
|
|
|
|
$email = $this->validate($request, ['email' => 'required|email|unique:users'])['email'];
|
|
|
|
$user = $request->user();
|
|
|
|
$user->email = $email;
|
|
|
|
$user->save();
|
2019-05-19 13:49:44 +08:00
|
|
|
|
2019-04-25 13:01:39 +08:00
|
|
|
return redirect('/user');
|
|
|
|
}
|
|
|
|
|
2020-03-28 22:36:27 +08:00
|
|
|
public function verify(Request $request, $uid)
|
2016-10-23 11:41:52 +08:00
|
|
|
{
|
2019-12-14 11:10:37 +08:00
|
|
|
if (!option('require_verification')) {
|
2018-08-17 12:32:44 +08:00
|
|
|
throw new PrettyPageException(trans('user.verification.disabled'), 1);
|
|
|
|
}
|
|
|
|
|
2020-03-28 22:36:27 +08:00
|
|
|
abort_unless($request->hasValidSignature(false), 403, trans('auth.verify.invalid'));
|
|
|
|
|
2019-08-24 10:22:26 +08:00
|
|
|
$user = User::find($uid);
|
2018-08-17 12:32:44 +08:00
|
|
|
|
2019-12-14 11:10:37 +08:00
|
|
|
if (!$user || $user->verified) {
|
2018-08-17 12:32:44 +08:00
|
|
|
throw new PrettyPageException(trans('auth.verify.invalid'), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->verified = true;
|
|
|
|
$user->save();
|
|
|
|
|
2019-09-18 23:06:48 +08:00
|
|
|
return view('auth.verify', ['site_name' => option_localized('site_name')]);
|
2016-10-23 11:41:52 +08:00
|
|
|
}
|
2019-04-23 10:05:58 +08:00
|
|
|
|
2019-04-25 13:29:43 +08:00
|
|
|
public function jwtLogin(Request $request)
|
2019-04-23 10:05:58 +08:00
|
|
|
{
|
2019-04-25 13:29:43 +08:00
|
|
|
$token = Auth::guard('jwt')->attempt([
|
2019-04-23 10:05:58 +08:00
|
|
|
'email' => $request->email,
|
2019-05-19 13:49:44 +08:00
|
|
|
'password' => $request->password,
|
2019-04-26 18:58:12 +08:00
|
|
|
]) ?: '';
|
2019-04-23 10:05:58 +08:00
|
|
|
|
|
|
|
return json(compact('token'));
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:29:43 +08:00
|
|
|
public function jwtLogout()
|
2019-04-23 10:05:58 +08:00
|
|
|
{
|
2019-04-25 13:29:43 +08:00
|
|
|
Auth::guard('jwt')->logout();
|
2019-05-19 13:49:44 +08:00
|
|
|
|
2019-04-23 10:05:58 +08:00
|
|
|
return response('', 204);
|
|
|
|
}
|
2019-04-23 12:45:06 +08:00
|
|
|
|
2019-04-25 13:29:43 +08:00
|
|
|
public function jwtRefresh()
|
2019-04-23 12:45:06 +08:00
|
|
|
{
|
2019-04-25 13:29:43 +08:00
|
|
|
return json(['token' => Auth::guard('jwt')->refresh()]);
|
2019-04-23 12:45:06 +08:00
|
|
|
}
|
2019-12-15 11:19:10 +08:00
|
|
|
|
|
|
|
public function oauthLogin($driver)
|
|
|
|
{
|
|
|
|
return Socialite::driver($driver)->redirect();
|
|
|
|
}
|
|
|
|
|
2020-03-02 15:02:39 +08:00
|
|
|
public function oauthCallback(Dispatcher $dispatcher, Filter $filter, $driver)
|
2019-12-15 11:19:10 +08:00
|
|
|
{
|
|
|
|
$remoteUser = Socialite::driver($driver)->user();
|
|
|
|
|
|
|
|
$email = $remoteUser->email;
|
|
|
|
if (empty($email)) {
|
|
|
|
abort(500, 'Unsupported OAuth Server which does not provide email.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::where('email', $email)->first();
|
2019-12-24 17:09:30 +08:00
|
|
|
if (!$user) {
|
2019-12-24 23:59:25 +08:00
|
|
|
$whip = new Whip();
|
|
|
|
$ip = $whip->getValidIpAddress();
|
2020-03-02 15:02:39 +08:00
|
|
|
$ip = $filter->apply('client_ip', $ip);
|
2019-12-24 23:59:25 +08:00
|
|
|
|
2019-12-15 11:19:10 +08:00
|
|
|
$user = new User();
|
|
|
|
$user->email = $email;
|
|
|
|
$user->nickname = $remoteUser->nickname ?? $remoteUser->name ?? $email;
|
|
|
|
$user->score = option('user_initial_score');
|
|
|
|
$user->avatar = 0;
|
|
|
|
$user->password = '';
|
2019-12-24 23:59:25 +08:00
|
|
|
$user->ip = $ip;
|
2019-12-15 11:19:10 +08:00
|
|
|
$user->permission = User::NORMAL;
|
2019-12-21 15:50:29 +08:00
|
|
|
$user->register_at = Carbon::now();
|
|
|
|
$user->last_sign_at = Carbon::now()->subDay();
|
2019-12-15 17:58:38 +08:00
|
|
|
$user->verified = true;
|
2019-12-15 11:19:10 +08:00
|
|
|
|
|
|
|
$user->save();
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.registration.completed', [$user]);
|
2019-12-15 11:19:10 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
$dispatcher->dispatch('auth.login.ready', [$user]);
|
|
|
|
Auth::login($user);
|
|
|
|
$dispatcher->dispatch('auth.login.succeeded', [$user]);
|
|
|
|
|
2019-12-15 11:19:10 +08:00
|
|
|
return redirect('/user');
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|