Skip to content

Commit 95ab8b3

Browse files
authored
feat: username in jetstream (#34)
* feat: username in jetstream * feat: mary ui template * fix: psalm * fix: tests * fix: ignore test * chore: run pint
1 parent 078b7df commit 95ab8b3

22 files changed

+405
-84
lines changed

.env.testing

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
APP_NAME=Laravel
2+
APP_ENV=local
3+
APP_KEY=base64:bihmw9M/GyC73y3gQjA5hmQQg+Rq3AoHgqojUZeSUoA=
4+
APP_DEBUG=true
5+
APP_TIMEZONE=UTC
6+
APP_URL=
7+
8+
APP_LOCALE=en
9+
APP_FALLBACK_LOCALE=en
10+
APP_FAKER_LOCALE=en_US
11+
12+
APP_MAINTENANCE_DRIVER=file
13+
# APP_MAINTENANCE_STORE=database
14+
15+
BCRYPT_ROUNDS=12
16+
17+
LOG_CHANNEL=stack
18+
LOG_STACK=single
19+
LOG_DEPRECATIONS_CHANNEL=null
20+
LOG_LEVEL=debug
21+
22+
DB_CONNECTION=sqlite
23+
# DB_HOST=127.0.0.1
24+
# DB_PORT=3306
25+
DB_DATABASE=:memory:
26+
# DB_USERNAME=root
27+
# DB_PASSWORD=
28+
29+
SESSION_DRIVER=database
30+
SESSION_LIFETIME=120
31+
SESSION_ENCRYPT=false
32+
SESSION_PATH=/
33+
SESSION_DOMAIN=null
34+
35+
BROADCAST_CONNECTION=log
36+
FILESYSTEM_DISK=local
37+
QUEUE_CONNECTION=database
38+
39+
CACHE_STORE=database
40+
CACHE_PREFIX=
41+
42+
MEMCACHED_HOST=127.0.0.1
43+
44+
REDIS_CLIENT=phpredis
45+
REDIS_HOST=127.0.0.1
46+
REDIS_PASSWORD=null
47+
REDIS_PORT=6379
48+
49+
MAIL_MAILER=log
50+
MAIL_HOST=127.0.0.1
51+
MAIL_PORT=2525
52+
MAIL_USERNAME=null
53+
MAIL_PASSWORD=null
54+
MAIL_ENCRYPTION=null
55+
MAIL_FROM_ADDRESS="[email protected]"
56+
MAIL_FROM_NAME="${APP_NAME}"
57+
58+
AWS_ACCESS_KEY_ID=
59+
AWS_SECRET_ACCESS_KEY=
60+
AWS_DEFAULT_REGION=us-east-1
61+
AWS_BUCKET=
62+
AWS_USE_PATH_STYLE_ENDPOINT=false
63+
64+
VITE_APP_NAME="${APP_NAME}"

.github/workflows/psalm.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ on:
99
jobs:
1010
psalm:
1111
runs-on: ${{ matrix.operating-system }}
12+
permissions:
13+
contents: write
14+
packages: write
15+
actions: write
1216
strategy:
1317
fail-fast: false
1418
matrix:
@@ -20,8 +24,10 @@ jobs:
2024
- uses: shivammathur/setup-php@c541c155eee45413f5b09a52248675b1a2575231 # v2
2125
with:
2226
php-version: ${{ matrix.php-versions }}
23-
extensions: mbstring, intl
27+
extensions: mbstring, intl, , :php-psr
2428
ini-values: post_max_size=256M, max_execution_time=180
2529
coverage: xdebug
26-
- uses: php-actions/composer@8a65f0d3c6a1d17ca4800491a40b5756a4c164f3 # v6 # or alternative dependency management
27-
- run: ./vendor/bin/psalm
30+
31+
- name: Composer install
32+
run: composer install --optimize-autoloader --prefer-dist
33+
- run: php ./vendor/bin/psalm --threads=2 --output-format=github --report=results.sarif

app/Actions/Fortify/CreateNewUser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\DB;
88
use Illuminate\Support\Facades\Hash;
99
use Illuminate\Support\Facades\Validator;
10+
use Illuminate\Validation\ValidationException;
1011
use Laravel\Fortify\Contracts\CreatesNewUsers;
1112
use Laravel\Jetstream\Jetstream;
1213

@@ -18,12 +19,15 @@ class CreateNewUser implements CreatesNewUsers
1819
* Create a newly registered user.
1920
*
2021
* @param array<string, string> $input
22+
*
23+
* @throws ValidationException
2124
*/
2225
public function create(array $input): User
2326
{
2427
Validator::make($input, [
2528
'name' => ['required', 'string', 'max:255'],
2629
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
30+
'username' => ['required', 'string', 'max:255', 'unique:users'],
2731
'password' => $this->passwordRules(),
2832
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
2933
])->validate();
@@ -32,6 +36,7 @@ public function create(array $input): User
3236
return tap(User::create([
3337
'name' => $input['name'],
3438
'email' => $input['email'],
39+
'username' => $input['username'],
3540
'password' => Hash::make($input['password']),
3641
]), function (User $user) {
3742
$this->createTeam($user);

app/Actions/Fortify/UpdateUserPassword.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\User;
66
use Illuminate\Support\Facades\Hash;
77
use Illuminate\Support\Facades\Validator;
8+
use Illuminate\Validation\ValidationException;
89
use Laravel\Fortify\Contracts\UpdatesUserPasswords;
910

1011
class UpdateUserPassword implements UpdatesUserPasswords
@@ -15,6 +16,8 @@ class UpdateUserPassword implements UpdatesUserPasswords
1516
* Validate and update the user's password.
1617
*
1718
* @param array<string, string> $input
19+
*
20+
* @throws ValidationException
1821
*/
1922
public function update(User $user, array $input): void
2023
{

app/Actions/Fortify/UpdateUserProfileInformation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class UpdateUserProfileInformation implements UpdatesUserProfileInformation
1414
* Validate and update the given user's profile information.
1515
*
1616
* @param array<string, mixed> $input
17+
*
1718
* @SuppressWarnings(PHPMD.ElseExpression)
1819
*/
1920
public function update(User $user, array $input): void

app/Models/Team.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/**
1313
* @psalm-suppress all
14+
*
15+
* @method static forceCreate(array $array)
1416
*/
1517
class Team extends JetstreamTeam
1618
{

app/Models/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414
/**
1515
* @property mixed $id
16+
* @property mixed $name
17+
*
1618
* @psalm-suppress all
19+
*
20+
* @method static where(string $string, mixed $identity)
21+
* @method static create(array $array)
1722
*/
1823
class User extends Authenticatable
1924
{
@@ -33,6 +38,7 @@ class User extends Authenticatable
3338
'name',
3439
'email',
3540
'password',
41+
'username',
3642
];
3743

3844
/**

app/Policies/TeamPolicy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\Team;
66
use App\Models\User;
77
use Illuminate\Auth\Access\HandlesAuthorization;
8+
89
/**
910
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
1011
*/

app/Providers/FortifyServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use App\Actions\Fortify\ResetUserPassword;
77
use App\Actions\Fortify\UpdateUserPassword;
88
use App\Actions\Fortify\UpdateUserProfileInformation;
9+
use App\Models\User;
910
use Illuminate\Cache\RateLimiting\Limit;
1011
use Illuminate\Http\Request;
12+
use Illuminate\Support\Facades\Hash;
1113
use Illuminate\Support\Facades\RateLimiter;
1214
use Illuminate\Support\ServiceProvider;
1315
use Illuminate\Support\Str;
@@ -45,5 +47,14 @@ public function boot(): void
4547
RateLimiter::for('two-factor', function (Request $request) {
4648
return Limit::perMinute(5)->by($request->session()->get('login.id'));
4749
});
50+
51+
Fortify::authenticateUsing(function (Request $request) {
52+
$user = User::where('email', $request->identity)
53+
->orWhere('username', $request->identity)->first();
54+
if ($user && Hash::check($request->password, $user->password)
55+
) {
56+
return $user;
57+
}
58+
});
4859
}
4960
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@
5757

5858

5959
"scripts": {
60+
"psalm": "vendor/bin/psalm --config=psalm.xml",
6061
"larastan": "vendor/bin/phpstan analyse --memory-limit=2G app tests",
6162
"pint": "vendor/bin/pint",
62-
"test:pest": "vendor/bin/pest --parallel",
63+
"test:pest": "vendor/bin/pest --parallel --compact",
6364
"test:phpstan": "vendor/bin/phpstan analyse",
6465
"test": [
6566
"@test:pest",

0 commit comments

Comments
 (0)