Coder Social home page Coder Social logo

think-auth's Introduction

Thinkphp5 Auth Manger Library.

Usage

First, u need configure some information:

Create DataBase Table:

Run Sql:

CREATE TABLE IF NOT EXISTS users (
    `id` int(11) unsigned not null PRIMARY KEY AUTO_INCREMENT,
    `username` varchar(32) not null,
    `password` varchar(64) not null
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

Or Create a migrations file and add copy the code to up methods:

$table = $this->table('users');
$table->addColumn('username', 'string', ['limit' => 32])
    ->addColumn('password', 'string', ['limit' => 64])
    ->save();

Configure information

1.Create the auth.php file in the application/extra directory,as follows:

<?php
return [
    'model'    => 'app\\model\\user',
    /** User password is use Hash Encrypt */
    'is_hash'  => true,
];

Methods List

<?php
use Qsnh\think\Auth\Auth;

/** Check user if login */
Auth::check();

/** login on app by user id */
Auth::loginByUserId($id);

/** login on app by user model */
Auth::login(think\Model $user);

/** login on app by array  */
Auth::attemp(array $credential, $remember = false);

/** Get logged-in user */
Auth::user();

/** get logged-in user id */
Auth::id();

Thinkphp5 Auth一键集成库

在使用之前,需要配置几项信息:

创建数据表:

运行下面的 Sql:

CREATE TABLE IF NOT EXISTS users (
    `id` int(11) unsigned not null PRIMARY KEY AUTO_INCREMENT,
    `username` varchar(32) not null,
    `password` varchar(64) not null
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

或者创建一个migrations文件将下面的代码复制到的up()里面:

php think migrate:create CreateUsersTable

$table = $this->table('users');
$table->addColumn('username', 'string', ['limit' => 32])
    ->addColumn('password', 'string', ['limit' => 64])
    ->save();

php think migrate:run

配置信息

1.在application/extra目录下面创建auth.php文件,填入下面内容:

<?php
return [
    /** 数据表关联模型 */
    'model'    => 'app\\model\\user',

    /** 如果密码使用hash加密的话 */
    'is_hash'  => true,
];

方法列表

<?php
use Qsnh\think\Auth\Auth;

/** 检测用户是否登录 */
Auth::check();

/** 通过ID登录 */
Auth::loginByUserId($id);

/** 通过已有的 User (extends think\Model) 登录 */
Auth::login(think\Model $user);

/** 通过一些条件登录,$remember=true => 会话长期保存  */
Auth::attemp(array $credential, $remember = false);

/** 获取已经的登录的用户,返回 User (extends think\Model) */
Auth::user();

/** 获取当前登录用户的ID */
Auth::id();

think-auth's People

Contributors

qsnh avatar

Watchers

James Cloos avatar baitongda 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.