<?php

namespace App\Http\Controllers;


/*
 * 用户认证控制器
 */

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;


class AuthController extends BaseController
{
    public function getLogin() {
        return view('login');
    }

    public function postLogin(Request $request)
    {
        // 登录逻辑
        $credentials = $request->only(['username', 'password']);

        $remember = (bool) $request->input('remember', false);

        $validator = Validator::make($credentials, [
            'username'          => 'required',
            'password'          => 'required',
        ]);

        if ($validator->fails()) {
            return ['status'=>'fail','message'=>$validator->errors()];
        }

        //去除空格
        $credentials = array_map('trim', $credentials);

        if (Auth::guard('web')->attempt($credentials, false)) {
          //  $request->session()->regenerate();
            // 登录成功
            return ['status'=>'success','message'=>'登录成功'];
        }
        // 登录失败
        return ['status'=>'fail','message'=>'用户名或密码错误'];
    }

    public function logout(Request $request)
    {
        // 登出逻辑
        Auth::guard('web')->logout();

        $request->session()->invalidate();

        return redirect('/');
    }

}
