Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "google/apiclient": "^2.0"
- 'Google' =>
- [
- 'googleClientID' => '123456.apps.googleusercontent.com',
- 'googleClientSecret' => 'abcdefghi',
- 'googleRedirectUrl' => 'http://example.com/oauth2callback'
- ]
- $routes->connect('/oauth2callback', ['controller' => 'Account', 'action' => 'confirmlogin'], ['_name' => 'account-google-redirect-url']);
- /**
- * Gmail login method
- */
- public function googlelogin()
- {
- $client = new Google_Client();
- $client->setClientId(Configure::read('Google.googleClientID'));
- $client->setClientSecret(Configure::read('Google.googleClientSecret'));
- $client->setRedirectUri(Configure::read('Google.googleRedirectUrl'));
- $client->se
- tScopes([
- "https://www.googleapis.com/auth/userinfo.profile",
- 'https://www.googleapis.com/auth/userinfo.email'
- ]);
- $url = $client->createAuthUrl();
- $this->redirect($url);
- }
- public function confirmlogin()
- {
- $client = new Google_Client();
- $client->setClientId(Configure::read('Google.googleClientID'));
- $client->setClientSecret(Configure::read('Google.googleClientSecret'));
- $client->setRedirectUri(Configure::read('Google.googleRedirectUrl'));
- $client->setScopes([
- "https://www.googleapis.com/auth/userinfo.profile",
- 'https://www.googleapis.com/auth/userinfo.email'
- ]);
- $client->setApprovalPrompt('auto');
- $usersTable = TableRegistry::get('Users');
- if (isset($this->request->query['code'])) {
- $client->authenticate($this->request->query['code']);
- $this->request->Session()->write('access_token', $client->getAccessToken());
- }
- if ($this->request->Session()->check('access_token') && ($this->request->Session()->read('access_token'))) {
- $client->setAccessToken($this->request->Session()->read('access_token'));
- }
- if ($client->getAccessToken()) {
- $this->request->Session()->write('access_token', $client->getAccessToken());
- $oauth2 = new Google_Service_Oauth2($client);
- $user = $oauth2->userinfo->get();
- try {
- if (!empty($user)) {
- if ((preg_match("/(@example.com)$/", $user['email'])) || (preg_match("/(@example.in)$/", $user['email']))) {
- $result = $usersTable->find('all')
- ->where(['email' => $user['email']])
- ->first();
- if (!empty($result)) {
- $this->AccessControl->setUser($result->toArray(), false);
- $this->Flash->set(__('You have successfuly logged in.'), ['element' => 'success']);
- $this->redirect(['_name' => 'dashboard']);
- } else {
- $data = [];
- $data['email'] = $user['email'];
- $data['first_name'] = $user['givenName'];
- $data['last_name'] = $user['familyName'];
- $data['socialId'] = $user['id'];
- $data['role_id'] = Configure::read('Role.loginWithGmailUserRole');
- //$data matches my Users table
- $entity = $usersTable->newEntity($data);
- if ($usersTable->save($entity)) {
- $data['id'] = $entity->id;
- $this->AccessControl->setUser($data, false);
- $this->Flash->set(__('You have successfuly logged in.'), ['element' => 'success']);
- $this->redirect(['_name' => 'dashboard']);
- } else {
- $this->Flash->error(__('Invalid login.'));
- //redirect to login action
- $this->redirect(['_name' => 'account-login']);
- }
- }
- } else {
- $this->Flash->error(__('Your email is invalid for this application.'));
- //redirect to login action
- $this->redirect(['_name' => 'account-login']);
- }
- } else {
- $this->Flash->error(__('Gmail infos not found.'));
- //redirect to login action
- return $this->redirect(['_name' => 'account-login']);
- }
- } catch (Exception $e) {
- $this->Flash->error(__('Gmail error.'));
- return $this->redirect(['_name' => 'account-login']);
- }
- }
- }
Add Comment
Please, Sign In to add comment