stormpath / stormpath-laravel

Build simple, secure web applications with Stormpath and Laravel
Other
29 stars 6 forks source link

Fatal error: Call to a member function getValue() on string #23

Closed georgeboot closed 8 years ago

georgeboot commented 8 years ago

The fix I found to solve this is to change line 125 of src/Support/StormpathLaravelServiceProvider.php

from:

$result = (new \Stormpath\Oauth\VerifyAccessToken(app('stormpath.application')))->verify($cookie->getValue());

to

$result = (new \Stormpath\Oauth\VerifyAccessToken(app('stormpath.application')))->verify($cookie);
kryten87 commented 8 years ago

Perfect timing - you posted this just as I ran into this problem! Thanks!

Rather than change the source files, I created a file with a helper function (app/Helpers/Stormpath.php). Here's the code:

function getUser()
{
    // get the stormpath access token cookie
    //
    $cookie = app()->request->cookie(config('stormpath.web.accessTokenCookie.name'));

    // do we have a cookie? if not, then just return null (since nobody is
    // logged in)
    //
    if(null === $cookie) {
        return null;
    }

    // attempt to retrieve the user details
    //
    try {
        // get the result from the web service
        //
        $result = (new \Stormpath\Oauth\VerifyAccessToken(app('stormpath.application')))->verify($cookie);

        // return the account information from the results
        //
        return $result->getAccount();
    } catch (\Exception $e) {}

    // if we get here, that means that the access token is not valid
    // (perhaps the user's session has timed out?). In that case, we'll just
    // act as if the user is not logged in.
    //
    return null;
}

The code is copied from the StormpathLaravelServiceProvider, with the changes @georgeboot described above.

Then I added this file to the autoload section in my composer.json:

"autoload": {
    ...
    "files": [
        "app/Helpers/Stormpath.php"
    ],
    ...
},

Now, when I want to get the user object, I simply call $user = getUser();.

This is just a temporary fix until the package is updated and fixes the problem.

bretterer commented 8 years ago

Thank you for pointing this out. I will get a fix out for this ASAP!

-Brian

chaseconey commented 8 years ago

:+1: also having this problem

bretterer commented 8 years ago

A patch for this will be in the next release. The issue is that during phpunit tests, the cookie comes back as an object, but in real live, the cookie is a string. Added in a check to see if object, then sets cookie to cookie->getValue()