Jump to content

Mobile App: authenticate users using APIs - webservices


TheLousyCoder

Recommended Posts

  • 2 months later...

Hi  Nikhil,

 

I somehow made a function that will authenticate a user, I asked my Java developer to store and maintain the userid that I pass after successful authentication.

 

Below function that I write might help you.

 

 

public function authenticate($email, $passwd) {
        $errors = array();
        $ct = 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 {
            $customer = new Customer();
            $context = Context::getContext();
 
            $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();
 
                Hook::exec('actionAuthentication');
                $ct['cart'] = (array) get_object_vars($context->cart);
                $ct['customer'] = (array) get_object_vars($context->customer);
                $ct['cookie'] = (array) get_object_vars($context->cookie);
                //  print_r($ct);
                $token = Tools::getToken(false);
            }
        }
        if (count($errors) > 0) {
            $fjson = json_encode($errors, true);
        } else {
            $fjson = json_encode($ct, true);
        }
 
        $res = '{'
                . '"status":200,'
                . '"datajson":' . $fjson
                . '}';
 
        return $res;
    }
Link to comment
Share on other sites

Yep after  searching I got something on same topic so thanks for reply.

 

In which language you developed your API  for Mobile Development?

 

If you using PHP then would you like to help me to build better and guide the actual flow for the API ??

 

Thanks once again.

 

Nick ['}

Link to comment
Share on other sites

Nice then, Its my pleasure to share with you

 

You said you developing in php but I want to know that you create web services in default directory like

 

-ROOT/webservices

 

or  you create your own

 

-ROOT/MyWebServices/api

 

if you used default then you change or modify the dispatcher.php file

Edited by Nikhil.nj (see edit history)
Link to comment
Share on other sites

Hello dear,

 

Hope you well and fine.

 

I am using library Prestashop's web services and it gives only XML and I want only JSON output..

 

Is it possible to get JSON code using this library if YES then how??

 

Thanks in Advance

 

Nikhil ['}

Link to comment
Share on other sites

  • 2 months later...
  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...