302 ошибка laravel

I have started a new Laravel 5.2 project, using laravel new MyApp, and added authentication via php artisan make:auth. This is intended to be a members only website, where the first user is seeded, and creates the rest (no manual user creation/password reset/etc).

These are the routes I have currently defined:

 Route::group(['middleware' => 'web'], function () {
  // Authentication Routes...
  Route::get( 'user/login',  ['as' => 'user.login',     'uses' => 'AuthAuthController@showLoginForm']);
  Route::post('user/login',  ['as' => 'user.doLogin',   'uses' => 'AuthAuthController@login'        ]);

  Route::group(['middleware' => 'auth'], function() {
    // Authenticated user routes
    Route::get( '/', ['as'=>'home', 'uses'=> 'HomeController@index']);
    Route::get( 'user/{uid?}', ['as' => 'user.profile',   'uses' => 'AuthAuthController@profile' ]);
    Route::get( 'user/logout', ['as' => 'user.logout',    'uses' => 'AuthAuthController@logout'  ]);
    Route::get( '/user/add',   ['as' => 'user.add',       'uses' => 'AuthAuthController@showAddUser']);

    [...]
  });
});

I can login just fine, however I’m experiencing some very «funky» behavior — when I try to logout ( via the built-in logout method that was created via artisan ), the page does a 302 redirect to home, and I am still logged in.

What’s more, while almost all pages (not listed here) work as expected, user.add also produces a 302 to the home page.

Do note the homepage is declared to the AuthController as $redirectTo, if that makes any difference

I found out about the redirects via the debugbar. Any idea on what to look for ?

That was a frustrating few hours. I added a route into my api.php route and every time I visited it it triggered a redirect. I saw nothing in the browser and it was like my controller function wasn’t even being called.

I put the usual dd() and dump() into my function and it wasn’t being called. I even changed the function name so it didn’t exist and thought I’d get an error message, but nothing – still a redirect.

What is going on? I expanded out the call from “NamespaceClass@function” to put in a function () {} and that was being called and ran ok.

Clearly there was something wrong with my class somewhere. The other functions within it worked fine, just this new one didn’t even seem to be there.

Sure enough a bit of Duck-Jitsu and I found this: https://stackoverflow.com/questions/35020477/laravel-unexpected-redirects-302 – answer 3 is where I got my clue.

I looked at my class constructor and I was using the auth middleware to ensure the functions were not used unless authenticated.

public function __construct() {
  $this->middleware(['auth:api']);
}

For my function I wasn’t using any authentication, so of course I was getting a redirect. But because it’s an api call and the expected return in JSON under an XHttpResponse I wasn’t able to properly debug it. I was getting back a HTML page instead.

As a work around all I needed to do was add in an exception for my function:

public function __construct() {
  $this->middleware(['auth:api'], ['except' => [ 'myFunction' ]]);
}

Now all I have to do is go back and sort out the authentication for the function so I don’t need to bypass it.

I am trying to get info from action, but when click, just page refresh and in console I get code 302 and stay on current page.

I read a lot of similar topics here but found nothing.

I am trying to execute http://laravel2.lo/getUserChannels?user_id=2

Laravel 5.7.16

route:

Auth::routes();

Route::group(['middleware' => ['auth']], function () {
    Route::view('createUser', 'createuser');
    Route::view('createChannel', 'createchannel');
    Route::view('joinChannel', 'joinchannel');


    Route::get('profile', 'UserController@profile');
    Route::get('users', 'UserController@users');
    Route::get('getChannelUsers', 'UserController@getChannelUsers');
    Route::get('getUserChannels', 'ChannelController@getUserChannels');

});

ChannelController:

class ChannelController extends Controller
{
    public function getUserChannels(Request $request)
    {
        $this->validate($request, [
            'user_id' => 'required|integer',
        ]);

        /** @var User $user */
        $user = User::find($request->user_id);

        return view('singleuser', ['channels' => $user->channels, 'username' => $user->name]);
    }
}

In the log file no errors.

Thanks for any help and advise.


Go to laravel


r/laravel

Laravel is a free and open-source PHP web framework, created by Taylor Otwell. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as authentication, routing, sessions, and caching.




Members





Online



Help: Laravel 302 post error

So I’m trying to implement user login/login via Laravel. Of course I know how to do this in vanilla PHP but I want to learn how to do it using a framework. I wrote my own code to send user signup data to the database but the authentication process is a pain in the ass and then I realized that Laravel ships with all the code I need so why not use it. I ran the command php artisan make:auth. But I don’t understand how it works under the hood. I am trying to signup a new user and I keep getting a 302 error and I have no idea how to debug it. Also I noticed when Laravel throws errors is has nothing to do with my code its errors from the framework itself which is no help to me.

Archived post. New comments cannot be posted and votes cannot be cast.

More posts you may like

/**
 * Store a newly created resource in storage.
*
 * @param StoreStaffEventLog|Request $request
* @return IlluminateHttpResponse
 */
public function store(StoreStaffEventLog $request)
{
    if (!$this->eventStaffRepository->find($request->staff_event_id)) return $this->respondBadRequest();

    $this->eventStaffLogRepository->create([
         'staff_event_id' => $request->staff_event_id,
         'type' => $request->type,
    ]);

     return $this->respondCreated();
}

I’ll get a 302 response redirect. See JS response below:

XMLHttpRequest cannot load http://company-site.com/api/1.0/staff/logs. Redirect from
'http://company-site.com/api/1.0/staff/logs' to 'http://localhost:8100/' has been blocked 
by CORS policy: Request requires preflight, which is disallowed to follow cross-origin redirect.

But if I replace the StoreStaffEventLog $request to Request $request. My request works fine and returns me a 201 Created status code.

Понравилась статья? Поделить с друзьями:

Не пропустите эти материалы по теме:

  • Яндекс еда ошибка привязки карты
  • 3012 код ошибки
  • 3011 ошибка ман
  • 30104 ошибка опель астра
  • 300 ошибка на сайте

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии