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

DbTableAuthAdapter is deprecated how to solve it? #352

Closed
zhushaolei opened this Issue Jan 22, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@zhushaolei
Copy link

zhushaolei commented Jan 22, 2019

  • I was not able to find a solution to the DbTableAuthAdapte deprecated:
    in follwoing snippet:
public function getServiceConfig()
    {
        return array(
            'abstract_factories' => array(),
            'aliases' => array(),
            'factories' => array(
                // SERVICES
                'AuthService' => function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'user','email','password', 'MD5(?)');
                
                $authService = new AuthenticationService();
                $authService->setAdapter($dbTableAuthAdapter);
                return $authService;
                },

Provide a narrative description of what you are trying to accomplish.
I wish to get a correct solution ( a snippet code) against to DbTableAuthAdapter to fix my login functionality and continue to study and practise on the book" Zend Framework2.0 by example"

@zhushaolei

This comment has been minimized.

Copy link
Author

zhushaolei commented Jan 22, 2019

<?php
namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

use Users\Form\LoginForm;
use Users\Form\LoginFilter;

use Users\Model\User;
use Users\Model\UserTable;



class LoginController extends AbstractActionController
{
    protected $storage;
    protected $authservice;
    
    public function getAuthService()
    {
        if (! $this->authservice) {
            $this->authservice = $this->getServiceLocator()->get('AuthService');
        }
        
        return $this->authservice;
    }
    
    public function logoutAction()
    {
        $this->getAuthService()->clearIdentity();
        
        return $this->redirect()->toRoute('users/login');
    }
    
    public function indexAction()
    {
        $form = $this->getServiceLocator()->get('LoginForm');
        $viewModel  = new ViewModel(array('form' => $form));
        return $viewModel;
    }
    
    public function processAction()
    {
        if (!$this->request->isPost()) {
            return $this->redirect()->toRoute('users/login');
        }
        
        $post = $this->request->getPost();
        
        $form = $this->getServiceLocator()->get('LoginForm');
        $userTable = $this->getServiceLocator()->get('UserTable');
        
        $form->setData($post);
        if (!$form->isValid()) {
            $model = new ViewModel(array(
                'error' => true,
                'form'  => $form,
            ));
            $model->setTemplate('users/login/index');
            return $model;
        } else {
            //check authentication...
            $this->getAuthService()->getAdapter()
            ->setIdentity($this->request->getPost('email'))
            ->setCredential($this->request->getPost('password'));
            $result = $this->getAuthService()->authenticate();
            if ($result->isValid()) {
                $this->getAuthService()->getStorage()->write($this->request->getPost('email'));
                return $this->redirect()->toRoute('users/login', array(
                    'action' =>  'confirm'
                ));
            } else {
                $model = new ViewModel(array(
                    'error' => true,
                    'form'  => $form,
                ));
                $model->setTemplate('users/login/index');
                return $model;
            }
        }
    }
    
    public function confirmAction()
    {
        $this->layout('layout/myaccount');
        $user_email = $this->getAuthService()->getStorage()->read();
        $viewModel  = new ViewModel(array(
            'user_email' => $user_email
        ));
        return $viewModel;
    }
    
}
@froschdesign

This comment has been minimized.

Copy link
Member

froschdesign commented Jan 22, 2019

The Zend\Authentication\Adapter\DbTable is not a part of the zend-db component. It belongs to zend-authentication.
Please look at the documentation of zend-authentication. There you will find the notification of the deprecation and the also how you can fix the problem: https://docs.zendframework.com/zend-authentication/adapter/dbtable/intro/#zendauthenticationadapterdbtable-class-is-deprecated

For further questions please our forum: https://discourse.zendframework.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment