dongqianzhan8325 2015-04-16 19:47
浏览 58
已采纳

使用Parse.com PHP SDK进行Laravel身份验证

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条回答 默认 最新

  • douou6807 2015-04-17 11:52
    关注

    https://github.com/HipsterJazzbo/LaraParse

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值