Skip to content
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

Request user not changing using beforeResponseCall #741

Closed
1 task done
ferdousanam opened this issue Oct 7, 2023 · 3 comments
Closed
1 task done

Request user not changing using beforeResponseCall #741

ferdousanam opened this issue Oct 7, 2023 · 3 comments
Labels
question Further information is requested sanctum

Comments

@ferdousanam
Copy link

Scribe version

4.22.0

PHP version

8.2.4

Framework

Laravel

Framework version

10.14.1

Scribe config

auth.enabled => true
auth.default => true
auth.name => "token"

What happened?

<?php

namespace App\Providers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Knuckles\Camel\Extraction\ExtractedEndpointData;
use Knuckles\Scribe\Scribe;

class ScribeServiceProvider extends ServiceProvider
{
    public function boot()
    {
        if (class_exists(\Knuckles\Scribe\Scribe::class)) {
            Scribe::beforeResponseCall(function (Request $request, ExtractedEndpointData $endpointData) {
                // Get the current URL prefix
                $urlPrefix = $request->route()->getAction('prefix');

                // Check the URL prefix and set the appropriate token
                if ($urlPrefix === 'admin') {
                    $token = User::where('type', 'admin')->first()->createToken('user')->plainTextToken;
                } else {
                    $token = User::where('type', 'staff')->first()->createToken('user')->plainTextToken;
                }

                // Set the Authorization header
                $request->headers->set("Authorization", "Bearer $token");

                // You also need to set the headers in $_SERVER
                $request->server->set("HTTP_AUTHORIZATION", "Bearer $token");
            });
        }
    }
}

When I generate scribe, The request()->user() remains same in the endpoint. But I debugged the request headers The Authorization value is changed.

info($request->url(), array_merge($request->headers->all(), ['user_type' => $request->user() ? $request->user()->type : 'failed']));

NB: I am using "laravel/sanctum": "^3.2", in this case.

Docs

@ferdousanam ferdousanam added bug Something isn't working triage labels Oct 7, 2023
@shalvah
Copy link
Contributor

shalvah commented Oct 8, 2023

Can you try something like this?

https://github.com/laravel/framework/blob/cddb4f3bb5231f44f18fce304dbf190ad8348ddc/src/Illuminate/Foundation/Testing/Concerns/InteractsWithAuthentication.php#L34

This is what Laravel uses for $this->actingAs($user) in tests.

@shalvah shalvah added question Further information is requested and removed bug Something isn't working triage labels Oct 8, 2023
@ferdousanam
Copy link
Author

<?php

namespace App\Providers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Knuckles\Camel\Extraction\ExtractedEndpointData;
use Knuckles\Scribe\Scribe;
use Laravel\Sanctum\Sanctum;

class ScribeServiceProvider extends ServiceProvider
{
    public function boot()
    {
        if (class_exists(\Knuckles\Scribe\Scribe::class)) {
            Scribe::beforeResponseCall(function (Request $request, ExtractedEndpointData $endpointData) {
                // Get the current URL prefix
                $urlPrefix = $request->route()->getAction('prefix');

                // Check the URL prefix and set the appropriate token
                if ($urlPrefix === 'admin') {
                    $user = User::where('type', 'admin')->first();
                } else {
                    $user = User::where('type', 'staff')->first();
                }

                Sanctum::actingAs($user, ['*']);
            });
        }
    }
}
Sanctum::actingAs($user, ['*']);

This fixed the issue. But I am confused why this happened using token.

@shalvah
Copy link
Contributor

shalvah commented Oct 9, 2023

#580 (comment)

@shalvah shalvah closed this as completed Oct 9, 2023
@shalvah shalvah added the sanctum label Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested sanctum
Projects
None yet
Development

No branches or pull requests

2 participants