2017-06-01 86 views
0

我已經使用Laravel編寫了一個用於檢查身份驗證的API調用。我需要將該控制器移動到Lumen以用作微服務。流明的基本身份驗證

這是我在Laravel的控制器。

public function byCredantial(Request $request) 
{ 
    $user = [ 
     'email' => $request->input('email'), 
     'password' => $request->input('password') 
    ]; 

    if (Auth::attempt($user)) { 
     $response = $this->getSuccess(Auth::user()->id); 

     return response()->json($response, 200); 
    } else { 
     $response = $this->getError($user); 

     return response()->json($response, 401); 
    } 
} 

Lumen doc未提供如何進行此類驗證。他們沒有檢查機關的功能是否正確。我怎麼能在流明中做到這一點。這可能嗎?

回答

0

你可以在流明中做到這一點。外觀在默認情況下是禁用的(如果您想啓用它,您可以看到documentation中的說明),但我不建議啓用外觀作爲應用程序的額外開銷。相反,我會修改你的函數來調用app('auth')。這將返回類外殼代理Auth而不加載所有其他外牆。

public function byCredantial(Request $request) 
{ 
    $user = [ 
     'email' => $request->input('email'), 
     'password' => $request->input('password') 
    ]; 

    $auth = app('auth'); 

    if ($auth->attempt($user)) { 
     $response = $this->getSuccess($auth->user()->id); 

     return response()->json($response, 200); 
    } else { 
     $response = $this->getError($user); 

     return response()->json($response, 401); 
    } 
} 

此外,我建議你閱讀認證documentation和放置散裝這段代碼在AuthServiceProvider