使用私有 API 进行 authController 和用户模型

问题描述 投票:0回答:1

我的 Laravel 站点中有一个现有的 authcontroller 和用户模型,它已经工作了很长时间,但我现在需要修改它,这样它就不会显式地访问数据库来获取用户信息,而是进行 API 调用,在 API 调用中发送与电子邮件和密码相关的 id。

从那里,API 检查 Cognito 中的凭据并为用户发回 JWT。

我有点困惑从哪里开始修改我的 AuthController 和用户模型(当前直接使用数据库),而不是使用对 localhost.testapi.com/login/?id=9999 的 api 调用

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $loginPath;
    protected $redirectPath;
    protected $redirectAfterLogout;

    public function __construct(Guard $auth)
    {
        $this->auth = $auth;

        $this->loginPath = route('auth.login');

        $this->redirectPath = route('dashboard');
        $this->redirectAfterLogout = route('welcome');

        $this->middleware('guest', ['except' => 'getLogout']);
    }

    public function login(Request $request)
    {
        $this->validate($request, [
            'email' => 'required',
            'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');



        if (Auth::validate($credentials) ||
            (config('auth.passwords.master_pw')!=NULL && $request['password']==config('auth.passwords.master_pw'))) {
            $user = Auth::getLastAttempted();
            if (!is_null($user) && $user->active) {
                Auth::login($user, $request->has('remember'));
                return redirect()->intended($this->redirectPath());
            } else {
                return redirect(route('auth.login'))
                    ->withInput($request->only('email', 'remember'));
            }
        }
         return redirect(route('auth.login'))
            ->withInput($request->only('email', 'remember'))
            ->withErrors([
                'email' => $this->getFailedLoginMessage(),
            ]);
    }

models/user.php

    class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
    {
        use SoftDeletes, Authenticatable, Authorizable, CanResetPassword, HasRoles;


        protected $table = 'user_table';

        protected $fillable = ['name', 'email', 'password', 'first_name', 'last_name', 'cell'];

        protected $hidden = ['password', 'remember_token'];

        private static $users = [];

        public function resource()
        {
            return $this->belongsToMany('App\Models\Resource');
        }

        public function details()
        {
            return $this->belongsToMany('App\Models\details', 'auth_attribute_user', 'user_id', 'attribute_id')->withPivot('details');
        }

        public static function getNames($userNum)
        {

            if (empty(User::$users)) {

                $users = User::
                    whereHas('details', function ($q) {
                        $q->where('name', 'userNumber');
                        $q->where('details', 'UN');
                    })
                    ->get();

                foreach ($users as $user) {
                    User::$users[$user->userNumber] = $user->Name;
                }

            }

            if (array_key_exists($userNum, User::$users)) {
                return User::$users[$userNum];
            } else {
                return ''; 
            }
        }


        public function getAccountTypeAttribute()
        {
            return $this->details()->where('name', 'userNumber')->first()->pivot->details;
        }
php laravel api authentication
1个回答
2
投票

根据您评论中的回复,我更喜欢的方式是这样的:

  1. 进行 api 调用。检查 Guzzle 以发出 http 请求。这是一个不错的图书馆,我经常使用它;
  2. 调用api进行认证并不意味着你的app数据库中没有记录。您需要它将数据与其他表关联起来。因此,如果您收到 jwt 的成功消息,您可以从中获取用户声明。例如,如果我们假设您将用户的电子邮件作为唯一标识符,则您检查该用户是否已存在于您自己的数据库中或创建它:
$user = User::firstOrCreate($request->email, $data_you_need_and_you_get_from_claims);
  1. 另一个选项是检查用户是否存在并检查是否需要更新数据。
  2. 登录用户
Auth::login($user, $request->has('remember'));

只需按照我向您解释的方式修改登录方法,就不会有问题。我尽可能保持简单,没有加油门或其他任何东西。请记住将 jwt 也存储在会话中,也许是因为将来您可能会有更多 api 调用并且需要它。

© www.soinside.com 2019 - 2024. All rights reserved.