PHP code example of friendsofcake / authenticate

1. Go to this page and download the library: Download friendsofcake/authenticate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

friendsofcake / authenticate example snippets


    //in $components
    public $components = [
        'Auth' => [
            'authenticate' => [
                'FOC/Authenticate.MultiColumn' => [
                    'fields' => [
                        'username' => 'login',
                        'password' => 'password'
                    ],
                    'columns' => ['username', 'email'],
                    'userModel' => 'Users',
                    'scope' => ['Users.active' => 1]
                ]
            ]
        ]
    ];

    // Or in beforeFilter()
    $this->Auth->config('authenticate', [
        'FOC/Authenticate.MultiColumn' => [
            'fields' => [
                'username' => 'login',
                'password' => 'password'
            ],
            'columns' => ['username', 'email'],
            'userModel' => 'Users',
            'scope' => ['Users.active' => 1]
        ]
    ]);

    //in $components
    public $components = [
        'Auth' => [
            'authenticate' => [
                'FOC/Authenticate.Cookie' => [
                    'fields' => [
                        'username' => 'login',
                        'password' => 'password'
                    ],
                    'userModel' => 'SomePlugin.Users',
                    'scope' => ['User.active' => 1]
                ]
            ]
        ]
    ];
    //Or in beforeFilter()
    $this->Auth->authenticate = [
        'FOC/Authenticate.Cookie' => [
            'fields' => [
                'username' => 'login',
                'password' => 'password'
            ],
            'userModel' => 'SomePlugin.Users',
            'scope' => ['Users.active' => 1]
        ]
    ];

    //in $components
    public $components = [
        'Auth' => [
            'authenticate' => [
                'FOC/Authenticate.Cookie' => [
                    'fields' => [
                        'username' => 'login',
                        'password' => 'password'
                    ],
                    'userModel' => 'SomePlugin.Users',
                    'scope' => ['User.active' => 1]
                ],
                'FOC/Authenticate.MultiColumn' => [
                    'fields' => [
                        'username' => 'login',
                        'password' => 'password'
                    ],
                    'columns' => ['username', 'email'],
                    'userModel' => 'Users',
                    'scope' => ['Users.active' => 1]
                ]
            ]
        ]
    ];


App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

	public $components = ['Cookie'];

    public function login() {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                $this->_setCookie();
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }

	protected function _setCookie() {
		if (!$this->request->data('remember_me')) {
			return false;
		}
		$data = [
			'username' => $this->request->data('username'),
			'password' => $this->request->data('password')
		];
		$this->Cookie->write('RememberMe', $data, true, '+1 week');
		return true;
	}

}

    //in $components
    public $components = [
        'Auth' => [
            'authenticate' => [
                'FOC/Authenticate.Token' => [
                    'parameter' => '_token',
                    'header' => 'X-MyApiTokenHeader',
                    'userModel' => 'Users',
                    'scope' => ['Users.active' => 1],
                    'fields' => [
                        'username' => 'username',
                        'password' => 'password',
                        'token' => 'public_key',
                    ],
                    'continue' => true
                ]
            ]
        ]
    ];
    //Or in beforeFilter()
    $this->Auth->config('authenticate', [
        'FOC/Authenticate.Token' => [
            'parameter' => '_token',
            'header' => 'X-MyApiTokenHeader',
            'userModel' => 'Users',
            'scope' => ['Users.active' => 1],
            'fields' => [
                'username' => 'username',
                'password' => 'password',
                'token' => 'public_key',
            ],
            'continue' => true
        ]
    ]);