Skip to content

Commit c15241e

Browse files
authored
Merge pull request #142 from kenjis/fix-auth_logins
fix: rename `auth_logins.email`
2 parents 06d83e2 + 4d6f4dc commit c15241e

File tree

7 files changed

+30
-30
lines changed

7 files changed

+30
-30
lines changed

src/Database/Migrations/2020-12-28-223112_create_auth_tables.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function up(): void
5454
'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
5555
'ip_address' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
5656
'user_agent' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
57-
'email' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
57+
'identifier' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
5858
'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true], // Only for successful logins
5959
'date' => ['type' => 'datetime'],
6060
'success' => ['type' => 'tinyint', 'constraint' => 1],

src/Entities/User.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function lastLogin(bool $allowFailed = false): ?Login
194194
}
195195

196196
return $logins
197-
->where('email', $this->getAuthEmail())
197+
->where('identifier', $this->getAuthEmail())
198198
->orderBy('date', 'desc')
199199
->first();
200200
}

src/Models/LoginModel.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ class LoginModel extends Model
1818
protected $allowedFields = [
1919
'ip_address',
2020
'user_agent',
21-
'email',
21+
'identifier',
2222
'user_id',
2323
'date',
2424
'success',
2525
];
2626
protected $useTimestamps = false;
2727
protected $validationRules = [
2828
'ip_address' => 'required',
29-
'email' => 'required',
29+
'identifier' => 'required',
3030
'user_agent' => 'permit_empty|string',
3131
'user_id' => 'permit_empty|integer',
3232
'date' => 'required|valid_date',
@@ -39,12 +39,12 @@ class LoginModel extends Model
3939
*
4040
* @return BaseResult|false|int|object|string
4141
*/
42-
public function recordLoginAttempt(string $email, bool $success, ?string $ipAddress = null, ?string $userAgent = null, $userId = null)
42+
public function recordLoginAttempt(string $identifier, bool $success, ?string $ipAddress = null, ?string $userAgent = null, $userId = null)
4343
{
4444
return $this->insert([
4545
'ip_address' => $ipAddress,
4646
'user_agent' => $userAgent,
47-
'email' => $email,
47+
'identifier' => $identifier,
4848
'user_id' => $userId,
4949
'date' => date('Y-m-d H:i:s'),
5050
'success' => (int) $success,
@@ -60,7 +60,7 @@ public function fake(Generator &$faker): Login
6060
{
6161
return new Login([
6262
'ip_address' => $faker->ipv4,
63-
'email' => $faker->email,
63+
'identifier' => $faker->email,
6464
'user_id' => null,
6565
'date' => Time::parse('-1 day')->toDateTimeString(),
6666
'success' => true,

tests/Authentication/AccessTokenAuthenticatorTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ public function testAttemptCannotFindUser()
168168

169169
// A login attempt should have always been recorded
170170
$this->seeInDatabase('auth_logins', [
171-
'email' => 'token: abc123',
172-
'success' => 0,
171+
'identifier' => 'token: abc123',
172+
'success' => 0,
173173
]);
174174
}
175175

@@ -195,8 +195,8 @@ public function testAttemptSuccess()
195195

196196
// A login attempt should have been recorded
197197
$this->seeInDatabase('auth_logins', [
198-
'email' => 'token: ' . $token->raw_token,
199-
'success' => 1,
198+
'identifier' => 'token: ' . $token->raw_token,
199+
'success' => 1,
200200
]);
201201
}
202202

tests/Authentication/SessionAuthenticatorTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ public function testAttemptCannotFindUser()
254254

255255
// A login attempt should have always been recorded
256256
$this->seeInDatabase('auth_logins', [
257-
'email' => '[email protected]',
258-
'success' => 0,
257+
'identifier' => '[email protected]',
258+
'success' => 0,
259259
]);
260260
}
261261

@@ -284,8 +284,8 @@ public function testAttemptSuccess()
284284

285285
// A login attempt should have been recorded
286286
$this->seeInDatabase('auth_logins', [
287-
'email' => $this->user->email,
288-
'success' => 1,
287+
'identifier' => $this->user->email,
288+
'success' => 1,
289289
]);
290290
}
291291

@@ -314,8 +314,8 @@ public function testAttemptCaseInsensitive()
314314

315315
// A login attempt should have been recorded
316316
$this->seeInDatabase('auth_logins', [
317-
'email' => '[email protected]',
318-
'success' => 1,
317+
'identifier' => '[email protected]',
318+
'success' => 1,
319319
]);
320320
}
321321

@@ -346,8 +346,8 @@ public function testAttemptUsernameOnly()
346346

347347
// A login attempt should have been recorded
348348
$this->seeInDatabase('auth_logins', [
349-
'email' => 'fooROG',
350-
'success' => 1,
349+
'identifier' => 'fooROG',
350+
'success' => 1,
351351
]);
352352
}
353353
}

tests/Controllers/LoginTest.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public function testLoginBadEmail()
5252

5353
// Login should have been recorded successfully
5454
$this->seeInDatabase('auth_logins', [
55-
'email' => '[email protected]',
56-
'user_id' => null,
57-
'success' => 0,
55+
'identifier' => '[email protected]',
56+
'user_id' => null,
57+
'success' => 0,
5858
]);
5959

6060
$this->assertNotEmpty(session('error'));
@@ -79,9 +79,9 @@ public function testLoginActionEmailSuccess()
7979

8080
// Login should have been recorded successfully
8181
$this->seeInDatabase('auth_logins', [
82-
'email' => '[email protected]',
83-
'user_id' => $this->user->id,
84-
'success' => true,
82+
'identifier' => '[email protected]',
83+
'user_id' => $this->user->id,
84+
'success' => true,
8585
]);
8686
// Last Used date should have been set
8787
$identity = $this->user->getEmailIdentity();
@@ -109,9 +109,9 @@ public function testLoginActionUsernameSuccess()
109109

110110
// Login should have been recorded successfully
111111
$this->seeInDatabase('auth_logins', [
112-
'email' => $this->user->username,
113-
'user_id' => $this->user->id,
114-
'success' => true,
112+
'identifier' => $this->user->username,
113+
'user_id' => $this->user->id,
114+
'success' => true,
115115
]);
116116
// Last Used date should have been set
117117
$identity = $this->user->getEmailIdentity();

tests/Unit/UserTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public function testLastLogin()
7878

7979
$login = fake(
8080
LoginModel::class,
81-
['email' => $this->user->email, 'user_id' => $this->user->id]
81+
['identifier' => $this->user->email, 'user_id' => $this->user->id]
8282
);
8383
fake(
8484
LoginModel::class,
85-
['email' => $this->user->email, 'user_id' => $this->user->id, 'success' => false]
85+
['identifier' => $this->user->email, 'user_id' => $this->user->id, 'success' => false]
8686
);
8787

8888
$last = $this->user->lastLogin();

0 commit comments

Comments
 (0)