0

In laravel 9 Inertiajs/vuejs when unlogged user select admin area with typing url in browser, like
site/admin/currencies

Login page is opened and on submit I redirect to default user depending on access of user in JS code:

function loginSubmit() {

    formLogin.value.post(route('login'), {
        preserveScroll: true,
        onSuccess: (data) => {

            if(usePage().props.value.auth.is_logged_user_admin) {
                Toast.fire({
                    icon: 'success',
                    title: 'You are logged into the app as admin'
                })
                window.location.href= route('admin.dashboard.index')
                return
            } // if(usePage().props.value.auth.is_logged_user_admin) {


            if(usePage().props.value.auth.is_logged_user_content_editor) {
                Toast.fire({
                    icon: 'success',
                    title: 'You are logged into the app as content editor'
                })
                window.location.href= route('admin.cms_items.index')
                return
            } // if(usePage().props.value.auth.is_logged_user_content_editor) {
            // ...

But actually I see that at first site/admin/currencies page is opened and only after that site/admin/dashboard is opened if logged user is admin.

How can I cancel site/admin/currencies page opening?

UPDATED BLOCK: As I use Fortify in file app/Providers/FortifyServiceProvider.php I have :

<?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 App\Models\User;
use Carbon\Carbon;
// app/Providers/Responses/LoginResponse.php
//use App\Http\Responses\LoginResponse;
//use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Fortify;

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);

        Fortify::authenticateUsing(function (Request $request) {
            $user        = User::where('email', $request->email)->first();
            $request     = request();
            if ($user && Hash::check($request->password, $user->password)) {
                if ($user->status === 'A') {
                    return $user;
                } else {
                    throw ValidationException::withMessages([
                        Fortify::username() => "Account is not active",
                    ]);
                }

            }
        });

        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'));
        });
    }
}

I suppose default Fortify login functionality is called with this default action. I do not have any other files edited. How can it be overwritten/cancelled ?

mstdmstd
  • 2,195
  • 17
  • 63
  • 140
  • 1
    Route login probably goes to a controller with the login functionality, can you show us that aswell? – geertjanknapen May 09 '22 at 07:37
  • Please, read UPDATED BLOCK – mstdmstd May 09 '22 at 08:00
  • 1
    I think `LoginResponse` class may be the culprit. But I can't seem to find anything regarding where the redirect happens. There is a redirect in `LoginResponse` class, however I'm not sure if that's what you need. Unfortunatly I'm at work and don't have time to investigate further atm. – geertjanknapen May 09 '22 at 08:31
  • I do not have LoginResponse installed – mstdmstd May 09 '22 at 09:49
  • 1
    Does this answer your question? [laravel redirect to url after login](https://stackoverflow.com/questions/36240906/laravel-redirect-to-url-after-login) – Linus Juhlin May 09 '22 at 22:08

0 Answers0