Jump to content

Mobile app: how to authenticate users using API - web services


Recommended Posts

Quite honestly I'd create a separately accessible api (that's what I did for the app i'm currently working on), since the default one sucks a bit. In any case for the default, you need to send http authentication headers (depends on your app's language of course)

Link to comment
Share on other sites

Thanks Nemo for reply. 

I am already working the same way as you suggested. Currently developing Android app using Java.

But, I am not sure how would I go for authentication , I have created restful APIs for products and categories but this is freaking me out.

Can you please help me in this regard, Any logic or code will be pretty helpful.

Link to comment
Share on other sites

For more info, Here is the code I am trying to get working.

 

public function checkuser($email, $passwd) {
        $error = array();
        Hook::exec('actionBeforeAuthentication');
        //echo 'hook exe';
        $passwd = trim($passwd);
        $email = trim($email);
        if (empty($email)) {
            $errors[] = 'An email address required.';
        } elseif (!Validate::isEmail($email)) {
            $errors[] = 'Invalid email address.';
        } elseif (empty($passwd)) {
            $errors[] = 'Password is required.';
        } elseif (!Validate::isPasswd($passwd)) {
            $errors[] = 'Invalid password.';
        } else {
            $contaxt = Context::getContext();
            $customer = new Customer();
            $authentication = $customer->getByEmail(trim($email), trim($passwd));
            if (!$authentication || !$customer->id) {
                $errors = Tools::displayError('Email/Password is wrong.');
            } else {
                $context->cookie->id_compare = isset($context->cookie->id_compare) ? $context->cookie->id_compare : CompareProduct::getIdCompareByIdCustomer($customer->id);
                $context->cookie->id_customer = (int) ($customer->id);
                $context->cookie->customer_lastname = $customer->lastname;
                $context->cookie->customer_firstname = $customer->firstname;
                $context->cookie->logged = 1;
                $customer->logged = 1;
                $context->cookie->is_guest = $customer->isGuest();
                $context->cookie->passwd = $customer->passwd;
                $context->cookie->email = $customer->email;
 
                // Add customer to the context
                $context->customer = $customer;
 
                if (Configuration::get('PS_CART_FOLLOWING') && (empty($context->cookie->id_cart) || Cart::getNbProducts($context->cookie->id_cart) == 0) && $id_cart = (int) Cart::lastNoneOrderedCart($context->customer->id)) {
                    $context->cart = new Cart($id_cart);
                } else {
                    $context->cart->id_carrier = 0;
                    $context->cart->setDeliveryOption(null);
                    $context->cart->id_address_delivery = Address::getFirstCustomerAddressId((int) ($customer->id));
                    $context->cart->id_address_invoice = Address::getFirstCustomerAddressId((int) ($customer->id));
                }
 
                $context->cart->id_customer = (int) $customer->id;
                $context->cart->secure_key = $customer->secure_key;
                $context->cart->save();
                $context->cookie->id_cart = (int) $context->cart->id;
                $context->cookie->write();
                $context->cart->autosetProductAddress();
                print_r($context);
                die;
 
                Hook::exec('actionAuthentication');
                //echo 'haha';
            }
        }
        if (count($errors) > 0)
            $fjson = json_encode($errors);
        else
            $fjson = json_encode($context);
        $res = '{'
                . '"status":200,'
                . '"datajson":' . $fjson
                . '}';
 
        return $res;
        
    }
 
Ideally, my json should reply with authentication token and context related info to app whenever asked with username and password.
Link to comment
Share on other sites

  • 5 months later...

Hi all,

 

I am newbie to prestashop. I need your valuable suggestionson the following regard.

 

1) If  develop a mobile app using API from Prestashop as backend, will the mobile app can have all the Cart/Shipping/Catalog rules added in the Prestashop web admin?
2) If change any configuration settings in Prestashop admin, will it be available in mobile app too

In short , whether the changes made in Prestashop admin shall be available for both Web and mobile apps and will they work in similar fashion.

 

 

Please respond with your suggestions. Thanks in advance.

Link to comment
Share on other sites

×
×
  • Create New...