junkins/standard-auth

There is no license information available for the latest version (dev-master) of this package.

CakePHP StandardAuth

dev-master 2016-12-28 14:14 UTC

This package is auto-updated.

Last update: 2024-05-29 03:22:29 UTC


README

Introduction

Authentication mail function when creating a user. Password reminder function.

Setup

  1. you load StandardAuthComponent.
<?php
namespace App\Controller

use Cake\Controller\Controller;

/**
 * AdminAppController
 */
class AdminAppController extends Controller
{
  /**
   * initialize
   */
  public function initialize()
  {
      parent::initialize();
      $this->loadComponent('StandardAuth.StandardAuth', [
        'authenticate' => [
            'Form' => [
                'userModel' => 'Admins',
                'fields' => [
                    'username' => 'username',
                    'password' => 'password'
                ],
            ]
        ],
      ]);
  }
}
  1. you load StandardAuthBehavior.
<?php
namespace App\Model\Table;

use Cake\ORM\Table;

/**
 * AdminsTable
 */
class AdminsTable extends Table
{
  /**
   * initialize
   */
  public function initialize(array $config)
  {
      $this->addBehavior('StandardAuth.StandardAuth');
  }
}
  1. you define action on Controller.
<?php

    /**
     * approval
     */
    public function approval($id, $hash)
    {
        $this->Admins->get($id);
        $result = $this->StandardAuth->approval($id, $hash);
        if ($result) {
            $this->Flash->success(__('The user has been saved.'));
        } else {
            $this->Flash->error(__('Invalid credentials, try again'));
        }
        return $this->redirect(['action' => 'login']);
    }

    /**
    * reset
    * @author ito
    */
    public function reset()
    {
        $this->viewBuilder()->layout('login');
        $entity = $this->Admins->newEntity();
        if ($this->request->is('post')) {
            $data = $this->request->data;
            $entity = $this->Admins->patchEntity($entity, $data, ['validate' => 'reset']);
            if (
                empty($entity->errors()) &&
                $this->Admins->resetPassword($entity->email)
            ) {
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect(['action' => 'login']);
            } else {
                $this->Flash->error(__('The user could not be saved. Please, try again.'));
            }
        }

        $this->set(compact('entity'));
        $this->set('_serialize', ['entity']);
    }
}