vendredi 1 mai 2020

Laravel app logging out authenticated user when redirecting (it shouldn't)

I have created a method that logs a user into our site via a link from a separate website. We use a token and username to find the user, then use the built in login method to authenticate them (I can confirm this all works as expected).

Here is the function:

public function login($username, $token, $redirect)
{
    $accountLogin = AccountLogin::Where('username', $username)
        ->Where('token', $token)
        ->first();

    if ($accountLogin) {
        try {
            Auth::login($accountLogin);
            print_r(Auth::check()) // prints 1.
            header('Location: ' . $redirect);
            exit;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    echo(array(
       'status' => 'error',
       'message' => 'Error logging in'
    ));
}

My problem is that the user seems to be logged out after the redirect.

Within the function above I can see the the account is found, and Auth::check() returns 1 or if I echo out Auth::User() then I do get the users info.

We then attempt to do a redirect which should take us to the route passed in as a parameter (you do need to be authenticated for all these routes) but instead I am redirected to the home page and logged out. If I log in from that page then it takes me to the url I wanted to reach originally (so it's like it is remembering where I want to go, but just doesn't recognise that I'm logged in).

Both sites are currently running locally via apache.

TIA.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire