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

Question : How to login programmatically from controller without password or use loginById Function from AuthenticationBase ? #379

Closed
tamapratamaongit opened this issue Jun 15, 2021 · 2 comments

Comments

@tamapratamaongit
Copy link

tamapratamaongit commented Jun 15, 2021

i add google login to my web but i dont know how to login to system after get user data from google callback ..
here my controller.

`public function googlecallback()
{

    $socialite = new SocialiteManager(config('Ci4gen')->social);
    $code = $this->request->getVar('code');

    $user = $socialite->create('google')->userFromCode($code);
    $userId = $this->loginOrRegister($user);
   
    return redirect()->to('admin/dashboard');
}

protected function loginOrRegister($user)
{
    $userModel = new \App\Models\UserModel();
    $userData = $userModel->where('email', $user['email'])->first();
    if ($userData == null) {

        $data = [
            'name' => $user['name'],
            'email'  => $user['email'],
            'provider_id'  => $user['id'],
            'avatar'  => $user['avatar'],
            'username' => $user['username'], 
            'active'  => 1,
            'created_at' => date('Y-m-d H:i:s')
        ];
        $db = \Config\Database::connect();
        $builder = $db->table('users');
        $builder = $builder->insert($data);
        // login here its return error invalid model . 
       //use Myth\Auth\Authentication\AuthenticationBase as BaseAuth;
        $authact = new BaseAuth(config('Auth'));
        $authact->loginByID($db->insertID());
    }else{
        // login programmatically here by $user variable 
    }
}`
@tamapratamaongit tamapratamaongit changed the title #question How to login programmatically from controller without password ? #question How to login programmatically from controller without password or use loginById Function from AuthenticationBase ? Jun 15, 2021
@tamapratamaongit tamapratamaongit changed the title #question How to login programmatically from controller without password or use loginById Function from AuthenticationBase ? Question : How to login programmatically from controller without password or use loginById Function from AuthenticationBase ? Jun 15, 2021
@MGatner
Copy link
Collaborator

MGatner commented Jun 15, 2021

service('authentication')->login($user, true);

@tamapratamaongit
Copy link
Author

thanks, its work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants