public function indexAction() { $form = new LoginForm(); $authService = $this->getServiceLocator()->get('Zend\\Authentication\\AuthenticationService'); $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $adapter = $authService->getAdapter(); $adapter->setIdentityValue($data['email']); $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']) $authService->getStorage()->session->getManager()->rememberMe($time); // no way to get the session if ($data['remember-me']) { $sessionManager = new SessionManager(); $sessionManager->rememberMe($time); } return $this->redirect()->toRoute('user_dashboard'); } else { $this->flashMessenger()->addErrorMessage("Invalid login credentials provided. Try again, or sign up!"); } } } $view = new ViewModel(array('form' => $form)); return $view; }
public function loginAction() { $auth = new AuthenticationService(); if ($auth->hasIdentity()) { return $this->redirect()->toRoute('home'); } // process the form $form = new LoginForm(); $request = $this->getRequest(); if ($this->getRequest()->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); // check if the user exists $sm = $this->getServiceLocator(); $mapper = $sm->get('User\\Model\\UserMapper'); $params = array('where' => 'username = "******"'); $users = $mapper->select($params); if ($users) { $user = $users[0]; /** * If the account is not active, prompt the user to activate * the account */ if (!$user->getActive()) { return $this->redirect()->toRoute('registration', array('action' => 'confirm', 'id' => $user->getId())); } // authenticate the user $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $adapter = new AuthAdapter($dbAdapter, 'user', 'username', 'password_hash'); $adapter->setIdentity($data['credential']); $adapter->setCredential(hash('sha256', $user->getPassword_salt() . $data['password'])); $result = $auth->authenticate($adapter); if ($result->isValid()) { // store session information in database $mapper = $sm->get('User\\Model\\SessionMapper'); $session = new Session(array('user_id' => $user->getId(), 'ip_address' => $_SERVER['REMOTE_ADDR'], 'login_timestamp' => date('Y-m-d H:i:s'))); $mapper->save($session); // store user information in session variable $container = new Container('user'); $container->user = $user->get_array(); return $this->redirect()->toRoute('home'); } else { foreach ($result->getMessages() as $message) { print "{$message}\n"; } } } else { print "Invalid username/email"; } } } return new ViewModel(array('form' => $form)); }
public function loginAction() { // $p = new Bcrypt(); // $p->setSalt('xTiH$dg0913oJ.ceveiRfewFEBFEbh892e34.wfeguuq12332rtuBYCPLZvcwgams'); // $p1= $p->create('1234'); // echo $p1; // exit(); // if ($this->getServiceLocator()->get('AuthService')->hasIdentity()) { return $this->redirect()->toUrl('/'); } $form = new LoginForm('form'); $form->setInputFilter(new LoginFilter()); $request = $this->getRequest(); if ($request->isPost()) { $data = $request->getPost(); // print "<pre>"; // print_r($data); // exit(); $form->setData($data); if ($form->isValid()) { $data = $form->getData(); //$userPass = new UserPassword(); // print "<pre>"; // echo strlen($userPass->salt); // exit(); // $encryptPass = $userPass->create($data['password']); $authService = $this->getServiceLocator()->get('AuthService'); $authService->getAdapter()->setIdentity($data['username'])->setCredential($data['password']); $result = $authService->authenticate(); if ($result->isValid()) { return $this->redirect()->toUrl('/'); } } else { } } return new ViewModel(array('form' => $form)); }
/** * Action for logging in a user * @return JsonModel */ public function loginAction() { if ($this->request->isOptions()) { return new JsonModel(); } if ($this->request->isPost()) { $loginForm = new LoginForm(); $loginFilter = new LoginFilter(); $loginForm->setInputFilter($loginFilter); $post = get_object_vars(json_decode($this->request->getContent())); $loginForm->setData($post); if (!$loginForm->isValid()) { $errorMessages = array(); foreach ($loginForm->getMessages() as $elementName => $messages) { foreach ($messages as $message) { $errorMessages[$elementName] = $message; } } return new JsonModel(array('error' => 1, 'message' => 'You have an error in your form. Please try again.', 'formErrors' => $errorMessages)); } $formData = $loginForm->getData(); $authAdapter = new AuthAdapter($this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter')); $authAdapter->setTableName('lt_user'); $authAdapter->setIdentityColumn('email'); $authAdapter->setCredentialColumn('password'); $authAdapter->setIdentity($formData['email']); $authAdapter->setCredential($formData['password']); $result = $authAdapter->authenticate(); if (!$result->isValid()) { return new JsonModel(array('error' => 1, 'message' => 'Error while logging in. Please try again')); } else { $auth = $this->getServiceLocator()->get('AuthService'); $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $user = $objectManager->getRepository('Application\\Entity\\LtUser')->findOneBy(array('email' => $formData['email'])); $date = new \DateTime(); $hashRandomize = uniqid('swetea', true); $authToken = md5($formData['email'] . $date->format('Y-m-d') . $hashRandomize); $user->setAuthtoken($authToken); $storage = $auth->getStorage(); $storage->write(array('contactName' => $user->getContactname(), 'userGroup' => $user->getUsergroup(), 'email' => $user->getEmail(), 'userId' => $user->getUserId())); $objectManager->persist($user); $objectManager->flush(); return new JsonModel(array('error' => 0, 'message' => 'Login successful', 'authToken' => $authToken, 'userGroup' => $user->getUsergroup())); } } else { $this->response->setStatusCode(405); return new JsonModel(array('error' => 1, 'message' => 'Request Method not allowed')); } }