Login with identity OR email

How can I make it so my html/js login/forgotPassword input-fields accept identity OR email fer login id?

Hi @JesterOC

The server will match only value from the “identity” column, so for instance:

  • you have “username” and “email” columns in your Users table
  • the “username” is the identity column
  • the login/forgotPassword api expects the “username” only

You want to allow your customers to use both “email” or “username”, is that correct?
Well, to achieve this, you can use before EventHandlers to run your custom logic before the system operation:

  1. find a user with property1 == {loginInput} or property2 == {loginInput} or propertyX == {loginInput}
  2. then use the identity column value to login

Same for other operations

Regards,
Vlad

Trying it now, but only displayName (my identity column) is logging in..

email says invalid login or password.

error message from console says

POST https://api.backendless.com/FF55A111-06AF-7EB1-FFDE-B5B62760CA00/C1E76A66-370C-4491-B48E-57AC3520FCA7/users/login
401 (Unauthorized)

could you please provide curl of the login request to see what your request payload look like.

You can use fake credentials for now

idk exactly what curl is or how to use it so you’d have to walk me through it..

my js code im using looks like this

Backendless.UserService.login(login.username, login.password).then(
            function (user) {
                user ? showLoginSuccess(user) : showInfo("Login failed");
            },
            onError
        );

In your event handler there is a wrong whereClause

Is this proper? cus i tried but it still doesn’t work

try to add a print and check if it can load/find a user with the query

i added logging to print user value

logs say

2025-10-27 14:21:10,324 | SERVER_CODE | INFO | [3961564] [fb677189-833f-42f5-b054-a88f623c4e0c] [INVOKE HANDLER] user.beforeLogin (async)
2025-10-27 14:21:10,513 | TEST | ERROR | [{“___class”:“Users”,“SERIAL”:“354B4784CD6AC92EBBC2”,“lastLogin”:1761574486000,“character_data”:{“I’m the boss”:“1337”},“userStatus":“ENABLED”,“gender”:1,“created”:1615400957000,“displayName”:“JesterOC”,“accountType”:“BACKENDLESS”,“init_user_upon_login”:false,“skin”:11,“PAID”:true,“avatar”:null,“socialAccount”:“BACKENDLESS”,“ownerId”:“B7A24F67-7D8C-4E91-9647-057871EB48E0”,“oAuthIdentities”:null,“hasTesterRole”:false,“blUserLocale”:“en”,“updated”:1757774583103,“objectId”:“B7A24F67-7D8C-4E91-9647-057871EB48E0”,“email”:"ocey92@gmail.com”}]
2025-10-27 14:21:10,514 | SERVER_CODE | ERROR | [3961564] Error: the first argument must be either a string or a number
at Users._callee2$ (/usr/local/lib/node_modules/backendless-coderunner/node_modules/backendless/lib/users/index.js:79:21)
at Users. (/usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:52:18)
at Generator. (/usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/regenerator.js:52:51)
at Generator.next (/usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/regeneratorDefine.js:11:21)
at asyncGeneratorStep (/usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:17)
at _next (/usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/asyncToGenerator.js:17:9)
at /usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:7
at new Promise ()
at Users. (/usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/asyncToGenerator.js:14:12)
at Users.login (/usr/local/lib/node_modules/backendless-coderunner/node_modules/backendless/lib/users/index.js:122:24)

update.

the get_property “displayName” of user variable is the issue. it returns undefined
i tried checking if get property “email” printed, it was also undefined..
I don’t see why it would be undefined… both of those properties exist in user variable

I see, the Find Records block returns a list of objects, need to retrieve the first element from the list

I’m not familiar with getting the property out of the list, can you detail how to get the first element? and the displayName parameter?

update:
I can get the displayName now, however..
“Invalid login or password” is the result if i try login with email
POST 401 (Unauthorized)

logging in with displayName still works.. got me buggered. I would’ve thought it should be solved now that I can pass the identity to login codeblock.

1 Like

i’ll try it but i had a slightly different implementation where I got the object in the list and then "get property ‘displayName’ worked.. but as i said it was still given me an error

i’ll try your implementation and see if it makes a differ

Still an error with email somehow… it finds the user from the table and i can print the displayName (identity column).
No idea why it says 401 (Unauthorized) for the users/login POST though

heres the full error from the console.. logs report no errors but it fails with an error if i try email

https://api.backendless.com/FF55A111-06AF-7EB1-FFDE-B5B62760CA00/C1E76A66-370C-4491-B48E-57AC3520FCA7/users/login 401 (Unauthorized)

||sendRequest|@|backendless.js:13430|
||sendXmlHttpRequest|@|backendless.js:13391|
||(anonymous)|@|backendless.js:13049|
||send|@|backendless.js:12961|
||then|@|backendless.js:12982|
||_callee2$|@|backendless.js:11014|
||tryCatch|@|backendless.js:24202|
||(anonymous)|@|backendless.js:24290|
||(anonymous)|@|backendless.js:24231|
||asyncGeneratorStep|@|backendless.js:23791|
||_next|@|backendless.js:23805|
||(anonymous)|@|backendless.js:23810|
||webpack_modules../node_modules/@babel/runtime/helpers/asyncToGenerator.js.module.exports|@|backendless.js:23802|
||login|@|backendless.js:11024|
||loginUser|@|user-login.js:141|
||dispatch|@|jquery.min.js:2|
||(anonymous)|

any idea why it’s failing?

this sort of implementation works if i deactivate non-blocking option in event handlers.
Thanks for all your help Vladimir :slight_smile:
It’s now working as expected