doure5236 2017-04-15 12:57 采纳率: 0%
浏览 57
已采纳

流明中的用户认证

I'm trying to enable basic user authentication username, and password into my Lumen application.

In app.php file, the following has been uncommented as explained in 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.

  • 写回答

2条回答 默认 最新

  • dtid30526 2017-08-14 23:31
    关注

    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:

    1. Create user
    2. Delete user
    3. 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);
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境