2017-10-04 36 views
0

我有兩個環境:如何將身份驗證委託給外部API服務器?

  • 一個:基於Laravel應用API服務器
    (我對這個完全許可)
  • :爲一個
    (我不外部API服務器沒有他/她的認證沒有權限訪問用戶)

users表只有兩列,idexternal_user_idid創建者Aexternal_user_id創建者B

如果我實現一個自定義的供應商,我需要遵循兩個合同:

<?php 

interface UserProvider 
{ 
    public function retrieveById($identifier); 
    public function retrieveByToken($identifier, $token); 
    public function updateRememberToken(Authenticatable $user, $token); 
    public function retrieveByCredentials(array $credentials); 
    public function validateCredentials(Authenticatable $user, array $credentials); 
} 

interface Authenticatable 
{ 
    public function getAuthIdentifierName(); 
    public function getAuthIdentifier(); 
    public function getAuthPassword(); 
    public function getRememberToken(); 
    public function setRememberToken($value); 
    public function getRememberTokenName(); 
} 

然而,有一個非常棘手的問題:我如何驗證用戶之前調用retrieveByCredentials()?在每次方法

回答

0
  • BadMethodCallException
  • 切勿使用Auth::attempt()
  • 僅使用Auth::login()

 

<?php 

class MyUserProvider extends Provider implements UserProvider 
{ 
    public function retrieveById($identifier) 
    { 
     throw new \BadMethodCallException(); 
    } 
    public function retrieveByToken($identifier, $token) 
    { 
     throw new \BadMethodCallException(); 
    } 
    public function updateRememberToken(Authenticatable $user, $token) 
    { 
     throw new \BadMethodCallException(); 
    } 
    public function retrieveByCredentials(array $credentials) 
    { 
     throw new \BadMethodCallException(); 
    } 
    public function validateCredentials(Authenticatable $user, array $credentials) 
    { 
     throw new \BadMethodCallException(); 
    } 
} 

class User extends Model implements Authenticatable 
{ 
    public function getAuthIdentifierName() 
    { 
     throw new \BadMethodCallException(); 
    } 
    public function getAuthIdentifier() 
    { 
     throw new \BadMethodCallException(); 
    } 
    public function getAuthPassword() 
    { 
     throw new \BadMethodCallException(); 
    } 
    public function getRememberToken() 
    { 
     throw new \BadMethodCallException(); 
    } 
    public function setRememberToken($value) 
    { 
     throw new \BadMethodCallException(); 
    } 
    public function getRememberTokenName() 
    { 
     throw new \BadMethodCallException(); 
    } 
}