Check if it is authenticated

Asked

Viewed 1,328 times

0

I have the function authenticate which checks whether the e-mail and password entered match those of the users table. The code is working, but after checking which email and password check, I redirect to the view logged. So far so good. But I would like this view not to be accessed directly. I would like to make it accessible only to those who are logged in. Can you understand that? Because if I access the direct url of this view, it accesses it. 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;
    }

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 documentation of the Laravel, it is very good.

  • It worked! Even so I will read the documentation more calmly. Thank you

Browser other questions tagged

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