Verify that you are authenticated

0

I have the authenticate function that verifies that the email and password entered match those of the users table. The code is working, but after verifying that email and password check, I redirect to the logged in view. So far so good. But I wish this view would not be accessed directly. I would like to make it accessible only to those who are logged in. Did you understand? Because if I access the direct url of this view, it accesses. I want to restrict this.

public function autenticar(Request $request)
{
    //Pego dados do formulário de login e armazeno na variavel $dados
    $dados = $request->except('_token');

    //Depois de armazenar na variavel dados, separo em $email e $password
    $email = $dados['email'];
    $password = $dados['password'];

    //Faz a consulta e verifica se email digitado é igual email do banco
    $query = $this->user->where('email', $email)->first();

    //Verifica se o email existe
    if (!$query)
    {
        return null;
    }

    //Verifica se a senha digitada é igual a senha hash do banco
    if (Hash::check($password, $query->password))
    {
        return redirect()->route('logado');
    }
    else
    {
        return null;
    }
    
asked by anonymous 03.10.2017 / 23:29

1 answer

1

In your view you can do the following:

@if(Auth::check())
   //Conteúdo protegido
@endif

 @if(Auth::guest())
//Conteúdo para não logado(um form de login etc)
@endif

Anyway, there are other ways to do something like this but for learning purposes, it's a good start. Take a look at the laravel documentation, it's very good.

    
04.10.2017 / 02:54