在三方登录后,我们fecshop在/customer/google/loginv 这种接口时,同时进行了注册和登录的操作。
现在有一个问题,看源码没有看懂。
以 wechat登录为例:
就是vue端拿到了微信返回的code 去请求/customer/wechat/loginv 接口时,仅仅是获得了登录成功的返回。
返回头如下
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 25 Oct 2018 03:22:33 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
Fecshop-Uuid: 365d74d0-d805-11e8-bebd-0800274fb806
Access-Control-Expose-Headers: fecshop-uuid, fecshop-lang, fecshop-currency, access-token
Set-Cookie: _csrf=5f045908fbd02d9d2c1a832d7c6884105e26a6ae06ac7a749b61b8c48d0850efa%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22cWju8BMmksGcujFRac3800OEiSDHYfWc%22%3B%7D; path=/; HttpOnly
{"code":200,"message":"process success","data":[]}
在这个返回中不应该像邮箱登录一样 有Access-Token吗?目前的代码中,无论是Yii::$app->user->login()方法 还是Yii::$service->customer->login()方法都没有返回Access-Token 而再次请求 没有 access-token 则身份认证就为空了。请求指点迷津。
//2. 创建第三方用户的账户,密码自动生成
/**
* @property $user | Array ,example:
* ['first_name' => $first_name,'last_name' => $last_name,'email' => $email,]
* @property $type | String 代表第三方登录的名称,譬如google,facebook
* @return bool
* 如果用户emai存在,则直接登录,成功后返回true
* 如果用户不存在,则注册用户,然后直接登录,成功后返回true
*/
protected function actionRegisterThirdPartyAccountAndLogin($user, $type)
{
// 查看邮箱是否存在
$email = $user['email'];
$customer_one = Yii::$service->customer->getUserIdentityByEmail($email);
if ($customer_one) {
$loginStatus = \Yii::$app->user->login($customer_one);
if ($loginStatus) {
return true;
}
// 不存在,注册。
} else {
if (!(isset($user['password']) && $user['password'])) {
$user['password'] = $this->getRandomPassword();
}
$registerData = [
'email' => $email,
'firstname' => $user['first_name'],
'lastname' => $user['last_name'],
'password' => $user['password'],
'type' => $type,
];
$registerStatus = Yii::$service->customer->register($registerData);
if ($registerStatus) {
$loginStatus = Yii::$service->customer->login($registerData);
if ($loginStatus) {
return true;
}
}
}
return false;
}