Skip to content

Commit 747df5a

Browse files
authored
feat: add WebauthnAuthenticatable trait (#393)
1 parent e9b950e commit 747df5a

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

src/WebauthnAuthenticatable.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace LaravelWebauthn;
4+
5+
use Illuminate\Database\Eloquent\Relations\HasMany;
6+
use LaravelWebauthn\Models\WebauthnKey;
7+
8+
trait WebauthnAuthenticatable
9+
{
10+
/**
11+
* Get the webauthn keys associated to this user.
12+
*
13+
* @return HasMany
14+
*/
15+
public function webauthnKeys()
16+
{
17+
return $this->hasMany(WebauthnKey::class);
18+
}
19+
}

tests/FeatureTestCase.php

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

33
namespace LaravelWebauthn\Tests;
44

5-
use Illuminate\Foundation\Auth\User as Authenticatable;
65
use Orchestra\Testbench\TestCase;
76

87
class FeatureTestCase extends TestCase
@@ -104,19 +103,3 @@ public function user()
104103
return factory(User::class)->create();
105104
}
106105
}
107-
108-
class User extends Authenticatable
109-
{
110-
/**
111-
* The attributes that are mass assignable.
112-
*
113-
* @var string[]
114-
*/
115-
protected $fillable = [
116-
'name',
117-
'email',
118-
'password',
119-
'email_verified_at',
120-
'remember_token',
121-
];
122-
}

tests/Unit/Models/UserTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace LaravelWebauthn\Tests\Unit\Models;
4+
5+
use LaravelWebauthn\Models\WebauthnKey;
6+
use LaravelWebauthn\Tests\FeatureTestCase;
7+
8+
class UserTest extends FeatureTestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
public function it_get_keys()
14+
{
15+
$user = $this->user();
16+
17+
$webauthnKey = factory(WebauthnKey::class)->create([
18+
'user_id' => $user->getAuthIdentifier(),
19+
'credentialId' => '1',
20+
]);
21+
22+
$keys = $user->webauthnKeys()->get();
23+
24+
$this->assertCount(1, $keys);
25+
$this->assertEquals($webauthnKey->user_id, $keys->first()->user_id);
26+
}
27+
}

tests/User.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace LaravelWebauthn\Tests;
44

55
use Illuminate\Foundation\Auth\User as Authenticatable;
6+
use LaravelWebauthn\WebauthnAuthenticatable;
67

78
class User extends Authenticatable
89
{
10+
use WebauthnAuthenticatable;
11+
912
/**
1013
* The attributes that are mass assignable.
1114
*

0 commit comments

Comments
 (0)