gpt4 book ai didi

php - Lumen 中的用户身份验证

转载 作者:行者123 更新时间:2023-12-04 14:48:48 26 4
gpt4 key购买 nike

我正在尝试在我的 Lumen 应用程序中启用基本用户身份验证用户名密码

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);

我的路线如下所示:

 $app->post('auth/register', ['uses' => 'Auth\AuthController@postRegister']);

我的 Controller 如下所示:

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'];
}
}

我遇到了一些奇怪而奇妙的错误,目前我得到:

ReflectionException in BoundMethod.php line 155:
Class App\Repositories\UserRepository does not exist

我已经进行了一些广泛的谷歌搜索,但在 Lumen 中似乎没有很多关于用户身份验证的记录使用,因此寻找一个关于我在这里错过的内容的指针。

最佳答案

我最初的错误:我正在寻找一种登录用户的方法,我应该寻找的是身份验证。考虑到我实际需要实现的目标,我想出了以下功能:

  1. 创建用户
  2. 删除用户
  3. 验证用户

考虑到这一点,我最终得到了如下所示的结果:

<?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);
}

}

关于php - Lumen 中的用户身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43426340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com