Coder Social home page Coder Social logo

unclexo / xouser Goto Github PK

View Code? Open in Web Editor NEW
3.0 0.0 0.0 31 KB

A ZF2 module for user authentication and managing user sessions in database

License: MIT License

PHP 93.70% HTML 6.30%
user-authentication session-management db save-handlers zf2-module zf2

xouser's Introduction

Introduction

XOUser is a skeleton module for user authentication and registration for Zend Framework 2. This is for making a user module very fast by extending its available functionality. XOUser stores session data in database. This is mainly a combination of Zend\Db, Zend\Session and Zend\Authentication for managing users persistently.

Version

Please use one of versions from below:

Supported Zend Framework version
>= 2.2.7 to <= 2.5.0

Features

  • User login - authenticate via username or email (by specifying one of these two and changing that in other places).
  • User registration.
  • User change-password.
  • Forms protected against CSRF.

Installation

Using composer:

  1. In your project, use the following command on your terminal.
$ composer require unclexo/xo-user
  1. Add this XOUser module name in your application.config.php file.
<?php
  return array(
    'modules' => array(
      // ...
      'XOUser',
    ),
    // ...
  );

Database Config:

XOUser expects and assumes you have a valid database configuration under a top key named db.

Database Tables:

XOUser expects two database tables named users and session for managing users and sessions respectively:

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `email` varchar(100) DEFAULT NULL,
  `username` varchar(100) DEFAULT NULL,
  `password` varchar(60) DEFAULT NULL,
  `modifiedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(`id`),
  KEY `idx_email` (`email`), 
  KEY `idx_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `users` (`id`, `email`, `username`, `password`, `modifiedAt`, `createdAt`) VALUES (1, '[email protected]', 'admin', '$2y$10$iMDN8kS81DAdHy9/zNd3we2ChPwhy2bTkVIsCyHpNtaNZl9zUuyxG', '0000-00-00 00:00:00', '0000-00-00 00:00:00');

CREATE TABLE IF NOT EXISTS `session` (
  `id` char(32) NOT NULL,
  `name` char(32) NOT NULL,
  `modified` int(11) DEFAULT NULL,
  `lifetime` int(11) DEFAULT NULL,
  `data` text,
  PRIMARY KEY (`id`),
  KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Session Config:

If you want to set custom configuration for handling session, you should do that under top key session_config. If you do not already have a custom configuration for your session, put the following in ./config/module.config.php:

<?php
'session_config' => array(
  'name' => 'session_name',
  'use_cookies' => true,
  'cookie_lifetime' => 0,
  'gc_maxlifetime' => 3600,
),

Usage:

Use the following snippet of code in your controller method to manage login action. For more information, you should check the LoginController's processAction method.

<?php
$auth = $this->getAuthService()
  ->getAdapter()
  ->setIdentity($data['username'])
  ->setCredential($data['password'])
  ->setIdentityType('username'); // This can only be 'username' and 'email'

$result = $this->getAuthService()->authenticate();

if ($result->isValid()) {
  // Do something
} else {
  // Do something
} 

Next up, just use this over and over again where you need:

<?php
if (!$this->getAuthService()->hasIdentity()) {
  return $this->redirect()->toRoute('auth');
}

Available Routes

/auth 	
/auth/login
/auth/signup
/auth/change-password
/auth/logout

Go to your site: http://yoursite.dev/auth and you should see a login page.

Login

username: admin
password: 12345678

Password Hash Caution

DO NOT CHANGE THE PASSWORD HASH SETTINGS FROM THEIR DEFAULTS unless you have fully understood exactly what and why you are doing!

ZF2 Components

The following ZF2 components are considerably used in XOUser module:

License

This ZF2 module released under MIT license.

xouser's People

Contributors

unclexo avatar

Stargazers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.