Использование частного API для authController и модели пользователяPhp

Кемеровские программисты php общаются здесь
Anonymous
Использование частного API для authController и модели пользователя

Сообщение Anonymous »

У меня есть существующий контроллер авторизации и модель пользователя на моем сайте Laravel, который работает уже давно, но теперь мне нужно изменить его так, чтобы вместо явного обращения к базе данных для получения информации о пользователе он вместо этого создавал Вызов API, отправляющий идентификатор в вызове API, который относится к электронной почте и паролю.

Оттуда API проверяет учетные данные в Cognito и отправляет обратно JWT для пользователя. .

Я немного не понимаю, с чего начать изменение моего AuthController и модели пользователя, которые в настоящее время напрямую используют базу данных, чтобы вместо этого использовать вызов API для localhost.testapi.com/login/?id=9999

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


Подробнее здесь: https://stackoverflow.com/questions/538 ... user-model

Вернуться в «Php»