friendsofcake/authenticate

This package is abandoned and no longer maintained. No replacement package was suggested.

CakePHP plugin with authentication classes for AuthComponent.

Installs: 41 452

Dependents: 2

Suggesters: 0

Security: 0

Stars: 82

Watchers: 16

Forks: 39

Open Issues: 1

Type:cakephp-plugin

2.0.0 2015-12-22 21:34 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:27:09 UTC


README

Build Status Coverage Status

NOTE: This project is no longer maintained actively.

The Authenticate classes have become redundant or better alternatives have surfaced:

Plugin containing some authenticate classes for AuthComponent.

Current classes:

  • MultiColumnAuthenticate, allow login with multiple db columns in single username field For example username or email
  • CookieAuthenticate, login with a cookie
  • TokenAuthenticate, login with a token as url parameter or header

Requirements

  • CakePHP 3.0

Installation

[Composer]

run: composer require friendsofcake/authenticate:dev-cake3 or add "friendsofcake/authenticate":"dev-cake3" to require section in your application's composer.json.

Usage

In your app's config/bootstrap.php add: Plugin::load('FOC/Authenticate');

Configuration:

Setup the authentication class settings

MultiColumnAuthenticate:

    //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]
        ]
    ]);

CookieAuthenticate:

    //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]
        ]
    ];

Setup both:

It will first try to read the cookie, if that fails will try with form data:

    //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]
                ]
            ]
        ]
    ];

Setting the cookie

Example for setting the cookie:

<?php
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;
	}

}

TokenAuthenticate

    //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
        ]
    ]);