<?php

namespace App\Http\Controllers\Account;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Traits\CommonTrait;

class CertifyController extends Controller
{
    use CommonTrait;

    public function __construct()
    {
        $this->middleware(['front', 'auth']);
    }

    /**
     * 비밀번호 확인
     * @method GET
     * @see /account/certify
     */
    public function index(Request $request)
    {
        $request->session()->reflash();
        $callbackURL = $request->session()->get('url.intended', session('url'));

        return view(layout('account.certify'), [
            'callbackURL' => $callbackURL,
            'menuID' => 'CERTIFY'
        ]);
    }

    /**
     * 비밀번호 인증
     * @method POST
     * @see /account/certify
     */
    public function update(Request $request)
    {
        $certified = $this->passwordAuthed($request->post('password'));
        if ($certified) {
            $request->session()->flash('is-certified', 1);
            $callbackUrl = urldecode($request->post('callback_url'));
            return redirect($callbackUrl);
        } else {
            return back()->withErrors(['password' => '비밀번호가 일치하지 않습니다.'])->withInput();
        }
    }
}
