# How to use another model for authentication using Auth Component

**URL:** <https://discourse.cakephp.org/t/how-to-use-another-model-for-authentication-using-auth-component/11732>\
**Category:** Need Help\
**Tags:** help\
**Created:** [December 11, 2023, 8:08am UTC](https://discourse.cakephp.org/t/how-to-use-another-model-for-authentication-using-auth-component/11732 "2023-12-11T08:08:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rods](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Rods](https://discourse.cakephp.org/u/Rods)\
**Post date:** [December 11, 2023, 8:08am UTC](https://discourse.cakephp.org/t/how-to-use-another-model-for-authentication-using-auth-component/11732/1 "2023-12-11T08:08:24Z")

</div>

Uploading: image.png…  
public function login(){

```
    if($this->Auth->user('id')){
        $this->Flash->success(ucwords('You are already login!'));
        return $this->redirect(['prefix' => false, 'controller' => 'Students', 'action' => 'logout']);
    }

    $student = $this->Students->newEmptyEntity();

    $this->Auth->setConfig('authenticate', [
        'Form' => [
            'fields' => ['username' => 'code'],
            'userModel' => 'Students',
            'finder' => 'auth',
        ],
    ]);

    if ($this->request->is('post')) {
        $student = $this->Auth->identify();

        $auth = $this->Students->find()
            ->where([
                'Students.code like'=>'%'.$this->request->getData('code').'%',
            ])
            ->count();

        if($auth > 0){

            dd($student);

            if (!empty($student)) {

                $this->Auth->setUser($student);
                $student = $this->Students->get($this->Auth->user('id'));

                if($this->Students->save($student)){
                    $result=['message'=>ucwords('Log In Complete'),'result'=>ucwords('success'),
                        'redirect' => Router::url(['prefix' => 'Student', 'controller' => 'Reservations', 'action' => 'index'])];
                    return $this->response->withStatus(200)->withType('application/json')
                        ->withStringBody(json_encode($result));
                }else{
                    $result=['message'=>ucwords('Log In Complete'),'result'=>ucwords('success'),
                        'redirect' => Router::url(['prefix' => 'Student', 'controller' => 'Reservations', 'action' => 'index'])];
                    return $this->response->withStatus(200)->withType('application/json')
                        ->withStringBody(json_encode($result));
                }

            }else{
                $result=['message'=>ucwords('Please Check you ID Number'),'result'=>ucwords('error')];
                return $this->response->withStatus(422)->withType('application/json')
                    ->withStringBody(json_encode($result));
            }

        }else{
            $result=['message'=>ucwords('Cannot Count'),'result'=>ucwords('error')];
            return $this->response->withStatus(422)->withType('application/json')
                ->withStringBody(json_encode($result));
        }

    }
    $this->set(compact('student'));
}

```
