Can't persist Auth::login with Laravel 5 - laravel

I'm trying to manually log in a user within a Controller or a Route. This works during the scope of the page, but it doesn't persists.
Here is my code :
Route::get('check', function (){
if (Auth::loginUsingId(1)) { // User #1 is existing in the DB
return redirect()->intended('/check2');
}
});
Route::get('check2', function (){
dd(Auth::user());
});
And I got : null on my /check2 page
Can someone give me a quick hint?
PS: I tested the default Auth Laravel system and it works.
Thank you very much!

The issue was simple. On Laravel 5.2, you need to wrap all concerned Routes inside the middleware "web" in order to play with Authentification.
Thanks all.

Related

Route [x] not defined when using auth middleware

I am making a website with laravel but i am getting an error while using middleware auth. it says Route[x] not defined.
i am using laravel 5.8 please some one help me
The route you have written that has no named route so you need to specify name for that route like this
Route::get('x', function() {
//
})->name('x')->middleware('auth');
for more information you can check here https://laravel.com/docs/7.x/urls#urls-for-named-routes

Exclude conditional routes in Laravel

I'm building and application in Laravel and Vuejs where I'm having Laravel routes as below:
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->where('view', '!=', 'admin')->name('home');
I'm using Vue-router so I'm having routing in vuejs, and I'm using history mode. The problem is when I try to call /admin it generally calls HomeController#home method. even if I go deeper like /admin/dashboard it is calling the same home method. I want if admin prefix is being called then it should call HomeController#admin method.
its all okay for me please check this
Route::get('/admin/{view?}', function (){
dd('okay');
})->where('view', '(.*)')->name('admin');
Route::get('/{view?}', function(){
dd('okay1');
})->where('view', '(.*)')->name('home');
So try this
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->name('home');

No notifications are returned

I am trying to get user's unread notifications though my controller.
This works:
public function notifications(){
return \App\User::find(auth()->user()->id)->unreadNotifications()->limit(5)->get();
}
This doesn't, it returns an empty collection:
public function notifications(){
return auth()->user()->unreadNotifications()->limit(5)->get();
}
Could you tell me what I am missing? Thanks in advance.
Using Laravel 5.8 with Backpack 3.5.
The default auth guard of Laravel is overwitten to use Backpack auth in backpack routes, using the UseBackpackAuthGuardInsteadOfDefaultAuthGuard middleware of the permissions manager package. In the rest of the controller auth() and backpack_auth works normally.
Try this:
public function notifications()
return Auth::user()->unreadNotifications()->limit(5)->get();
}
As said in the docs:
You may access the authenticated user via the Auth facade:
Alternatively, once a user is authenticated, you may access the authenticated user via an Illuminate\Http\Request instance. Remember, type-hinted classes will automatically be injected into your controller methods:
Auth and auth() likely don't work here because you're using the Backpack For Laravel authentication which uses a different guard than the default one Laravel uses.
This would probably work for you:
backpack_user()->unreadNotifications()->limit(5)->get();
If that works, here's why:
If you take a look at project/vendor/backpack/base/src/helpers.php you'll see that backpack_user() is an alias for backpack_auth()->user() and backpack_auth does a:
return \Auth::guard(backpack_guard_name());
That's the important bit because it grabs the guard defined config/backpack/base.php (which is backpack by default) and uses that instead of Laravel's default guard of web.

Laravel 5 -- Redirect in Controller is not working in AJAX call

Important: this comes from ajax call.
Everything works perfectly except:
use Illuminate\Routing\Redirector;
public function my_call() {
return redirect()->route('page-1');
}
Throws 500 error.
return view('page-1') works without problems.
Maybe anybody sees what I am doing wrong?
Thank you.
Do you have an actual route with the name that you are using, you cannot redirect to a view as these are returned by routes
Apperantly you cannot do this on server side if you do AJAX call:/
Here an explanation: https://laracasts.com/discuss/channels/vue/redirect-after-ajax-post-request
It's for vue but I believe it applies universally for ajax.

Laravel & PHPUnit: Getting 500 when unit testing "Passport" restricted route

I have a Laravel 5.3 app.
Inside my api.php file, there's a route for posting an answer within a poll.
Route::group(['middleware' => 'auth:api'], function () {
Route::post('/poll/answer', 'API\PollsController#createAnswer');
});
The route is part of a group, restricted by the auth:api middleware, using Laravel's Passport engine.
When calling this route from Postman or any other tool to test APIs, I get 401 which is ok because I'm not attaching the token.
But when unit testing this call using PHPUnit, it returns 500. I have no idea why.
$this->postJson('api/poll/answer');
I'm probably missing a configuration or setup instruction.
Any ideas?
The 500 error was occurring because I forgot to add an app key to the .env.testing file.
It got solved after adding that.

Resources