How do I customize the context.php in vendor folder?

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
bar_on
Posts: 9
Joined: 28 Jul 2024, 01:45

How do I customize the context.php in vendor folder?

Post by bar_on » 30 Oct 2024, 08:24

In vendor/aimeos/aimeos-core/src/MShop/Context.php, there is a function about retrieving user,

Code: Select all

public function user() : ?string
	{
		if( $this->user instanceof \Closure )
		{
			$fcn = $this->user;
			$this->user = $fcn();
		}


		return $this->user;
	}
I want it to return auth()->id() instead, is there anyway I can override the file just like the usual manager for models, or is there other ways of configuring the authentication?

Version:
"aimeos/aimeos-laravel": "~2023.10",
"php": "^8.1.0",

User avatar
aimeos
Administrator
Posts: 8336
Joined: 01 Jan 1970, 00:00

Re: How do I customize the context.php in vendor folder?

Post by aimeos » 30 Oct 2024, 17:49

No, that won't work and will result in errors whenever $context->user() is called because you would return an unexpected value. Instead, use

Code: Select all

$context->user()->getId()
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

bar_on
Posts: 9
Joined: 28 Jul 2024, 01:45

Re: How do I customize the context.php in vendor folder?

Post by bar_on » 07 Nov 2024, 06:34

Thanks for the reply.

I couldn't get the customer get self jsonapi to work (ie. returning everything null when logged in), except changing the user function (in context.php of vendor folder) to return the laravel auth user id instead. I'm using laravel passport for user auth. Hence I'd like to know:

1. If there is any setup I can do to override the context.php file (just like overriding the model manager through custom extension), and
2. If there is anything wrong with laravel passport compatibility with the library.

bar_on
Posts: 9
Joined: 28 Jul 2024, 01:45

Re: How do I customize the context.php in vendor folder?

Post by bar_on » 07 Nov 2024, 06:47

From another post I read that the user function works if Auth::id() returns the correct user id.
However, Laravel by default uses guard 'web' instead of 'api'. If I change the default guard to api, the aimeos backend wouldn't login. How do I configure aimeos to use guard 'api' instead? (ie. auth('api')->id() instead of Auth::id())

User avatar
aimeos
Administrator
Posts: 8336
Joined: 01 Jan 1970, 00:00

Re: How do I customize the context.php in vendor folder?

Post by aimeos » 08 Nov 2024, 07:56

bar_on wrote: 07 Nov 2024, 06:34 1. If there is any setup I can do to override the context.php file (just like overriding the model manager through custom extension), and
No, and the approach is wrong. Try to find out why the customer item isn't loaded here:
https://github.com/aimeos/aimeos-larave ... t.php#L287
bar_on wrote: 07 Nov 2024, 06:34 2. If there is anything wrong with laravel passport compatibility with the library.
We haven't used Passport for authentication up to now but there should be no principle problem.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

bar_on
Posts: 9
Joined: 28 Jul 2024, 01:45

Re: How do I customize the context.php in vendor folder?

Post by bar_on » 11 Nov 2024, 06:05

bar_on wrote: 07 Nov 2024, 06:47 From another post I read that the user function works if Auth::id() returns the correct user id.
However, Laravel by default uses guard 'web' instead of 'api'. If I change the default guard to api, the aimeos backend wouldn't login. How do I configure aimeos to use guard 'api' instead? (ie. auth('api')->id() instead of Auth::id())
Thanks for the reply! Just one more question, may I know if there's any configurations for setting the auth guard? Thanks!!

User avatar
aimeos
Administrator
Posts: 8336
Joined: 01 Jan 1970, 00:00

Re: How do I customize the context.php in vendor folder?

Post by aimeos » 12 Nov 2024, 08:36

You can change the guard in the ./config/shop.php using the "guards" key:
https://github.com/aimeos/aimeos-larave ... t.php#L281
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

bar_on
Posts: 9
Joined: 28 Jul 2024, 01:45

Re: How do I customize the context.php in vendor folder?

Post by bar_on » 13 Nov 2024, 07:34

aimeos wrote: 12 Nov 2024, 08:36 You can change the guard in the ./config/shop.php using the "guards" key:
https://github.com/aimeos/aimeos-larave ... t.php#L281
It works now! Thank you very much.
For anyone using jsonapi with laravel passport, the setup in shop.php is
'guards' => ['jsonapi' => 'your own guard name'].

Post Reply