I'm using the Laratrust package to manage the authentification.
Now I'm trying to adding a new condition to autheticate.
The user should have the status=1 to login.
So I'm using the function autheicate() defined in laratrust , I'm just adding the column status to verify the authentification.
So I have the following code :
public function authenticate()
{
$this->ensureIsNotRateLimited();
if (! Auth::attempt($this->only('email', 'password','status'->'1'), $this->boolean('remember'))) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => __('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}
But I'm getting the following error :
syntax error, unexpected ''1'' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
If you have any idea about how I can add the condition status to my code , help me.
Thank you in advance.
Please Add Status Middleware In Karnal.php in routeMiddleware array
app/http/karnal.php
protected $routeMiddleware = [
'status' =>\App\Http\Middleware\MiddlewareName::class // change MiddlewareName to your middleware name
]
Related
I'm using laravel 8. I'm trying to implement sign in with google option for a mobile application and developing API using laravel Socialite. I don't know how to do this proper way. But I followed this article
I used google access token and passed using postman.
here is my route
Route::get('/customer/login/google', [CustomerAPIController::class,'google']);
here is my function as the article given
public function google(Request $request)
{
$provider = "google";
$token = $request->input('access_token');
$providerUser = Socialite::driver($provider)->userFromToken($token);
// check if access token exists etc..
$user = User::where('provider_name', $provider)->where('provider_id', $providerUser->id)->first();
// if there is no record with these data, create a new user
if($user == null){
$user = User::create([
'provider_name' => $provider,
'provider_id' => $providerUser->id,
]);
}
// create a token for the user, so they can login
$token = $user->createToken(env('APP_NAME'))->accessToken;
// return the token for usage
return response()->json([
'success' => true,
'token' => $token
]);
}
But I got this error when passing the access token,
"Trying to access array offset on value of type null"
I don't know my progress is correct. But I found the issue is coming from this line of the function,
$providerUser = Socialite::driver($provider)->userFromToken($token);
What is the wrong in here? does my whole procedure incorrect.? how to implement this. IF there is a answer or guide, that would be very helpful.(If the description unclear or if there is missing data please inform)
I'm gonna make a Unit Testing for my resource.
Here my testing function below :
public function testCreateMyResource()
{
$user = factory(\App\User::class)->states('admin')->create();
$this->actingAs($user);
$data = [
'Field' => "Example",
];
$response = $this->actingAs($user)->postJson('/nova-api/my-resource?editing=true&editMode=create',$data);
$response->assertStatus(201);
$response->assertJson(['status' => true]);
$response->assertJson(['message' => "Created!"]);
}
But it was return 403.
I expected to return 201 as I login normally to Nova dashboard and create new record in the form.
It seems like forbidden to access the route inside Testing Class.
Is there anyway to access the route ? Please any body help me to solve this.
I try to use phpunit with Laravel and Spatie but i have a issue.
I have this test :
public function testBasicTest()
{
$user = User::where('id', 2)->first();
$response = $this->actingAs($user, 'api')->json('POST', '/providersList', [
'database' => 'test'
]);
$response->assertStatus(200);
}
But i have a 401 error
Expected status code 200 but received 401. Failed asserting that 200 is identical to 401.
I have this in web.php
Route::group(['middleware' => ['auth:api','role:Admin']], function() {
Route::post('/providersList', 'ProviderController#index');
});
This is a common issue when doing testing, I can assure you that the cause of this error is because of your authentication because of 401 HTTP error code, check if the acting user has role admin
To get a better error output, add this to the top of your test function
$this->withoutExceptionHandling();
It should give a better idea of what the issue is.
I developed an API with Laravel 5.5. All is working fine.
But imagine that a user enter an "api" url directly in the browser (for example: api/books), then they will receive an error:
InvalidArgumentException
Route [login] not defined.
How to prevent this? I tried to add some routes in the routes/web.php file, but without success.
Or perhaps I should do nothing (there will be very few users who will do that)?
I found the answer here:
Laravel 5.5 change unauthenticated login redirect url
I only do that in the "app/Exceptions/Handler.php" file, I modified the function "render" like that :
public function render($request, Exception $exception)
{
// return parent::render($request, $exception);
// add dom
return redirect('/');
// or redirection with a json
/*
return response()->json(
[
'errors' => [
'status' => 401,
'message' => 'Unauthenticated',
]
], 401
);
*/
}
And it works fine. As the "Laravel" part will be used only as back end for APIs, it will be enough.
I am new to PHPUnit and TDD. I just upgrade my project from Laravel 5.4 to 5.5 with phpunit 6.5.5 installed . In the learning process, I wrote this test:
/** #test */
public function it_assigns_an_employee_to_a_group() {
$group = factory(Group::class)->create();
$employee = factory(Employee::class)->create();
$this->post(route('employee.manage.group', $employee), [
'groups' => [$group->id]
]);
$this->assertEquals(1, $employee->groups);
}
And I have a defined route in the web.php file that look like this
Route::post('{employee}/manage/groups', 'ManageEmployeeController#group')
->name('employee.manage.group');
I have not yet created the ManageEmployeeController and when I run the test, instead of get an error telling me that the Controller does not exist, I get this error
Failed asserting that null matches expected 1.
How can I solve this issue please?
The exception was automatically handle by Laravel, so I disabled it using
$this->withoutExceptionHandling();
The test method now look like this:
/** #test */
public function it_assigns_an_employee_to_a_group() {
//Disable exception handling
$this->withoutExceptionHandling();
$group = factory(Group::class)->create();
$employee = factory(Employee::class)->create();
$this->post(route('employee.manage.group', $employee), [
'groups' => [$group->id]
]);
$this->assertEquals(1, $employee->groups);
}
You may not have create the method in the Controller but that doesn t mean your test will stop.
The test runs.It makes a call to your endpoint. It returns 404 status because no method in controller found.
And then you make an assertion which will fail since your post request
wasn't successful and no groups were created for your employee.
Just add a status assertion $response->assertStatus(code) or
$response->assetSuccessful()