I am trying upgrade Symfony application from 4.1 to 4.4 and I have error in authentication in SecurityController I have
public function loginAction (Request $request)
{
/* var AuthenticationUtils $authUtils */
$authUtils= $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('admin/user/login.html.twig', array(
'error' => $error,
'last_username' => $lastUsername,
));
}
but after upgrade I have this error
how I may solve this error?
..as mentioned by #dbrunmann
public function loginAction (Request $request, AuthenticationUtils $authUtils)
{
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('admin/user/login.html.twig', array(
'error' => $error,
'last_username' => $lastUsername,
));
}
Use php bon\console debug:autowiring <some_service_name> - to find out which typhint to use to get the right service
E.g. php bin\console debug:autowiring utils will give you
Related
Strange behavior. If enter any phrase in the link, that should lead to a 404 page. Instead, an 404 error is raised:
Unable to generate a URL for the named route "login" as such route does not exist.
Login
Even if this part of the code is commented out, there will be an error for every other path.
For all other cases, everything works and controller links are generated.
Controller:
/**
* #Route("/login", name="login")
* #param AuthenticationUtils $authenticationUtils
* #return Response
*/
public function userLogin(AuthenticationUtils $authenticationUtils)
{
if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return $this->redirect($this->generateUrl('home_page'));
}
// 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,
]
);
}
Is there any way to refresh User session? I have my controller with change locale form, once it submitted i want to refresh that session with new set locale. I already tried to use session migrate() ,but unsuccessfully.
Any ideas?
Part of my controller
/**
* #route("/changeLocale", name="changeLocale")
*/
public function changeLocale(Request $request, Session $session)
{
$form = $this->createFormBuilder(null)
->add('locale', ChoiceType::class, [
'choices' => [
'Français' => 'fr_FR',
'Czech' => 'cs_CZ',
'English(US)' => 'en_US'
]
])
->add('save', SubmitType::class)
->getForm()
;
$form->handleRequest($request);
if ($form->isSubmitted()) {
$em = $this->getDoctrine()->getManager();
$locale = $form->getData()['locale'];
$user = $this->getUser();
$user->setLocale($locale);
$em->persist($user);
$em->flush();
$session->migrate();
}
return $this->render('admin/dashboard/locale.html.twig', [
'form' => $form->createView()
]);
}
That's the wrong way.
When you authenticate, you save the locale of your user entity in the session. Right? If you have a LocaleSubscriber that saves the locale in your session, recreating your session won't work. The locale is only saved in the session at authentication.
If you change the locale, you just need to update the session.
if ($form->isSubmitted()) {
$em = $this->getDoctrine()->getManager();
$locale = $form->getData()['locale'];
$user = $this->getUser();
$user->setLocale($locale);
$em->persist($user);
$em->flush();
// Update the session
$request->getSession()->set('_locale', $locale);
}
ERROR:
I am trying to login as admin and i am defined the guards but there is an error in the validator that validator() must be of the type array in 158 line.
BranchController:
public function authenticateBranchAdmin(Request $request){
$validator = Validator($request, [
Line 158-> 'email' => 'required|email',
'password' => 'required'
]);
if($validator->passes()){
if(Auth::guard('branch')->attempt([
'email' => $request->email,
'password' => $request->password,
])){
return redirect('/branch'.'/'.Auth::guard('branch')->id);
}else{
if($this->AdminIsVerified($request->email)){
$request->session()->flash('message', 'Invalid email or password!');
}else{
$request->session()->flash('message', 'Please Register this Account!');
}
return redirect('/Admin/login');
}
}else{
return redirect('/Admin/login')->withErrors($validator)->withInput();
}
}
You need to convert $request to array, as array is expected by Validator, so instead of $request use $request->all().
I am having a code for login which is in my AuthController like this.
public function login(Request $request){
$email = $request->input('email');
$password = $request->input('password');
$validation = array(
'email' =>'required',
'password' => 'required');
$validator = Validator::make($request->all(), $validation);
if ($validator->fails()) {
$messages = $validator->messages();
return redirect('login_with_assismo')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
if (auth()->authenticate()) {
return redirect()->intended('welcome');
}
}
}
When i use this is i think login performs but it redirect me to the page somthng like this
localhost:8000/login
Anyone help me how to authenticate login am i doing something wrong or what. Please get the solution for this.
You can use attempt() function to login the user as:
public function login(Request $request)
{
$inputs = $request->only('email', 'password');
$rules = array(
'email' =>'required',
'password' => 'required'
);
$validator = Validator::make($inputs, $rules);
if ($validator->fails()) {
$messages = $validator->messages();
return redirect('login_with_assismo')
->withErrors($validator)
->withInput($request->except('password'));
}
if (auth()->attempt($inputs)) {
return redirect()->intended('welcome');
}
return redirect()->back()->withInput();
}
You may want to use the Auth facade, as described here:
https://laravel.com/docs/5.3/authentication#authenticating-users
Dont panic bro its because of Auth middleware go with #amit's code just go to
App\Http\Middleware\RedirectIfAuthenticated
and replace this route with your's
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
after that you are good to go
I want to redirect and send my request to another route but this method is not working.
return $this->redirect($this->generateUrl(
'admin_platform_create',
array('request' => $request)
));
When I arrive into admin_platform_create it doesn't pass that if
public function createAction(Request $request)
{
if ($request->getMethod() == 'POST')
{
Though this is working,
return $this->forward('AdminPlatformBundle:Manage:edit',
array('request' => $request));
It is not what I need as it is not writing the new URL.
Thanks for your help!
If you are using Symfony 2.6 or later, you can do it:
return $this->redirectToRoute('route', [
'request' => $request
], 307);