<?php

namespace App\Http\Controllers\Auth;

use App\Http\Requests\Auth\LoginRequest;
use App\Providers\RouteServiceProvider;
use Illuminate\Routing\Controller;
use Portal\Ecuti\Model\Users;

class LoginController extends Controller
{
    /**
     * Display the login view.
     *
     * @return \Illuminate\Contracts\View\View
     */
    // public function show()
    // {
    //     return view('auth.login');
    // }

    public function show()
    {
        
        if(\request('id')){

            $email = \request('id');
            if($email){

                $userinfo = Users::where('email','=',$email)->first();
                if($userinfo){

                    auth()->loginUsingId($userinfo->id);

                    return redirect('/');

                }else{

                    $url = "".env('SSO_PORTAL')."/error?app=Ecuti&email=".$email;
                }

                
            }else
            {
               $url = "".env('SSO_PORTAL')."/error?app=Ecuti&email=".$email;
            }

        }else
        {
      
            $url = "".env('SSO_PORTAL')."/logs?url=".env('APP_URL')."/auth/login";

        }
        // return redirect($url);
        return view('auth.login');

    }


    /**
     * Handle an incoming authentication request.
     *
     * @param  \App\Http\Requests\Auth\LoginRequest  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(LoginRequest $request)
    {
        $request->authenticate();

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

        return redirect()->intended(RouteServiceProvider::HOME);
    }
}
