How to force login user on new module

Hello all,

I have create a module for oAuth2 login connexion.
The goal it is to block all trafic on Grav before login on oAuth2.

On my module all is done for oAuth2 part, but now, i am blocking on how to create correclty user and login.

After login on oAuth2, i have write this to create and update user :slight_smile:
$user = User::load($user_data[‘uid’]);

                $user->set('email', $user_data['email']);
                $user->set('fullname', $user_data['name']);
                $user->set('state', 'enabled');
                $user->set('language', 'en');
                $user->set('access',    [
                                            'site' => 
                                                [
                                                    'login' => 'true'
                                                ],
                                            'admin' => 
                                                [
                                                    'super' => 'true', 
                                                    'login' => 'true'
                                                ]
                ]);

// Save user
$user->save();

                // Push user on System
                $this->grav['user']->merge($user->toArray()); 

With this code, it i go to /login, i see i am login. But when i go to /admin, i have a redirection to home website.

i have tried to used :
/** @var Login $login */
$login = $this->grav[‘login’];

                // Fire Login process.
                $event = $login->login(
                    ['username' => $user_data['uid']], 
                    ['remember_me' => true, 'oauth2' => true], 
                    ['authorized' => true, 'authenticated' => true, 'return_event' => true]
                );
                $user2 = $event->getUser(); 

But, when i am use this, $user2 is considerated not authenticated and not authorized.

How to create a user and login user on module to login on front and admin ?

Thx for you helping, my final goal it is to shared all my source code on Github.

Seb

If frontend login works but this is not passed to Admin then I would try to change the “Split session” setting. By default frontend and backend (Admin) sessions are separated. See also https://github.com/getgrav/grav/issues/1085

1 Like

hello

Thx for this information and it work :slight_smile:

Ok now i understand why $this->grav[‘user’] is empty on admin when i push data on front.

But, do you know how to create a session for /admin on Front ? excepted this configuration change ?

Also, i havent found documentation about Login Plugin to use-it ? Because on my code i only push data on $this->grav[‘user’], but when i analyse other plugin with login, they used UserLoginEvent.

  // Fire Login process.
                $event = $login->login(
                    ['username' => $user_data['uid']], 
                    ['remember_me' => true, 'oauth2' => true], 
                    ['authorized' => true, 'authenticated' => true, 'return_event' => true]
                );
                $user2 = $event->getUser();

I think that logging in on the frontend will create an Admin session when Session Split is active. But I’m not sure. I don’t know the answer to your second question, sorry.