1
votes

I'm trying to knock up a quick prototype in Laravel which uses Parse as the backend. I've imported the Parse SDK using composer and correctly initialised it. I wish to extend the Laravel Auth to use Parse Users.

What I've done so far:

  • Subclassed ParseUser and made my subclass implement the AuthenticatableContract and CanResetPasswordContract.
  • Created a ParseUserProvider which implements the UserProvider
  • Extended Authentication to use my new 'Parse' driver

I can sign users up which seems to log them in but I'm unable to access any of the Auth methods like Auth::user() once they are logged in. I also can't log them out - I get an error "Argument 1 passed to Illuminate\Auth\Guard::refreshRememberToken() must implement interface Illuminate\Contracts\Auth\Authenticatable, array given". Additionally, if I try to login from fresh, I can see my data being retrieved from Parse and retrieveByCredentials/validateCredentials successfully firing however I still can't access the user properties. Any pointers for what may be going wrong? My Code:

class ParseAuthProvider extends ServiceProvider {

/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{
    $this->app['auth']->extend('parse',function()
    {
        return new ParseUserProvider( new ParseUser());
    });
}
...


class ParseUserProvider implements UserProvider {

/**
 * The Parse user model.
 *
 * @var string
 */
protected $model;

/**
 * Create a new Parse user provider.
 *
 * @param  string  $model
 * @return void
 */
public function __construct($model)
{
    User::registerSubclass();
    $this->model = $model;
}

/**
 * Retrieve a user by their unique identifier.
 *
 * @param  mixed  $identifier
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 */
public function retrieveById($identifier)
{
    $query = User::query();
    $query->equalTo("id", $identifier);

    return $query->find();
}

/**
 * Retrieve a user by their unique identifier and "remember me" token.
 *
 * @param  mixed  $identifier
 * @param  string  $token
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 */
public function retrieveByToken($identifier, $token)
{
    $query = User::query();
    $query->equalTo("id", $identifier);
    $query->equalTo("remember_token", $token);

    return $query->find();
}

/**
 * Update the "remember me" token for the given user in storage.
 *
 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
 * @param  string  $token
 * @return void
 */
public function updateRememberToken(UserContract $user, $token)
{
    $user->set("remember_token",$token);

    $user->save();
}

/**
 * Retrieve a user by the given credentials.
 *
 * @param  array  $credentials
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 */
public function retrieveByCredentials(array $credentials)
{
    // First we will add each credential element to the query as a where clause.
    // Then we can execute the query and, if we found a user, return it in a
    // Eloquent User "model" that will be utilized by the Guard instances.

    $query = User::query();
    $query->equalTo("username", $credentials['email']);
    $user = $query->first();

    if (!empty($user)){
        return $user;
    }

    return  null;
}

/**
 * Validate a user against the given credentials.
 *
 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
 * @param  array  $credentials
 * @return bool
 */
public function validateCredentials(UserContract $user, array $credentials)
{
    try{
        var_dump($user);
        $user->logIn($credentials['email'],$credentials['password']);
        return true;

    } catch (ParseException $ex){
        return false;
    }


}

/**
 * Create a new instance of the model.
 *
 * @return \Illuminate\Database\Eloquent\Model
 */
public function createModel()
{
    $class = '\\'.ltrim($this->model, '\\');

    return new $class;
}

}

class User extends ParseUser implements AuthenticatableContract, CanResetPasswordContract {

use Authenticatable, CanResetPassword;


/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getObjectId();
}

/**
 * Get the password for the user. 
 * Not available in the Parse World
 *
 * @return string
 */
public function getAuthPassword()
{
    return null;
}

/**
 * Get the token value for the "remember me" session.
 *
 * @return string
 */
public function getRememberToken()
{
    return $this->get("remember_token");
}

/**
 * Set the token value for the "remember me" session.
 *
 * @param  string  $value
 * @return void
 */
public function setRememberToken($value)
{
    $this->set("remember_token", $value);
}


}
1

1 Answers

0
votes

https://github.com/HipsterJazzbo/LaraParse

This package pretty much does exactly what I was aiming to do for anyone who comes across this issue