Create login in Cakephp,Authencation in cakephp

Create login in Cakephp,Authencation in cakephp, login Registration in cakephp basic working on cakephp.

steps to working on cakephp

step1:First create database here database name is cakephp.
step2:create users table in database

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`f_name` varchar(255) DEFAULT NULL,
`l_name` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`role_id` tinyint(2) NOT NULL DEFAULT '2',
`status` tinyint(2) NOT NULL DEFAULT '1',
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

step:3 Create folder app->View->Users
login.ctp

<?php
echo $this->Form->create('User');
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->submit();
echo $this->Form->end();
echo $this->Html->link('Register',array('controller'=>'users','action'=>'register')).'&nbsp;&nbsp;&nbsp;';
echo $this->Html->link('FbLogin',array('controller'=>'users','action'=>'fflogin'));
?>

register.ctp

<?php
echo $this->Form->create('User');
echo $this->Form->input('f_name',array('label'=>'First Name'));
echo $this->Form->input('l_name',array('label'=>'Last Name'));
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->submit();
echo $this->Form->end();
?>

step:3 create UsersController file in Controller

<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of UsersController
*
* @author hp
*/
class UsersController extends AppController {

public function beforeFilter() {
parent::beforeFilter();
//parent::sendMail('kumawat.pramodkumar6@gmail.com', 'Test', 'Hi how r U', 'kumawat.pramodkumar6@gmail.com');
}

public function index() {

}

public function login() {

if ($this->Auth->login()) {
$this->redirect($this->Auth->redirectUrl());
} else {
if ($this->request->is('post')) {
$this->request->data['User']['password'] = '';
$this->Session->setFlash('Invalid email and password');
}
}
}

public function dashboard() {
pr($this->Auth->user());
die;
}

public function register() {
$this->loadModel('State');
$arr = $this->State->find('list', array('fields' => array('state_id', 'state')));
$this->set('state', $arr);
// $user = $this->fflogin();
//pr($user);die;
if ($this->request->is('post')) {
$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
if ($this->User->save($this->request->data)) {
$this->Session->setFlash('User created successfully');
$this->redirect(array('action' => 'login'));
} else {
$this->Session->setFlash('Error');
}
}
}
}

step:5 open appcontroller and put this code

<?php

/**
* Application level Controller
*
* This file is application-wide controller file. You can put all
* application-wide controller-related methods here.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link          http://cakephp.org CakePHP(tm) Project
* @package       app.Controller
* @since         CakePHP(tm) v 0.2.9
* @license       http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Controller', 'Controller');

/**
* Application Controller
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @package        app.Controller
* @link        http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
*/
class AppController extends Controller {

var $components = array('Auth', 'Session','Email');
var $helpers = array('Html', 'Form');

public function beforeFilter() {
//echo  $this->Html->url();
$this->Auth->userModel = 'User';
$this->Auth->authenticate = array('Form' => array('scope' => array('role_id' => 2, 'status' => 1), 'fields' => array('username' => 'email')));
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
$this->Auth->autoRedirect = true;
$this->Auth->allow(array('register','get_cities','fblogin','fflogin'));
}
}

step:6 create Model User

<?php
/**
* Application model for CakePHP.
*
* This file is application-wide model file. You can put all
* application-wide model-related methods here.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link          http://cakephp.org CakePHP(tm) Project
* @package       app.Model
* @since         CakePHP(tm) v 0.2.9
* @license       http://www.opensource.org/licenses/mit-license.php MIT License
*/

App::uses('Model', 'Model');

/**
* Application model for Cake.
*
* Add your application-wide methods in the class below, your models
* will inherit them.
*
* @package       app.Model
*/
class User extends AppModel {

}

step7:Logout create function logout in Users controller

public function admin_logout() {
$this->redirect($this->Auth->logout());
}
Share Button

About admin

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Author: pramod kumar kumawat Mob: 9269727062
Free WordPress Themes - Download High-quality Templates