Hi all
I'm having some problem with using a Custom user provider only when accessing the password reset link. I can't recall which example I've copied online but when searching for Custom authentication you will get many different implementations.
The current copy of the application I have so far works nicely beside the resetting password link throwing an error so I don't want to start from scratch.
BindingResolutionException in Container.php line 788:
Target [Illuminate\Contracts\Auth\UserProvider] is not instantiable.
in Container.php line 788
at Container->build('Illuminate\Contracts\Auth\UserProvider', array()) in Container.php line 659
at Container->make('Illuminate\Contracts\Auth\UserProvider', array()) in Application.php line 644
at Application->make('Illuminate\Contracts\Auth\UserProvider') in Container.php line 890
at Container->resolveClass(object(ReflectionParameter)) in Container.php line 851
at Container->getDependencies(array(object(ReflectionParameter), object(ReflectionParameter), object(ReflectionParameter), object(ReflectionParameter)), array()) in Container.php line 816
at Container->build('App\Http\Middleware\CustomPasswordBroker', array()) in Container.php line 659
at Container->make('App\Http\Middleware\CustomPasswordBroker', array()) in Application.php line 644
at Application->make('App\Http\Middleware\CustomPasswordBroker') in Container.php line 890
at Container->resolveClass(object(ReflectionParameter)) in Container.php line 851
at Container->getDependencies(array(object(ReflectionParameter), object(ReflectionParameter)), array()) in Container.php line 816
at Container->build('App\Http\Controllers\Auth\PasswordController', array()) in Container.php line 659
at Container->make('App\Http\Controllers\Auth\PasswordController', array()) in Application.php line 644
at Application->make('App\Http\Controllers\Auth\PasswordController') in ControllerDispatcher.php line 83
at ControllerDispatcher->makeController('App\Http\Controllers\Auth\PasswordController') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\Auth\PasswordController', 'getEmail') in Route.php line 204
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 701
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 703
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 670
at Router->dispatchToRoute(object(Request)) in Router.php line 628
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 56
Please ask which files I should put up if you need to see more.
Userprovider is interface, you must use the class that implement the interface, i thing that is eloquente Userprovider
tiagocaetano said:
Userprovider is interface, you must use the class that implement the interface, i thing that is eloquente Userprovider
Thank you for pointing that out.
in my app/Http/Middleware/CustomPasswordBroker.php
Changed:
use Illuminate\Contracts\Auth\UserProvider;
To:
use App\Http\Controllers\Auth\CustomUserProvider as UserProvider;
Seems to fix the previous issue but now I get
Unresolvable dependency resolving [Parameter #3 [ <required> $emailView ]] in class App\Http\Middleware\CustomPasswordBroker
<?php namespace App\Http\Middleware;
use Closure;
use UnexpectedValueException;
//use Illuminate\Contracts\Auth\UserProvider; // Original
//use Illuminate\Contracts\Auth\CustomUserProvider;
// pp\Http\Controllers\Auth;
use App\Http\Controllers\Auth\CustomUserProvider as UserProvider;
use Illuminate\Contracts\Mail\Mailer as MailerContract;
use Illuminate\Contracts\Auth\PasswordBroker as PasswordBrokerContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
//use TokenRepositoryInterface;
//vendor/laravel/framework/src/Illuminate/Auth/Passwords/TokenRepositoryInterface.php
//use Illuminate\Auth\Passwords\TokenRepositoryInterface;
use Illuminate\Auth\Passwords\TokenRepositoryInterface;
class CustomPasswordBroker implements PasswordBrokerContract {
/**
* The password token repository.
*
* @var \Illuminate\Auth\Passwords\TokenRepositoryInterface
*/
protected $tokens;
/**
* The user provider implementation.
*
* @var \Illuminate\Contracts\Auth\UserProvider
*/
protected $users;
/**
* The mailer instance.
*
* @var \Illuminate\Contracts\Mail\Mailer
*/
protected $mailer;
/**
* The view of the password reset link e-mail.
*
* @var string
*/
protected $emailView;
/**
* The custom password validator callback.
*
* @var \Closure
*/
protected $passwordValidator;
/**
* Create a new password broker instance.
*
* @param \Illuminate\Auth\Passwords\TokenRepositoryInterface $tokens
* @param \Illuminate\Contracts\Auth\UserProvider $users
* @param \Illuminate\Contracts\Mail\Mailer $mailer
* @param string $emailView
* @return void
*/
public function __construct(TokenRepositoryInterface $tokens,
UserProvider $users,
MailerContract $mailer,
$emailView)
{
$this->users = $users;
$this->mailer = $mailer;
$this->tokens = $tokens;
$this->emailView = $emailView;
}
/**
* Send a password reset link to a user.
*
* @param array $credentials
* @param \Closure|null $callback
* @return string
*/
public function sendResetLink(array $credentials, Closure $callback = null)
{
// First we will check to see if we found a user at the given credentials and
// if we did not we will redirect back to this current URI with a piece of
// "flash" data in the session to indicate to the developers the errors.
$user = $this->getUser($credentials);
if (is_null($user))
{
return PasswordBrokerContract::INVALID_USER;
}
// Once we have the reset token, we are ready to send the message out to this
// user with a link to reset their password. We will then redirect back to
// the current URI having nothing set in the session to indicate errors.
$token = $this->tokens->create($user);
$this->emailResetLink($user, $token, $callback);
return PasswordBrokerContract::RESET_LINK_SENT;
}
/**
* Send the password reset link via e-mail.
*
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
* @param string $token
* @param \Closure|null $callback
* @return int
*/
public function emailResetLink(CanResetPasswordContract $user, $token, Closure $callback = null)
{
// We will use the reminder view that was given to the broker to display the
// password reminder e-mail. We'll pass a "token" variable into the views
// so that it may be displayed for an user to click for password reset.
$view = $this->emailView;
return $this->mailer->send($view, compact('token', 'user'), function($m) use ($user, $token, $callback)
{
$m->to($user->getEmailForPasswordReset());
if ( ! is_null($callback))
{
call_user_func($callback, $m, $user, $token);
}
});
}
/**
* Reset the password for the given token.
*
* @param array $credentials
* @param \Closure $callback
* @return mixed
*/
public function reset(array $credentials, Closure $callback)
{
// If the responses from the validate method is not a user instance, we will
// assume that it is a redirect and simply return it from this method and
// the user is properly redirected having an error message on the post.
$user = $this->validateReset($credentials);
if ( ! $user instanceof CanResetPasswordContract)
{
return $user;
}
$pass = $credentials['password'];
// Once we have called this callback, we will remove this token row from the
// table and return the response from this callback so the user gets sent
// to the destination given by the developers from the callback return.
call_user_func($callback, $user, $pass);
$this->tokens->delete($credentials['token']);
return PasswordBrokerContract::PASSWORD_RESET;
}
/**
* Validate a password reset for the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\CanResetPassword
*/
protected function validateReset(array $credentials)
{
if (is_null($user = $this->getUser($credentials)))
{
return PasswordBrokerContract::INVALID_USER;
}
if ( ! $this->validateNewPassword($credentials))
{
return PasswordBrokerContract::INVALID_PASSWORD;
}
if ( ! $this->tokens->exists($user, $credentials['token']))
{
return PasswordBrokerContract::INVALID_TOKEN;
}
return $user;
}
/**
* Set a custom password validator.
*
* @param \Closure $callback
* @return void
*/
public function validator(Closure $callback)
{
$this->passwordValidator = $callback;
}
/**
* Determine if the passwords match for the request.
*
* @param array $credentials
* @return bool
*/
public function validateNewPassword(array $credentials)
{
list($password, $confirm) = [
$credentials['password'],
$credentials['password_confirmation'],
];
if (isset($this->passwordValidator))
{
return call_user_func(
$this->passwordValidator, $credentials) && $password === $confirm;
}
return $this->validatePasswordWithDefaults($credentials);
}
/**
* Determine if the passwords are valid for the request.
*
* @param array $credentials
* @return bool
*/
protected function validatePasswordWithDefaults(array $credentials)
{
list($password, $confirm) = [
$credentials['password'],
$credentials['password_confirmation'],
];
return $password === $confirm && mb_strlen($password) >= 6;
}
/**
* Get the user for the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\CanResetPassword
*
* @throws \UnexpectedValueException
*/
public function getUser(array $credentials)
{
$credentials = array_except($credentials, ['token']);
// $credentials = array_except($credentials, ['token', 'mysql_database']); // IB4T Core Change :
$user = $this->users->retrieveByCredentials($credentials);
if ($user && ! $user instanceof CanResetPasswordContract)
{
throw new UnexpectedValueException("User must implement CanResetPassword interface.");
}
return $user;
}
/**
* Get the password reset token repository implementation.
*
* @return \Illuminate\Auth\Passwords\TokenRepositoryInterface
*/
protected function getRepository()
{
return $this->tokens;
}
}
You have interface( ), and this classe is implementacion, you have $emailview that is a content of view? Is that?
tiagocaetano said:
You have interface( ), and this classe is implementacion, you have $emailview that is a content of view? Is that?
Sorry I'm not sure what's going on here. This was copied from an example online but it only seems to throw an error when I try and use the password reset link.
Yes I am too. I'm sorry I haven't had much time to work through this. Will get back to you definitely if I figure out why this is happening.
Thank you for your help so far though.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community