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

56 lines
1.8 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;
class LoginController extends Controller
{
public function index (LoginIndex $request) {
$email = $request->input('email');
$password = $request->input('password');
$user = User::where('email', $email)->first();
if (!$user) {
abort(500, '用户名或密码错误');
}
if (!password_verify($password, $user->password)) {
abort(500, '用户名或密码错误');
}
$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
2019-12-25 15:19:28 +08:00
public function token2Login (Request $request) {
2019-12-25 15:16:30 +08:00
if (empty($request->input('token'))) {
abort(500, '参数错误');
}
$user = User::where('token', $request->input('token'))->first();
if ($user) {
$request->session()->put('email', $user->email);
$request->session()->put('id', $user->id);
if ($user->is_admin) {
$request->session()->put('is_admin', true);
}
}
$location = url('/#/' . $request->input('redirect') ? $request->input('redirect') : 'dashboard');
if (config('v2board.app_url')) {
2019-12-25 15:25:55 +08:00
$location = config('v2board.app_url') . '/#/' . $request->input('redirect') ? $request->input('redirect') : 'dashboard';
2019-12-25 15:16:30 +08:00
}
2019-12-25 15:24:37 +08:00
header('Location:' . $location);
2019-12-25 15:16:30 +08:00
}
2019-10-29 15:33:36 +08:00
}