說明:自定義用戶身份驗證基礎
我一直在使用Laravel了一堆項目的現在。 在Laravel中實現用戶身份驗證很簡單。現在,我正在處理的結構有點不同 - 我本地沒有database
或users
表。我必須進行API調用以查詢我需要的內容。
我已經試過
public function postSignIn(){
$username = strtolower(Input::get('username'));
$password_api = VSE::user('password',$username); // abc <-----
$password = Input::get('password'); // abc <-----
if ($password == $password_api) {
//Log user in
$auth = Auth::attempt(); // Stuck here <----
}
if ($auth) {
return Redirect::to('/dashboard')->with('success', 'Hi '. $username .' ! You have been successfully logged in.');
}
else {
return Redirect::to('/')->with('error', 'Username/Password Wrong')->withInput(Request::except('password'))->with('username', $username);
}
}
更新
我在VSE
類使用一個簡單的命令shell_exec
連接到API
public static function user($attr, $username) {
$data = shell_exec('curl '.env('API_HOST').'vse/accounts');
$raw = json_decode($data,true);
$array = $raw['data'];
return $array[$attr];
}
我希望我可以在這裏向你展示,但它在我的本地機器上的虛擬機上,所以請留在我身邊。基本上,它
執行
curl http://172.16.67.137:1234/vse/accounts
< ---更新
響應
Object
data:Array[2]
0:Object
DBA:""
account_id:111
account_type:"admin"
address1:"111 Park Ave"
address2:"Floor 4"
address3:"Suite 4011"
city:"New York"
customer_type:2
display_name:"BobJ"
email_address:"[email protected]"
first_name:"Bob"
last_name:"Jones"
last_updated_utc_in_secs:200200300
middle_names:"X."
name_prefix:"Mr"
name_suffix:"Jr."
nation_code:"USA"
non_person_name:false
password:"abc"
phone1:"212-555-1212"
phone2:""
phone3:""
postal_code:"10022"
state:"NY"
time_zone_offset_from_utc:-5
1:Object
DBA:""
account_id:112
account_type:"mbn"
address1:"112 Park Ave"
address2:"Floor 3"
address3:"Suite 3011"
city:"New York"
customer_type:2
display_name:"TomS"
email_address:"[email protected]"
first_name:"Tom"
last_name:"Smith"
last_updated_utc_in_secs:200200300
middle_names:"Z."
name_prefix:"Mr"
name_suffix:"Sr."
nation_code:"USA"
non_person_name:false
password:"abd"
phone1:"212-555-2323"
phone2:""
phone3:""
postal_code:"10022"
state:"NY"
time_zone_offset_from_utc:-5
message:"Success"
status:200
正如你可以看到鮑勃密碼abc
和湯姆是abd
通過創建自己的用戶提供程序來處理登錄驗證和用戶詳細信息,您可以[擴展Laravel身份驗證系統](http://laravel.com/docs/5.1/authentication#adding-custom-authentication-drivers)並將其設置爲auth驅動程序。 – Bogdan
如果您發現文檔不夠詳細,那麼您可以查看['Illuminate \ Auth \ DatabaseUserProvider'](https://github.com/laravel/framework/blob/5.1/src/Illuminate/ Auth/DatabaseUserProvider.php)源代碼來查看它如何處理數據庫存儲的用戶並將邏輯應用於遠程API。 – Bogdan
如果你想提供一些代碼來展示你如何連接到API,以及你正在做什麼請求來驗證和獲取用戶信息,那麼我可能會提供一個關於如何將API調用集成到Laravel Auth驅動程序。 – Bogdan