Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth::user(), not working #51214

Closed
mawuli-agbenyo-creator opened this issue Apr 25, 2024 · 4 comments
Closed

Auth::user(), not working #51214

mawuli-agbenyo-creator opened this issue Apr 25, 2024 · 4 comments

Comments

@mawuli-agbenyo-creator
Copy link

Laravel Version

Laravel Framework 11.0.8

PHP Version

PHP 8.2.7

Database Driver & Version

No response

Description

I can't get the auth user Auth::user() To work. This is my third time facing this problem i use the middleware (auth) to protect my route and i can still access them. i have tried

php artisan key:generate php artisan cache:clear php artisan config:clear I keep getting No Auth

Steps To Reproduce

public function register_school(SchoolRegistrationRequest $request)
{
if (Auth::check()) {
return 'OK Auth';
} else {
return 'No Auth';
}

    try {
        $school = tblschool::create([
            'code' => $request->school_code,
            'name' => $request->school_name,
            'region' => $request->school_region,
            'district' => $request->school_district,
            'email' => $request->school_email,
            'createdate' => now(),
            'createuser' => Auth::user()->email,

        ]);

        if ($school) {
            return response()->json([
                "status" => true,
                "msg" => "School Registered Successfully",

            ]);
        } else {
            return response()->json([
                "status" => false,
                "msg" => "School Registration Failed",
            ]);
        }
    } catch (\Throwable $th) {
        return response()->json([
            "status" => false,
            "system" => true,
            "msg" => $th,
        ], 500);
    } catch (\Exception $e) {
        // Handle exceptions
        return response()->json([
            "status" => false,
            "system" => true,
            "msg" =>'Error: ' . $e->getMessage(),
        ], 500);
      
    } catch (\Error $e) {
        // Handle errors
        return response()->json([
            "status" => false,
            "system" => true,
            "msg" =>'Error: ' . $e->getMessage(),
        ], 500);
    }
}
@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@mawuli-agbenyo-creator
Copy link
Author

It might be challenging to reproduce this bug, but I can add you as a contributor to the project so you can examine the issue.

@mawuli-agbenyo-creator
Copy link
Author

`Route::prefix("School")->group(function () {

Route::get('get_bills_for_school', [intel::class, 'get_bills_for_school'])->name('get_bills_for_school');

});`

` public function get_bills_for_school()
{
try {

        if (Auth::user()) {
            return Auth::user();
        } else {
            return response()->json([
                "status" => false,
                "message" => "User not found",
            ]);
        }
        
        $bills_data = tblbills::where('sch_code', Auth::user()->schoolCode);

        return response()->json([
            "status" => true,
            "data" => ResourceBills::collection($bills_data),

        ]);
    } catch (\Throwable $th) {
        //throw $th;
        return response()->json([
            "status" => false,
            "system" => true,
            "message" => 'Error: ' . $th->getMessage(),
        ], 500);
    }
}`     After authentication, some methods in a controller can successfully retrieve the authenticated user using Auth::user(), while other methods in the same controller cannot. This phenomenon is quite strange.  For Example this is the responds i get from this method `{

"status": false,
"message": "User not found"
}` I have encountered this problem multiple times in the past.

@ghanu119
Copy link

@mawuli-agbenyo-creator
Have you added the Auth middleware in routes? As it did not available in your last comment where you added the Route definition. So, I am confirming.

@laravel laravel locked and limited conversation to collaborators Apr 29, 2024
@driesvints driesvints converted this issue into discussion #51237 Apr 29, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants