Login via API for mobile

I have a mobile application, I use this code for login auth:
and it’s login successfully, but in my mobile app I use BASE_URL to login becuase I want to use the application for more than 1 customer, how I can achieve this with below code,


import frappe
from frappe import auth

def login(usr, pwd):
    login_manager = frappe.auth.LoginManager()
    login_manager.authenticate(user=usr, pwd=pwd)
  except frappe.execeptions.AuthenticationError:
    frappe.local.reponse['message'] = {
      "success_key": 0,
      "message": "Invalid credentials"
    return 'Invalid credentials'
  api_generate = generate_keys(frappe.session.user)
  user = frappe.get_doc('User', frappe.session.user)

  frappe.response['message'] = {
    "success_key": 1,
    "message": "Logged in",
    "sid": frappe.session.sid,
    "api_key": user.api_key,
    "api_secret": api_generate,
    "username": user.username,
    "email": user.email

def generate_keys(user):
  user_details = frappe.get_doc('User', user)
  api_secret = frappe.generate_hash(length=15)

  if not user_details.api_key:
    api_key = frappe.generate_hash(length=15)
    user_details.api_key = api_key
  user_details.api_secret = api_secret

  return api_secret

I’m confused… What do you mean “I want to use the application for more than customer”?

Assuming the user of the mobile app can input the url that points to their Frappe instance, why wouldn’t it work as is?

I’m confused… What do you mean “I want to use the application for more than customer”?

The application is used for more than 1 Frappe instance. becuase I need to input the URL

YOu mean it’s used by more than one Frappe instance at the same time? If so, I don’t have any advice for you. That kind of seems like a disaster…

No, it’s doable without any disaster, becuase I already use some mobile application use ERPnext as back-end working in same way

Any help??

you mean you want to pass the base_url dynamically for all calls. you can save the base_url in a variable in redux and call it for every request within the app. You can save it as a setting in the app (recommended for better UX) before login or ask the user to provide it on login. this can be cleared on logout.

1 Like

If you only need to use the app with a single ERPNext instance at a time then I don’t see it being a problem. Meaning, User1 will use instance1.erpnext.com and User2 will use instance2.erpnext.com.

All you’d need to do is store that base url somewhere in the app, and then base your API calls on that.

so [user].[base_url]/whatever-the-api-endpoint-is

1 Like