Home > OS >  Jetstream unable to delete account after modifying the authentication logic. (Laravel 9,Jetstream, I
Jetstream unable to delete account after modifying the authentication logic. (Laravel 9,Jetstream, I

Time:03-24

Recently I have decided to add another field when in log in page( Username ), it is unique.

When log in you can use either username or email to do so.

After the modification, "Login", "Registeration", "Changing Password", "Password Reset", "Email Verification" worked fine.

"Two Factor Authentication", "Browser Sessions", "Delete Account" does not working just wondering what has gone wrong

When i try to delete account or log out from all browser session, this pops out

for more detailed error enter image description here enter image description here enter image description here

config/fortify

'username' => 'auth',

'email' => 'email',

Models/User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use JoelButcher\Socialstream\HasConnectedAccounts;
use JoelButcher\Socialstream\SetsProfilePhotoFromUrl;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Jetstream\HasTeams;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Post;


class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto {
        getProfilePhotoUrlAttribute as getPhotoUrl;
    }
    use HasTeams;
    use HasConnectedAccounts;
    use Notifiable;
    use SetsProfilePhotoFromUrl;
    use TwoFactorAuthenticatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'username'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];

    /**
     * Get the URL to the user's profile photo.
     *
     * @return string
     */
    public function getProfilePhotoUrlAttribute()
    {
        if (filter_var($this->profile_photo_path, FILTER_VALIDATE_URL)) {
            return $this->profile_photo_path;
        }

}

Users database

    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->string('email')->unique();
                $table->string('username')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password')->nullable();
                $table->rememberToken();
                $table->foreignId('current_team_id')->nullable();
                $table->foreignId('current_connected_account_id')->nullable();
                $table->foreignId('current_connected_post_id')->nullable();
                $table->text('profile_photo_path')->nullable();
                $table->timestamps();
            });
        }

  }
}

action/fortify/UpdatesUserProfileInformation.php

Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'username' => ['required', 'string', 'max:255', 'alpha_dash:users', Rule::unique('users')->ignore($user->id)],
            'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
                'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:10024'],
            ])->validateWithBag('updateProfileInformation');

action/fortify/CreatesNewUsers.php

Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'username' => ['required', 'string', 'max:255', 'unique:users','alpha_dash:users'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => $this->passwordRules(),
            'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
           
  ])->validate();

return DB::transaction(function () use ($input) {
            return tap(User::create([
                'name' => $input['name'],
                'email' => $input['email'],
                'username' => $input['username'],
                'password' => Hash::make($input['password']),
            ]), function (User $user) {
                $this->createTeam($user);
            });
        });

provides/fortifyserviceprovider.php

Fortify::authenticateUsing(function (LoginRequest $request) {
            $user = User::where('email', $request->auth)
                ->orWhere('username', $request->auth)->first();

            if (
                $user &&
                Hash::check($request->password, $user->password)
            ) {
                return $user;
            }
        });

provides/jetstreamserviceprovider.php

Fortify::authenticateUsing(function (LoginRequest $request) {
                $user = User::where('email', $request->auth)
                    ->orWhere('username', $request->auth)->first();
    
                if (
                    $user &&
                    Hash::check($request->password, $user->password)
                ) {
                    return $user;
                }
            });

CodePudding user response:

I have found the solution for all this,

Inside vendor/laravel/fortify/src/Actions/ConfirmPassword.php, there is a function __invoke which Confirm that the given password is valid for the given user.

In default, $username = config('fortify.username'); but my config('fortify.username') is set to auth that why it is not inside the database and return column not found

Is there a way to change this? cuz modifying the vendor is not a good solution.

CodePudding user response:

@Chai Fuu Wong, it could be the auth problem

  • Related