| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace App\Module\OAuth\Controllers;
- use App\Http\Controllers\Controller;
- use App\Module\OAuth\Services\AuthService;
- use Illuminate\Http\Request;
- class LoginController extends Controller
- {
- protected $auth;
- public function __construct(AuthService $auth)
- {
- $this->auth = $auth;
- }
- /**
- * 显示登录页面
- */
- public function showLoginForm(Request $request)
- {
- $data = $request->session()->all();
- dump($data);
- // 如果已经登录,检查是否有待处理的授权请求
- if ($this->auth->check()) {
- if ($authorizeParams = session('oauth_authorize_params')) {
- session()->forget('oauth_authorize_params');
- return redirect()->route('oauth.authorize', $authorizeParams);
- }
- return redirect('/');
- }
- return view('oauth::login');
- }
- /**
- * 处理登录请求
- */
- public function login(Request $request)
- {
- $credentials = $request->validate([
- 'username' => 'required|string',
- 'password' => 'required|string',
- ]);
- if ($user = $this->auth->attempt($credentials['username'], $credentials['password'])) {
- // 检查是否有待处理的授权请求
- if ($authorizeParams = session('oauth_authorize_params')) {
- session()->forget('oauth_authorize_params');
- return redirect()->route('oauth.authorize', $authorizeParams);
- }
- return redirect('/');
- }
- return back()->withErrors([
- 'username' => '用户名或密码错误',
- ])->withInput();
- }
- /**
- * 退出登录
- */
- public function logout(Request $request)
- {
- $this->auth->logout();
- return redirect()->route('login');
- }
- }
|