Welcome Guest, Not a member yet? Register   Sign In
Very Simple Auth Helper
#1
Smile 

PHP Code:
<?php

    
/*
        * Base Auth - Helper
        - register
        - login
        - logout
        
        * Table: ci_users
        - id
        - username
        - password_hash
    */

    
function register($username$password)
    {
        
$db      db_connect();
        
$builder $db->table('ci_users');
        
$arrW    = ['username' => $username];
        
$users   $builder->getWhere($arrW1);
        if (
count($users->getResult()) == 1) {
            return [
                
'status' => false,
                
'code'   => 'ExistUsername'
            
];
        }
        
        
$arrA = [
            
'username'      => $username,
            
'password_hash' => hashPassword($password)
        ];
        
$builder->insert($arrA);
        
        return [
            
'status' => true,
            
'code'   => 'Success'
        
];
    }
    
    function 
login($username$password)
    {
        if (
session()->appUID 0) {
            return 
true;
        }
        
        
$db      db_connect();
        
$builder $db->table('ci_users');
        
$arrW    = [
            
'username'      => $username,
            
'password_hash' => hashPassword($password)
        ];
        
        
$users $builder->getWhere($arrW1);
        if (
count($users->getResult()) == 1) {
            
$row $users->getRow();
            
session()->set([
                
'appUID'     => $row->id,
                
'appUSN'     => $username,
                
'appIsAdmin' => ($username == 'admin' true false)
            ]);
            
            return 
true;
        }
        
        return 
false;
    }
    
    function 
logout()
    {
        
session()->remove([
            
'appUID',
            
'appUSN',
            
'appIsAdmin'
        
]);
    }
    
    function 
users()
    {
        if (
session()->appUID 0) {
            
$db      db_connect();
            
$builder $db->table('ci_users');
            
$arrW    = ['id' => session()->appUID];
            
$users   $builder->getWhere($arrW1);
            if (
count($users->getResult()) == 1) {
                return 
$users->getRow();
            }
        }
        
        return 
null;
    }
    
    function 
hashPassword($string)
    {
        
$aConf  config('App');
        
/*
            Example:
            $aConf->authKey = 'a#C';
            $aConf->authIv  = '@fB';
        */
        
$pass   false;
        
$method "AES-256-CBC";
        
$key    hash('sha256'$aConf->authKey);
        
$iv     substr(hash('sha256'$aConf->authIv), 016);
        
$pass   base64_encode(openssl_encrypt($string$method$key0$iv));

        return 
$pass;
    }
    
    function 
is_logged()
    {
        return (bool)
session()->appUID;
    }
    
    function 
is_admin()
    {
        return 
session()->appIsAdmin;
    }
    
    function 
is_username()
    {
        return 
session()->appUSN;
    } 

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#2

Very simple indeed! Folks looking for a functional approach will probably appreciate this.

One recommendation: check out the framework guidelines for Authentication extensions: https://codeigniter4.github.io/CodeIgnit...ation.html
Following these ensures that developers can build on common expectations for Authentication that are not specific to one package.
Reply
#3

(10-16-2020, 06:35 AM)MGatner Wrote: Very simple indeed! Folks looking for a functional approach will probably appreciate this.

One recommendation: check out the framework guidelines for Authentication extensions: https://codeigniter4.github.io/CodeIgnit...ation.html
Following these ensures that developers can build on common expectations for Authentication that are not specific to one package.

Thanks @MGatner

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#4

Instead of using getResult(); which is for multiple records use getRow(); single record.

You could also use $this->db->countAllResults(); Instead of count();
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 10-19-2020, 05:32 PM by nc03061981.)

(10-19-2020, 06:02 AM)InsiteFX Wrote: Instead of using getResult(); which is for multiple records use getRow(); single record.

You could also use $this->db->countAllResults(); Instead of count();

- countAllResults() will execute query again and then count results, so only use this method when you not yet executed query
- count($query->getResult()) only count nums of result return, so use this when you executed query
- count($query->getResult()) will faster count($query->getRow()), because getRow will call ->getFirstRow before count

- to check have result or not, should use empty($query->getResult())
- to check nums rows, should use count($query->getResult())

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply




Theme © iAndrew 2016 - Forum software by © MyBB