<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use Redirect;
use Session;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backend';

    protected $maxAttempts = 5;
    protected $decayMinutes = 10;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }
    
    protected function validateLogin(Request $request)
    {
        $validateRules = [
            'g-recaptcha-response' => 'required',
        ];

        $validateMessage = [
            'g-recaptcha-response.required' => '請點選我不是機器人',
        ];

        $request->validate($validateRules, $validateMessage);

        $recaptcha = new \ReCaptcha\ReCaptcha('6LfkEvohAAAAABqmn1qQTNZnacY2rI2Z0hfzJO8-');      // reCaptcha secretKey
        $resp = $recaptcha->verify($request->input('g-recaptcha-response'), $request->ip());
        if (!$resp->isSuccess()) {
            throw ValidationException::withMessages(['error' => 'recaptcha 驗證失敗']);
        }
    }
}
