@atlasapollo You’d be better for just importing the data into your new database, instead of doing it in an ad hoc basis each time a user logs in.
FortifyServiceProvider modification for Fortify::authenticateUsing acting strange
Hi,
Hopefully someone might have insight into some strange behavior I'm not able to understand yet.
I'm using Fortify and am trying to use Fortify::authenticateUsing for custom login processing. There is an older production system that I am in the process of replacing with the new code base and would like for the users to be able to sign into the new system with their old-system credentials and sync some content between the old and new systems.
If they sign into the new system with their old-system credentials and don't have an account with the new system, on login failure in the new system, Fortify will attempt to sign into the old system with the credentials. On successful login, the old system will return some data for the new system to sync. If the login with the old system is successful, we create a new account with the old credentials on the new system. Then we sync the data with the new system.
All of this is working fine, but what is strange is that, when the process is complete, the UI remains on the login screen and the login button becomes available for pressing again. If I login again, the login is successful because the account now exists on the new system. When login is successful the UI is advanced to the dashboard screen as expected for any successful login.
I'm not quite sure what I'm doing wrong. I try to return a user object, as instructed in the Fortify docs, or return null if appropriate. Can you please share advice if you have any ideas? It would be great if the user were just advanced to the dashboard after account creation.
The new system is Jetstream, Inertia and Vue frontend.
Please see my code for FortifyServiceProvider:
<?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 Laravel\Fortify\Features;
use Laravel\Fortify\Fortify;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\RedirectResponse;
use App\Models\User;
use App\Models\Story;
use App\Models\StorySection;
use App\Models\SyncWithOldIlysProd;
use Auth;
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(5)->by($email.$request->ip());
});
RateLimiter::for('two-factor', function (Request $request) {
return Limit::perMinute(5)->by($request->session()->get('login.id'));
});
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('email', $request->email)->first();
if ($user &&
Hash::check($request->password, $user->password)) {
return $user;
}
else {
$user = null;
if (env('SYNC_WITH_OLD_ILYS_PROD', false)) {
$email = $request->email;
$password = $request->password;
$response = Http::post('https://nnnnn/loginAndSync', [
'e' => $email,
'p' => $password
]);
$stories = $response->json();
if (!empty($stories)) {
$newUser = new CreateNewUser();
$user = $newUser->create([
"name" => $email,
"email" => $email,
"password" => $password,
"password_confirmation" => $password,
"terms" => true
]);
$user->email_verified_at = now();
$user->save();
$userId = $user->id;
// Delete old content
Story::where('user_id', $userId)->delete();
$sync = new SyncWithOldIlysProd();
$sync->user_id = $user->id;
$sync->email = $user->email;
$sync->stories_json = json_encode($stories);
$sync->save();
foreach ($stories as $originalStory) {
$story = Story::firstOrCreate([
'user_id' => $userId,
'uuid' => Str::uuid(),
'title' => $originalStory["name"]
]);
$totalWordCount = 0;
foreach ($originalStory["story_contents"] as $section) {
$sectionWordCount = Str::wordCount($section["content"]);
$section = StorySection::firstOrCreate([
'story_id' => $story["id"],
'story_uuid' => $story["uuid"],
'uuid' => Str::uuid(),
'user_id' => $userId,
'title' => $section["name"],
'content' => $section["content"],
'sort_position' => $section["sort_id"],
'word_count' => $sectionWordCount,
'last_text_position' => null
]);
$totalWordCount += $sectionWordCount;
}
$story["word_count"] = $totalWordCount;
$story->save();
}
}
}
}
return $user;
});
}
}
Thank you in advance!
Please or to participate in this conversation.