1

I'm working with laravel 5.2, and I have a problem with customized authentication. 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.

1 Answer 1

1

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'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.