<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{
    public $user;

    public function __construct()
    {
        parent::__construct();

        // GET SESSION
        $this->user = $this->session->userdata('user');

        // LOGIN CHECK
        if(
            empty($this->user) ||
            !isset($this->user['is_login']) ||
            $this->user['is_login'] != true
        ){
            redirect('login');
        }

        // TOKEN VALIDATION
        $checkUser = $this->db
            ->where('user_id', $this->user['id'])
            ->where('login_token', $this->user['token'])
            ->get('members')
            ->row();

        if(!$checkUser)
        {
            $this->session->unset_userdata('user');

            redirect('login');
        }
    }

    // ADMIN CHECK
    protected function adminAuth()
    {
        if(
            $this->user['usertype'] != 'Admin' &&
            $this->user['usertype'] != 'Retailer' &&
            $this->user['usertype'] != 'Distributor'
        ){
            redirect('login');
        }
    }

    // CUSTOMER CHECK
    protected function customerAuth()
    {
        if($this->user['usertype'] != 'Customer')
        {
            redirect('login');
        }
    }
}