Skip to content

Reset Password not working Laravel 5.4. #1124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Sb8691 opened this issue Feb 20, 2017 · 13 comments
Closed

Reset Password not working Laravel 5.4. #1124

Sb8691 opened this issue Feb 20, 2017 · 13 comments

Comments

@Sb8691
Copy link

Sb8691 commented Feb 20, 2017

Hi Guys,

I've tried to implement password reset, also with 'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider::class' , but there are certain mistakes

First: Jenssegers\Mongodb\Auth\PasswordBrokerManager::class is big mistake with missing parameter in function, should look like this :

protected function createTokenRepository(array $config)
{
return new DatabaseTokenRepository(
$this->app['db']->connection(),
$this->app['hash'], // this is missing
$config['table'],
$this->app['config']['app.key'],
$config['expire']
);
}

And after that is problem with: Cannot use object of type MongoDB\BSON\UTCDateTime as array

It would be helpful to fix it,

Thanks

@dasjet
Copy link

dasjet commented Mar 14, 2017

Same problem here.. Any solution?

@misterion
Copy link

Same problem here

@natanshalva
Copy link

Any solution?

@Jchang0210
Copy link

Same problem here

@kcleveland
Copy link

kcleveland commented Jul 12, 2017

Same issue here as well. Any fix planned?

---Updated with Solution---

I was able to make this work by making some changes to the Moloquent PasswordBrokerManager.php and DatabaseTokenRepository.php files:

PasswordBrokerManager.php:

namespace Moloquent\Auth;

use Illuminate\Auth\Passwords\PasswordBrokerManager as BasePasswordBrokerManager;

class PasswordBrokerManager extends BasePasswordBrokerManager
{

    protected function createTokenRepository(array $config)
    {

        $key = $this->app['config']['app.key'];

        if (\Illuminate\Support\Str::startsWith($key, 'base64:')) {
            $key = base64_decode(substr($key, 7));
        }

        $connection = isset($config['connection']) ? $config['connection'] : null;

        return new DatabaseTokenRepository(
            $this->app['db']->connection(),
            $this->app['hash'],
            $config['table'],
            $this->app['config']['app.key'],
            $config['expire']
        );
    }

}

DatabaseTokenRepository.php:

namespace Moloquent\Auth;

use DateTime;
use DateTimeZone;
use Illuminate\Auth\Passwords\DatabaseTokenRepository as BaseDatabaseTokenRepository;
use MongoDB\BSON\UTCDateTime;

class DatabaseTokenRepository extends BaseDatabaseTokenRepository
{
    /**
     * Build the record payload for the table.
     *
     * @param string $email
     * @param string $token
     *
     * @return array
     */
    protected function getPayload($email, $token)
    {
        return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new UTCDateTime(time() * 1000)];
    }

    /**
     * Determine if the token has expired.
     *
     * @param array $token
     *
     * @return bool
     */
    protected function tokenExpired($createdAt)
    {
        // Convert UTCDateTime to a date string.
        if ($createdAt instanceof UTCDateTime) {
            $date = $createdAt->toDateTime();
            $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
            $createdAt = $date->format('Y-m-d H:i:s');
         } elseif (is_array($createdAt) and isset($createdAt['date'])) {
            $date = new DateTime($createdAt['date'], new DateTimeZone(isset($createdAt['timezone']) ? $createdAt['timezone'] : 'UTC'));
            $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
            $createdAt = $date->format('Y-m-d H:i:s');
        }

        return parent::tokenExpired($createdAt);
    }
}

See previous discussion on this issue here.

@Brutalbeard
Copy link

Test the above and working for me.

@Gil9091e10
Copy link

Try this solution but I still get the same error: Can not use object of type MongoDB \ BSON \ UTCDateTime as array

Someone else found some other solution for this error.

@mikekamornikov
Copy link

My dirty hack for reset issues:

Add the following to Auth/ForgotPasswordController and Auth/ResetPasswordController to make it work (requires php 7):

/**
 * Get the broker to be used during password reset.
 *
 * TODO: remove this method once Jenssegers\Mongodb reset password issues are fixed
 *
 * @return \Illuminate\Contracts\Auth\PasswordBroker
 */
public function broker()
{
    return new class(app()) extends \Jenssegers\Mongodb\Auth\PasswordBrokerManager {

        protected function createTokenRepository(array $config)
        {
            $c = $this->app['db']->connection();
            $h = $this->app['hash'];
            $t = $config['table'];
            $k = $this->app['config']['app.key'];
            $e = $config['expire'];

            return new class($c, $h, $t, $k, $e) extends \Illuminate\Auth\Passwords\DatabaseTokenRepository {

                protected function getPayload($email, $token)
                {
                    return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new UTCDateTime(time() * 1000)];
                }

                protected function tokenExpired($token)
                {
                    // Convert UTCDateTime to a date string.
                    if ($token instanceof UTCDateTime) {
                        $date = $token->toDateTime();
                        $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
                        $token = $date->format('Y-m-d H:i:s');
                    } elseif (is_array($token) and isset($token['date'])) {
                        $date = new DateTime($token['date'], new DateTimeZone(isset($token['timezone']) ? $token['timezone'] : 'UTC'));
                        $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
                        $token = $date->format('Y-m-d H:i:s');
                    }

                    return parent::tokenExpired($token);
                }
            };
        }
    };
}

As you see there are 2 issues:

  • token should be stored as a hash ($this->hasher->make($token))
  • tokenExpired gets $token which doesn't require $token['created_at'] anymore

@bsormagec
Copy link

bsormagec commented Mar 4, 2018

any update ? @jenssegers

@alsemany
Copy link

alsemany commented Aug 20, 2018

Thank you so much @mikekamornikov this solution working very well

it just need to add

use MongoDB\BSON\UTCDateTime;

also
new \DateTimeZone

if someone tried this solution and not worked , you just need to make a new reset request
example.com/password/reset
then
try the new token

@joerecra
Copy link

Hey @mikekamornikov, I made all you wrote, but doesn't work.

I'm getting this error: DateTime::__construct(): Failed to parse time string (1539211570000) at position 11 (0): Unexpected character

Help me please!

@mikekamornikov
Copy link

mikekamornikov commented Oct 11, 2018

@joerecra I need more context. Where does this error come from (trace)? The code above works for me for quiet some time (LV 5.6, php 7.1)

@mcandylab
Copy link

In Config/App.php replace Illuminate\Auth\Passwords\PasswordResetServiceProvider on Jenssegers\Mongodb\Auth\PasswordResetServiceProvider
In App/Http/Controllers/Auth/ResetPasswordController.php add use MongoDB\BSON\UTCDateTime;

Timezone: Europe/Moscow
Laravel: 5.7.*
Jenssegers/mongodb: "^3.4"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests