Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie helper not woking #2848

Closed
ChibuezeAgwuDennis opened this issue Apr 19, 2020 · 8 comments
Closed

Cookie helper not woking #2848

ChibuezeAgwuDennis opened this issue Apr 19, 2020 · 8 comments

Comments

@ChibuezeAgwuDennis
Copy link

Hello I try to use the cookie system to store some information and also retrieve it I discovered that all the helper is not working.
In my library class fore remember me I try using the set_cookie(), get_cookie(), delete_cookie() after I have include the cookie helper through the function helper('cookie). Please I think this is a bug it was working now is not working again

@lonnieezell
Copy link
Member

Please provide some code examples. We need more to go on then It's not working.

@ChibuezeAgwuDennis
Copy link
Author

ChibuezeAgwuDennis commented Apr 20, 2020

Here is the my code for setting the cookie

if ($remember === 1) {
            
            set_cookie(array(
                'name' => getConfig('auth_cookie_id'),
                'value' => $this->_user->enc_key,
                'expire' => \time() + getConfig('auth_cookie_expiry'),
                'httponly' => FALSE
            ));

            // store encrypted key in cookie
            set_cookie(array(
                'name' => getConfig('auth_cookie_key'),
                'value' => $enkey,
                'expire' => \time() + getConfig('auth_cookie_expiry'),
                'httponly' => FALSE
            ));
        } else {
            // set what will be stored in the session
            $data = array(
                getConfig('auth_session_id') => $this->_user->enc_key,
                getConfig('auth_session_key') => $enkey
            );

            // store data in the session
            session()->set($data);
        }

Here, is where i try to get the cookie data
if (get_cookie(getConfig('auth_cookie_id'), TRUE) && get_cookie(getConfig('auth_cookie_key'), TRUE)) {
            // store cookie informations in variables
            $encryptedUserIDs = get_cookie(getConfig('auth_cookie_id'), TRUE);
            echo $enckey = get_cookie(getConfig('auth_cookie_key'), TRUE);

            $validate_using = 'cookie';
        } else {
            // store session informations in variables
            $encryptedUserIDs = session()->get(getConfig('auth_session_id'));
            $enckey = session()->get(getConfig('auth_session_key'));

            $validate_using = 'session';
        }

@lonnieezell
Copy link
Member

You might need to double-check your code, then. I modified the default Home controller that ships with the framework to this:

public function index()
	{
		helper('cookie');

		$cookie = get_cookie('foo');
		d($cookie);

		set_cookie([
			'name' => 'foo',
			'value' => 'bar'. time(),
			'expire' => \time() + 3600,
			'httponly' => FALSE
		]);

		return view('welcome_message');
	}

This example doesn't test the delete_cookie but the others are working just fine.

Can you be more specific about what's not working?

@ChibuezeAgwuDennis
Copy link
Author

Sorry for late comment. Here is the code.

    namespace App\Libraries;

use App\Models\Remember;
use App\Models\User;
use Config\Services;

class Auth
    {
        protected $encrypter;
        protected $user_model;
        protected $remember;

        function __construct()
        {
            \helper('cookie');
            $this->encrypter = Services::encrypter();
            $this->user_model = new User();
            $this->remember     = new Remember();
        }


        public function login($username_email = null, $password = null, $remember_me = false)
        {
            if (is_null($username_email) || is_null($password)) 
            {
                $this->alert->error('Please enter your credentials in the field provided');
                return false;
            }

            $user = $this->user_model->where('email', $username_email)->first();

            if (!is_null($user) && $this->verifyPassword($password, $user->password)) {
                
                $save_user_data = $user->id;
                if ($encrypt_data = $save_user_data) {
                    
                    if ($remember_me) {
                        
                        $data = [
                            'expires'         => strtotime(getConfig('remember_me_expires')),
                            'persistence'     => $encrypt_data,
                            'selector'        => $this->generateToken()
                        ];
                        return $this->setRememberMe($data);
                    }
                    session()->set(['ses_id' => $encrypt_data, 'loggedIn' => true]);
                    return \true;
                }
                
                return \false;
            }
            
            return false;
        }

        public function verifyPassword($pass = null, $hashed_pass = null)
        {
            return password_verify(
                base64_encode(
                    hash('sha384', $pass, true)
                ),
                $hashed_pass
            );
        }
        
        public function setRememberMe($data = [])
        {
            if (\set_cookie(
                \getConfig('auth_rememer_name'),
                $data['selector'],
                $data['expires']
            )) {
                $this->remember->insert($data);
                return true;
            }
        }
        
        public function checkRememberMe()
        {
            $cookie = \get_cookie(\getConfig('auth_rememer_name'));
            
            if ($cookie) {
                
                session()->set(['ses_id' => $stored->persistence, 'loggedIn' => true]);
            }
        }

        public function getUser()
        {
            return $this->user_model->first(session()->ses_id);

        }
        
        function generateToken($length = 20)
        {
            return bin2hex(random_bytes($length));
        }
        
        public function is_logged_in()
        {
            if ($this->checkRememberMe()) {
                
            }
            if (session()->loggedIn) {
                return true;
            }
            
            return false;
        }
        
        public function logout()
        {
            session()->remove(['ses_id', 'loggedIn']);
            return true;
        }
    } 

Please check it

@ChibuezeAgwuDennis
Copy link
Author

@lonnieezell Please help me check this code The Auth Library in order to see where I am getting wrong. Forgive me of tagging your name. I needed this urgently. I have waited but no check on it. And also I can login to my account in the forum. Here is the error Authorization code mismatch. Are you accessing this function correctly? Please go back and try again.

@lonnieezell
Copy link
Member

You cannot login to your forum account? Or you can? If you cannot, let me know the error and I'll see what I can do to get you in, if the password reset feature isn't working.

Unfortunately, I cannot review your code here. This is for posting bugs. The forums are where you'll get more eyes on it and more help anyway.

@ChibuezeAgwuDennis
Copy link
Author

When I submit my credentials I get this message in return Are you accessing this function correctly? Please go back and try again. here is my forum username Chivinsdev

@michalsn
Copy link
Member

It can be anything. For example, CodeIgniter doesn't have a getConfig() helper function. Sorry, but we won't be debugging a whole class here. Cookies are working fine.

@ChibuezeAgwuDennis if you will find an easy way to recreate the problem, feel free to open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants