Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

How to force login user on new module

Started by Sébastien HOUZET 8 years ago · 3 replies · 782 views
8 years ago

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 🙂
$user = User::load($user_data['uid']);

PHP
                $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();

PHP
                // 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'];

PHP
                // 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

8 years ago

hello

Thx for this information and it work 🙂

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.

PHP
  // 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();
8 years ago

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.

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 54 1 week ago
Plugins · by Xavier, 4 weeks ago
2 61 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1188 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 54 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 79 2 months ago