laravel Lumen 中的用户身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43426340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
User Authentication in Lumen
提问by hylian
I'm trying to enable basic user authentication username
, and password
into my Lumen application.
我正在尝试启用基本用户身份验证username
,并password
进入我的 Lumen 应用程序。
In app.php
file, the following has been uncommented as explained in https://lumen.laravel.com/docs/5.4/authentication
在app.php
文件中,以下内容已取消注释,如https://lumen.laravel.com/docs/5.4/authentication 中所述
$app->withFacades();
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
$app->register(App\Providers\AuthServiceProvider::class);
My Route looks like this:
我的路线是这样的:
$app->post('auth/register', ['uses' => 'Auth\AuthController@postRegister']);
My Controller looks like this:
我的控制器看起来像这样:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Auth;
use App\User;
class AuthController extends Controller {
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
}
public function postRegister(Request $request, UserRepository $userRepository)
{
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
$user = $userRepository->store($request);
Auth::login($user);
return ['result' => 'success'];
}
}
I have been getting a combination of weird and wonderful errors, currently I'm getting:
我收到了一系列奇怪和奇妙的错误,目前我得到:
ReflectionException in BoundMethod.php line 155:
Class App\Repositories\UserRepository does not exist
I've done some extensive google searching, but there doesn't seem to be many documented uses of user auth in Lumen so looking for a pointer as to what I've missed here.
我已经进行了一些广泛的谷歌搜索,但在 Lumen 中似乎没有很多用户身份验证的记录使用,所以寻找一个关于我在这里遗漏的内容的指针。
回答by hylian
My initial error: I was looking for a method of logging in a user, what I should have been looking for was authentication. Thinking about what I actually needed to achieve I came up with the below functions:
我最初的错误:我正在寻找一种登录用户的方法,我应该寻找的是authentication。考虑到我实际需要实现的功能,我想出了以下功能:
- Create user
- Delete user
- Verify user
- 创建用户
- 删除用户
- 验证用户
With that in mind I ended up with something like the below:
考虑到这一点,我最终得到了如下内容:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
//Required to hash the password
use Illuminate\Support\Facades\Hash;
class AuthController extends Controller {
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
}
public function validateRequest(Request $request) {
$rules = [
'email' => 'required|email|unique:users',
'password' => 'required|min:6'
];
$this->validate($request, $rules);
}
//Get the input and create a user
public function store(Request $request) {
$this->validateRequest($request);
$user = User::create([
'email' => $request->get('email'),
'password'=> Hash::make($request->get('password'))
]);
return response()->json(['status' => "success", "user_id" => $user->id], 201);
}
//delete the user
public function destroy($id) {
$user = User::find($id);
if(!$user){
return response()->json(['message' => "The user with {$id} doesn't exist"], 404);
}
$user->delete();
return response()->json(['data' => "The user with with id {$id} has been deleted"], 200);
}
//Authenticate the user
public function verify(Request $request) {
$email = $request->get('email');
$password = $request->get('password');
$user = User::where('email', $email)->first();
if($user && Hash::check($password, $user->password)) {
return response()->json($user, 200);
}
return response()->json(['message' => "User details incorrect"], 404);
}
//Return the user
public function show($id) {
$user = User::find($id);
if(!$user) {
return response()->json(['status' => "invalid", "message" => "The userid {$id} does not exist"], 404);
}
return response()->json(['status' => "success", 'data' => $user], 200);
}
//Update the password
public function update(Request $request, $id) {
$user = User::find($id);
if(!$user){
return response()->json(['message' => "The user with {$id} doesn't exist"], 404);
}
$this->validateRequest($request);
$user->email = $request->get('email');
$user->password = Hash::make($request->get('password'));
$user->save();
return response()->json(['data' => "The user with with id {$user->id} has been updated"], 200);
}
}
回答by Hedam
I'm not really sure what you want to achieve with UserRepository and Auth.
Lumen is a stateless framework, meaning that Auth::login()
never will have any effect. Also, as far as I'm concerned, UserRepository
is a Laravel thing. Not a Lumen thing.
我不太确定你想用 UserRepository 和 Auth 实现什么。Lumen 是一个无状态框架,这意味着它Auth::login()
永远不会产生任何影响。另外,就我而言,UserRepository
是 Laravel 的事情。不是流明的东西。
Create the user with App\User::create($request->all())
and access it through the Eloquent model. You can enable Eloquent in bootstrap/app.php
创建用户App\User::create($request->all())
并通过 Eloquent 模型访问它。您可以在bootstrap/app.php