public function loginAction() { $request = $this->getRequest(); $view = new ViewModel(); $loginForm = new LoginForm('loginForm'); $loginForm->setInputFilter(new LoginFilter()); if ($request->isPost()) { $data = $request->getPost(); $loginForm->setData($data); if ($loginForm->isValid()) { $data = $loginForm->getData(); $userPassword = new UserPassword(); $encyptPass = $userPassword->create($data['password']); $this->getAuthService()->getAdapter()->setIdentity($data['email'])->setCredential($encyptPass); $result = $this->getAuthService()->authenticate(); if ($result->isValid()) { $session = new Container('User'); $session->offsetSet('email', $data['email']); $this->flashMessenger()->addMessage(array('success' => 'Login Success.')); // Redirect to page after successful login } else { $this->flashMessenger()->addMessage(array('error' => 'invalid credentials.')); // Redirect to page after login failure } return $this->redirect()->tourl('/article_to_read/public/device'); // Logic for login authentication } else { $errors = $loginForm->getMessages(); //prx($errors); } } $view->setVariable('loginForm', $loginForm); return $view; }
public function indexAction() { $headTitle = $this->getServiceLocator()->get('viewHelperManager')->get('headTitle'); $translator = $this->getServiceLocator()->get('translator'); $headTitle->append($translator->translate('System Login')); $form = new LoginForm(); $vars = array(); $auth = new AuthenticationService(); if (!$auth->hasIdentity()) { $vars['form'] = $form; $request = $this->getRequest(); if ($request->isPost()) { $post_data = $request->getPost(); $form->setData($post_data); // Validate the form if ($form->isValid()) { // Authentication ... $dbAdapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'); // Configure the instance with constructor parameters... $authAdapter = new AuthAdapter($dbAdapter, 'account', 'username', 'password', 'MD5(?)'); // Set the input credential values (e.g., from a login form) $data = $form->getData(); $authAdapter->setIdentity($data['username'])->setCredential($data['password']); $auth = new AuthenticationService(); $result = $auth->authenticate($authAdapter); $vars['result'] = $result; if (!$result->isValid()) { // Authentication failed; } else { // Authentication succeeded; the identity ($username) is stored // in the session // $result->getIdentity() === $auth->getIdentity() // $result->getIdentity() === $username return $this->redirect()->toRoute('auth'); } } } } $view_page = new ViewModel($vars); return $view_page; }
public function loginAction() { $form = new LoginForm(); $form->get('submit')->setValue('Login'); $messages = null; $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter(new LoginFilter($this->getServiceLocator())); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); // $data = $this->getRequest()->getPost(); // If you used another name for the authentication service, change it here // it simply returns the Doctrine Auth. This is all it does. lets first create the connection to the DB and the Entity $authService = $this->getServiceLocator()->get('Zend\\Authentication\\AuthenticationService'); // Do the same you did for the ordinar Zend AuthService $adapter = $authService->getAdapter(); $adapter->setIdentityValue($data['username']); $adapter->setCredentialValue($data['password']); $authResult = $authService->authenticate(); if ($authResult->isValid()) { $identity = $authResult->getIdentity(); $authService->getStorage()->write($identity); $time = 1209600; // 14 days 1209600/3600 = 336 hours => 336/24 = 14 days if ($data['rememberme']) { $sessionManager = new \Zend\Session\SessionManager(); $sessionManager->rememberMe($time); } return $this->redirect()->toRoute('cms/default', array('controller' => 'user', 'action' => 'index')); } foreach ($authResult->getMessages() as $message) { $messages .= "{$message}\n"; } } } return new ViewModel(array('error' => 'Your authentication credentials are not valid', 'form' => $form, 'messages' => $messages)); }