<?php
namespace App\Controller\User;
use App\Service\MobileDetectService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class LoginController extends AbstractController
{
private $urlGenerator;
private $mobileDetectService;
function __construct(UrlGeneratorInterface $urlGenerator, MobileDetectService $mobileDetectService)
{
$this->urlGenerator = $urlGenerator;
$this->mobileDetectService = $mobileDetectService;
}
/**
* @Route("/", name="check_user")
*/
public function check()
{
if($this->getUser()){
if($this->isGranted('ROLE_ADMIN')){
return $this->redirectToRoute('admin_google_professional_save_locations');
}else{
if($this->mobileDetectService->isMobile()){
return $this->redirectToRoute('professional_mobile_home');
}else{
return $this->redirectToRoute('professional_dashboard');
}
}
}else{
return $this->redirectToRoute('app_login');
}
}
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, Request $request): Response
{
// if($request->getScheme() === "http"){
// $secureLogin = "https:" . $this->generateUrl("app_login", [], UrlGeneratorInterface::NETWORK_PATH);
// dump($request->getScheme());
// // return $this->redirect($secureLogin);
// }
if($this->getUser() !== null){
$roles = $this->getUser()->getRoles();
if(in_array("ROLE_ADMIN", $roles)){
return $this->redirectToRoute("home");
}else{
return $this->redirectToRoute("professional_dashboard");
}
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('user/security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{ }
/**
* @Route("/not-found", name="not_found")
*/
public function notFound(){
return new Response($this->renderView("layouts/not-found.html.twig"), 404);
// return ;
}
}