v2board/app/Http/Controllers/Passport/LoginController.php

95 lines
2.9 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Http\Controllers\Passport;
use Illuminate\Http\Request;
use App\Http\Requests\Passport\LoginIndex;
use App\Http\Controllers\Controller;
use App\Models\User;
2020-01-12 01:27:36 +08:00
use Illuminate\Support\Facades\Cache;
2019-12-28 17:31:56 +08:00
use App\Utils\Helper;
2019-10-29 15:33:36 +08:00
class LoginController extends Controller
{
2020-01-11 13:36:52 +08:00
public function index(LoginIndex $request)
{
2019-10-29 15:33:36 +08:00
$email = $request->input('email');
$password = $request->input('password');
2020-01-11 13:36:52 +08:00
2019-10-29 15:33:36 +08:00
$user = User::where('email', $email)->first();
if (!$user) {
abort(500, '用户名或密码错误');
}
if (!password_verify($password, $user->password)) {
abort(500, '用户名或密码错误');
}
2020-01-11 13:36:52 +08:00
2020-01-20 23:04:42 +08:00
if ($user->banned) {
2020-01-20 14:47:36 +08:00
abort(500, '该账户已被停止使用');
}
2019-10-29 15:33:36 +08:00
$request->session()->put('email', $user->email);
$request->session()->put('id', $user->id);
if ($user->is_admin) {
$request->session()->put('is_admin', true);
}
2019-12-02 16:46:09 +08:00
return response([
'data' => [
'is_admin' => $user->is_admin ? 2 : 1,
'token' => $user->token
]
]);
2019-10-29 15:33:36 +08:00
}
2019-12-25 15:16:30 +08:00
2020-01-11 13:36:52 +08:00
public function token2Login(Request $request)
{
2019-12-28 17:31:56 +08:00
if ($request->input('token')) {
$user = User::where('token', $request->input('token'))->first();
if (!$user) {
return header('Location:' . config('v2board.app_url'));
}
2019-12-28 17:34:08 +08:00
$code = Helper::guid();
$key = 'token2Login_' . $code;
2020-01-17 00:20:54 +08:00
Cache::put($key, $user->id, 600);
2020-01-11 13:36:52 +08:00
$redirect = '/#/login?verify=' . $code . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
2019-12-28 17:31:56 +08:00
if (config('v2board.app_url')) {
$location = config('v2board.app_url') . $redirect;
} else {
$location = url($redirect);
}
return header('Location:' . $location);
2019-12-25 15:16:30 +08:00
}
2019-12-28 17:31:56 +08:00
if ($request->input('verify')) {
$key = 'token2Login_' . $request->input('verify');
$userId = Cache::get($key);
2019-12-28 17:31:56 +08:00
if (!$userId) {
abort(500, '令牌有误');
}
$user = User::find($userId);
if (!$user) {
abort(500, '用户不存在');
}
2020-01-20 20:12:13 +08:00
if ($user->banned) {
2020-01-20 14:47:36 +08:00
abort(500, '该账户已被停止使用');
}
2019-12-25 15:16:30 +08:00
$request->session()->put('email', $user->email);
$request->session()->put('id', $user->id);
if ($user->is_admin) {
$request->session()->put('is_admin', true);
}
Cache::forget($key);
2019-12-28 17:31:56 +08:00
return response([
'data' => true
]);
2019-12-25 15:16:30 +08:00
}
}
2019-12-27 15:28:03 +08:00
2020-01-11 13:36:52 +08:00
public function check(Request $request)
{
2019-12-27 15:28:03 +08:00
return response([
'data' => $request->session()->get('id') ? true : false
]);
}
2019-10-29 15:33:36 +08:00
}