form request: the request user in null

Published 3 weeks ago by ederson

The task is to test form requests. In the general case it works fine.

There is a problem with one request where a custom validation rule is based on the user who made the request

In this case request()->user() is always null So far i tried 1)$this->be($user); 2)$this->actingAs($user); 3)manually login in a user

all failed.

any ideas on what i am doing wrong ?

Best Answer (As Selected By ederson)
tykus

Is that endpoint in the auth:api middleware group? How are you authenticating users; Passport/JWT?

martinbean

@ederson What does your test look like?

ederson

Sorry i should have added it in the first place It is a very simple test

$this->call('POST', 'api/goup', ['name' => 'Name','team'=>'team_id']);

$this->assertEquals(false, $fails);

then in the form request i extend the validator and access request()->user()

without the custom rule it works

tykus
tykus
3 weeks ago (690,060 XP)

This is an API endpoint? Use the api guard in actingAs call.

ederson

thanks tykus but i ve tried it already

request()->user() returns null

I've checked that the $user passed in actingAs is as actual user

tykus
tykus
3 weeks ago (690,060 XP)

Is that endpoint in the auth:api middleware group? How are you authenticating users; Passport/JWT?

ederson

stupid stupid stupid !!!!!!!!!!!!!!!!!!!!!

i use the api route file and i for some strange reason i assumed that auth:api is applied by default !

thanks for the help!

tykus
tykus
3 weeks ago (690,060 XP)

No worries, please mark my answer above as the best reply to help others.

Please sign in or create an account to participate in this conversation.