Laravel - Redirect::to or Redirect::back giving Tokenmismatchexception error?

Asked

Viewed 496 times

0

The following is happening, I have the login screen that does some checks, in case you do not pass any in my Controller I make a redirect for url login message with the error.

This is where the error, after the redirect with the message, and I try to login again is giving TokenMismatchException, just stops giving the error if I give a refresh on the page.

I’ve tried to use:

return Redirect::to('login');

and

return Redirect::back()->withInput();`

but to no avail.

Follows the codes:

View:

<form class="sign-in form-horizontal shadow rounded no-overflow" action="{{url('login/doLogin')}}" method="post">
    {{ csrf_field() }}
    <input type="text" class="form-control input-sm" placeholder="E-mail" name="username">
    <input type="password" class="form-control input-sm" placeholder="Senha" name="password">
    <button type="submit" class="btn btn-theme btn-lg btn-block no-margin rounded" id="login-btn">Entrar</button>
</form>

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Hash;
use Auth;
use Redirect;

class AuthController extends BaseController
{
    public $request = null;
    public function __construct(Request $request) {
        $this->request = $request;
    }
    public function doLogin(){
        $erro = "Erro ao logar";
        $requestedUser = $this->request->get('username');
        $requestedPassword = $this->request->get('password');
        $usuario = User::where('email', '=', $requestedUser)->first();
        if ($usuario) {
            if (Hash::check($requestedPassword, $usuario->password)) {
                if (!$usuario->userValidation) {
                    Auth::loginUsingId($usuario->id);
                    $this->toastrMessage("success", 'Logado com sucesso!');
                    return redirect('dashboard');
                }else{
                    $erro = "Acesse o seu e-mail para autenticar sua conta";
                }
            }else{
                $erro = "Senha incorreta";
            }
        }else{
            $erro = "Usuário não encontrado";
        }
        $this->toastrMessage("error", $erro);
        return Redirect::back()->withInput();
        // return Redirect::to('login');
    }
}

Remark¹: the version of Laravel is the 5.4 and have done projects with previous versions of Laravel and in none gave this error.

Observation²: As an interim measure I put the route that logs in to except of VerifyCsrfToken.

  • Could you put all the code? Just the part of Redirect does not say much. It would be interesting also the part of View

  • Please put the view too.

  • See if your login route is of the type post. If it is, it could be the reason for the mistake.

  • Code added. But I believe that it is not a flaw of the code, because, as I said, I have done in previous versions of laravel and it worked smoothly. @Virgilionovic I believe you asked view to find out if I had put the {{ csrf_field() }}. As you can see, he’s there.

  • @Amandalima no, the route login is the type get

  • Why don’t you use the FormReques?

  • @Virgilionovic I don’t know this FormReques could give me some link on the subject?

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.