I'm using Zend Framework 2.2.4 for my app, I added a 'Sign-in with Google' option for the login page. But it's not redirecting on the exact page, it goes back to the login page and places a #
e.g. myapp.dev/login#
This is the method for the 'Sign-in with Google', this is located at my LoggingController.php
public function loginSocialsAction()
{
$googleClient = $this->getGoogleClient();
$code = $this->params()->fromQuery('code', '');
$googleOauthV2 = new \Google_Oauth2Service($googleClient);
#if successful validation from Google
if (isset($code)) {
$googleClient->authenticate($code);
$gdata = $googleOauthV2->userinfo->get();
$user = $this->getUsersTable()->getByEmail($gdata['email']);
}
if ($googleClient->getAccessToken() && isset($gdata['email'])) {
if (!empty($user)) {
$user->role = $this->getRoleTable()->get($user->roleId);
$session = new Session\Container('currentuser');
$session->user = $user;
$this->initializeSession($session);
// if success, go to records page
return $this->redirect()->toRoute('records');
} else {
$message = 'The user is not in the system.';
$this->flashMessenger()->setNamespace('error')->addErrorMessage($message);
return $this->redirect()->toRoute('login');
}
} else {
$message = 'Google auth failed.';
$this->flashMessenger()->setNamespace('error')->addErrorMessage($message);
return $this->redirect()->toRoute('login');
}
}
Please mention the things that I missed or you want to know. Any idea(s) would really help!
You can try this for redirection.
For Controller Redirection
return $this->redirect()->toUrl($this->getRequest()->getBaseUrl().'/any/path/here/'.<<ID HERE, IF YOU WANT>>);
For View Redirection
In this case, a button Cancel when clicked redirects, resetting the values.
<button type="reset"
<a href="<?php echo $this->basePath('your/path/here/'. <<ID HERE IF ANY>> ) ?>">Cancel
</a>
</button>
The obvious difference can be seen, since controller's URLs are redirected using getBaseUrl()
while view's URLs are redirected using basepath()
.