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


Messages In This Thread
Very Simple Auth Helper - by nc03061981 - 10-16-2020, 06:32 AM
RE: Very Simple Auth Helper - by MGatner - 10-16-2020, 06:35 AM
RE: Very Simple Auth Helper - by nc03061981 - 10-16-2020, 06:38 AM
RE: Very Simple Auth Helper - by InsiteFX - 10-19-2020, 06:02 AM
RE: Very Simple Auth Helper - by nc03061981 - 10-19-2020, 05:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB