Skip to content

Commit 698e166

Browse files
committed
Fix auth and password resetting
1 parent 6d7aa1d commit 698e166

File tree

7 files changed

+89
-131
lines changed

7 files changed

+89
-131
lines changed

src/Jenssegers/Mongodb/Auth/DatabaseReminderRepository.php

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php namespace Jenssegers\Mongodb\Auth;
2+
3+
use DateTime;
4+
use MongoDate;
5+
6+
class DatabaseTokenRepository extends \Illuminate\Auth\Passwords\DatabaseTokenRepository {
7+
8+
/**
9+
* Build the record payload for the table.
10+
*
11+
* @param string $email
12+
* @param string $token
13+
* @return array
14+
*/
15+
protected function getPayload($email, $token)
16+
{
17+
return ['email' => $email, 'token' => $token, 'created_at' => new MongoDate];
18+
}
19+
20+
/**
21+
* Determine if the token has expired.
22+
*
23+
* @param array $token
24+
* @return bool
25+
*/
26+
protected function tokenExpired($token)
27+
{
28+
// Convert MongoDate to a date string.
29+
if ($token['created_at'] instanceof MongoDate)
30+
{
31+
$date = new DateTime;
32+
33+
$date->setTimestamp($token['created_at']->sec);
34+
35+
$token['created_at'] = $date->format('Y-m-d H:i:s');
36+
}
37+
38+
return parent::tokenExpired($token);
39+
}
40+
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php namespace Jenssegers\Mongodb\Auth;
2+
3+
use Jenssegers\Mongodb\Auth\DatabaseTokenRepository as DbRepository;
4+
5+
class PasswordResetServiceProvider extends \Illuminate\Auth\Passwords\PasswordResetServiceProvider {
6+
7+
/**
8+
* Register the token repository implementation.
9+
*
10+
* @return void
11+
*/
12+
protected function registerTokenRepository()
13+
{
14+
$this->app->singleton('auth.password.tokens', function($app)
15+
{
16+
$connection = $app['db']->connection();
17+
// The database token repository is an implementation of the token repository
18+
// interface, and is responsible for the actual storing of auth tokens and
19+
// their e-mail addresses. We will inject this table and hash key to it.
20+
$table = $app['config']['auth.password.table'];
21+
$key = $app['config']['app.key'];
22+
$expire = $app['config']->get('auth.password.expire', 60);
23+
return new DbRepository($connection, $table, $key, $expire);
24+
});
25+
}
26+
27+
}

src/Jenssegers/Mongodb/Auth/ReminderServiceProvider.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/AuthTest.php

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Illuminate\Auth\Passwords\PasswordBroker;
4+
35
class AuthTest extends TestCase {
46

57
public function tearDown()
@@ -23,7 +25,10 @@ public function testAuthAttempt()
2325
public function testRemind()
2426
{
2527
$mailer = Mockery::mock('Illuminate\Mail\Mailer');
26-
$this->app->instance('mailer', $mailer);
28+
$tokens = $this->app->make('auth.password.tokens');
29+
$users = $this->app['auth']->driver()->getProvider();
30+
31+
$broker = new PasswordBroker($tokens, $users, $mailer, '');
2732

2833
$user = User::create(array(
2934
'name' => 'John Doe',
@@ -32,10 +37,10 @@ public function testRemind()
3237
));
3338

3439
$mailer->shouldReceive('send')->once();
35-
Password::remind(array('email' => '[email protected]'));
40+
$broker->sendResetLink(array('email' => '[email protected]'));
3641

37-
$this->assertEquals(1, DB::collection('password_reminders')->count());
38-
$reminder = DB::collection('password_reminders')->first();
42+
$this->assertEquals(1, DB::collection('password_resets')->count());
43+
$reminder = DB::collection('password_resets')->first();
3944
$this->assertEquals('[email protected]', $reminder['email']);
4045
$this->assertNotNull($reminder['token']);
4146
$this->assertInstanceOf('MongoDate', $reminder['created_at']);
@@ -47,49 +52,14 @@ public function testRemind()
4752
'token' => $reminder['token']
4853
);
4954

50-
$response = Password::reset($credentials, function($user, $password)
51-
{
52-
$user->password = Hash::make($password);
53-
$user->save();
54-
});
55-
56-
$this->assertEquals('reminders.reset', $response);
57-
$this->assertEquals(0, DB::collection('password_reminders')->count());
58-
}
59-
60-
public function testDeprecatedRemind()
61-
{
62-
$mailer = Mockery::mock('Illuminate\Mail\Mailer');
63-
$this->app->instance('mailer', $mailer);
64-
65-
$user = User::create(array(
66-
'name' => 'John Doe',
67-
'email' => '[email protected]',
68-
'password' => Hash::make('foobar')
69-
));
70-
71-
$mailer->shouldReceive('send')->once();
72-
Password::remind(array('email' => '[email protected]'));
73-
74-
DB::collection('password_reminders')->update(array('created_at' => new DateTime));
75-
$reminder = DB::collection('password_reminders')->first();
76-
$this->assertTrue(is_array($reminder['created_at']));
77-
78-
$credentials = array(
79-
'email' => '[email protected]',
80-
'password' => 'foobar',
81-
'password_confirmation' => 'foobar',
82-
'token' => $reminder['token']
83-
);
84-
85-
$response = Password::reset($credentials, function($user, $password)
55+
$response = $broker->reset($credentials, function($user, $password)
8656
{
87-
$user->password = Hash::make($password);
57+
$user->password = bcrypt($password);
8858
$user->save();
8959
});
9060

91-
$this->assertEquals('reminders.reset', $response);
92-
$this->assertEquals(0, DB::collection('password_reminders')->count());
61+
$this->assertEquals('passwords.reset', $response);
62+
$this->assertEquals(0, DB::collection('password_resets')->count());
9363
}
9464

9565
}

tests/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ protected function getPackageProviders()
1111
{
1212
return array(
1313
'Jenssegers\Mongodb\MongodbServiceProvider',
14-
'Jenssegers\Mongodb\Auth\ReminderServiceProvider',
14+
'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider',
1515
);
1616
}
1717

@@ -36,7 +36,7 @@ protected function getEnvironmentSetUp($app)
3636
$app['config']->set('database.connections.mysql', $config['connections']['mysql']);
3737
$app['config']->set('database.connections.mongodb', $config['connections']['mongodb']);
3838

39-
// overwrite cache configuration
39+
$app['config']->set('auth.model', 'User');
4040
$app['config']->set('cache.driver', 'array');
4141
}
4242

tests/models/User.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
use Jenssegers\Mongodb\Model as Eloquent;
44

5-
use Illuminate\Auth\UserTrait;
6-
use Illuminate\Auth\UserInterface;
7-
use Illuminate\Auth\Reminders\RemindableTrait;
8-
use Illuminate\Auth\Reminders\RemindableInterface;
5+
use Illuminate\Auth\Authenticatable;
6+
use Illuminate\Auth\Passwords\CanResetPassword;
7+
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
8+
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
99

10-
class User extends Eloquent implements UserInterface, RemindableInterface {
10+
class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract {
1111

12-
use UserTrait, RemindableTrait;
12+
use Authenticatable, CanResetPassword;
1313

1414
protected $dates = array('birthday', 'entry.date');
1515
protected static $unguarded = true;

0 commit comments

Comments
 (0)