Moslem Cherif Moslem Cherif - 1 month ago 9
PHP Question

Laravel Authentification doesn't work

I'm working with laravel 5.2, and I have a problem with customized authentification. It doesn't log the user on .

Here you can find my code for the function :


public function login(Request $request)
{
$this->validateLogin($request);

$email = $request->get('email');
$user = User::where('email', $email)->first();

if(! is_null($user))
{
if (Auth::login($user))
{
return redirect('/profile');
}
}
else
{
$this->register($request);
}
}


In the place of Auth::login, I tried also the function attempt and check, but nothing was working ...

It logs the user, but it showed a blank page, without redirecting to the url..

Can you help me plz?

Thanks alot

Answer

You are seeing a blank page instead of being redirect to '/profile' because your second if condition is never true as

Auth::login($user)

never returns a null even if user is logged in so basically

if (Auth::login($user))
    {
        return redirect('/profile');
    }

this is if condition is never satisfied in your case.

You can try this

public function login(Request $request)
{
    $this->validateLogin($request);

    $email = $request->get('email');
    $user = User::where('email', $email)->first();

    if(! is_null($user))
    {
        Auth::login($user);
        if (Auth::check())
        {
            return redirect('/profile');
        }
    }
    else
    {
        $this->register($request);
    }
}


Auth::check();

will return true if the user is logged in and will be redirected to '/profile'.