PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 4, 2022

[FIXED] Fortify login response not working, keeps returning {two-factor: false}

 February 04, 2022     laravel, laravel-fortify     No comments   

Issue

Here is my code:

My LoginResponse

<?php

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

class LoginResponse implements LoginResponseContract
{
    /**
     * @param  $request
     * @return mixed
     */
    public function toResponse($request)
    {
        return response()->json([
            'resp' => "success"
        ],200);
    }
}

FortifyServiceProviders

<?php

namespace App\Providers;

use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Laravel\Fortify\Fortify;
use App\Http\Responses\LoginResponse;


class FortifyServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {

    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Fortify::createUsersUsing(CreateNewUser::class);
        Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
        Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
        Fortify::resetUserPasswordsUsing(ResetUserPassword::class);

        RateLimiter::for('login', function (Request $request) {
            $email = (string) $request->email;

            return Limit::perMinute(15)->by($email.$request->ip());
        });

        $this->app->singleton(LoginResponseContract::class, LoginResponse::class);
    }

This keeps returning {two-factor:false}

I have also tried by updating fortify login response on FortifyServiceProvider's register method as: (this one was totally different and before LoginResponse class was created)

public function register()
{
    $this->app->instance(LoginResponse::class, new class implements LoginResponse {
        public function toResponse($request)
        {
            return response()->json([
                'resp' => 'success'
            ],200);
        }
    });
}

But, still on login, I am receiving this response, {two-factor: false}, what am I missing and where should I update so that I could get the correct login response on fortify. I am using fortify and sanctum for the laravel spa.


Solution

Not sure what was wrong, but you could follow this procedure to check if this actually works.

Basically, what you have done is correct this is how you could find the bug.

  1. Instead of writing that line on FortifyServiceProvider, write it on AppServiceProvider's boot method.

  2. If it works,(and should be), you should confirm if you set up the Fortify correctly, you should go through each of those lines on docs, to check if you miss something. https://laravel.com/docs/8.x/fortify

  3. In case, if you want to narrow this down, I am not sure if you have included App\Providers\FortifyServiceProvider::class, this on your config/app. So, it is never firing FortifyServiceProvider, to confirm, you could write dd('hit'), on FortifyServiceProvider's boot method.

Please, check and let me know your findings.



Answered By - Saroj Shrestha
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing