Login authentication Issue in JS SDK

Hi

We are using the JS SDK in our application and for the authentication module, we are using Backendless inbuilt UserService.

Following is the method we are using to log in

BackendLess.UserService.login(email, password, stayLoggedIn);

In this method, If the stayLoggedIn is true.

So according to the documentation, on the app restart, the methods in the attached screenshot returns either the user Id, user token or the user object, but instead, they return undefined.
Please help us in this case. Our main aim is to skip Login Screen if the user has chosen stayLoggedIn as true.

Hello @Baljeet_Singh,

getCurrentUser is an async method, so you should put await before it or use .then()/.catch() to retrieve result.

We’ll take a look at the LocalCache issue and get back to you with the results.

Regards,
Stanislaw

Any documentation is available @stanislaw.grin ?

Documentation on await, @Baljeet_Singh ? Here’s the first link from google search:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

@Baljeet_Singh I happen to be working on login at the moment. Here’s an example (in React) that shows how JS Promises are used with the login method:

import { useEffect, useState } from 'react';
import { Link, useHistory } from 'react-router-dom';
import Backendless from 'backendless';

const initialFormState = {
    username: '', password: ''
};

function Login(props) {
    const [formState, updateFormState] = useState(initialFormState);
    const history = useHistory();

    useEffect(() => {
        if (props.user && props.user.email) {
            history.push("/");
        }
    });

    function onChange(e) {
        e.persist();
        updateFormState(() => ({...formState, [e.target.name]: e.target.value}));
    }

    function signIn() {
        const {username, password } = formState;
        Backendless.UserService.login(username, password, true)
            .then(function (user) {
                props.setUser(user);
                history.push("/");
            })
            .catch(function(error) {
                console.error(error);
            });
    }

    return (
        <div>
            <h1>Login</h1>
            <input style={{width: 250}} name="username" onChange={onChange} placeholder="username" type="text" /><br />
            <input style={{width: 250}} name="password" onChange={onChange} placeholder="password" type="password" /><br />
            <button onClick={signIn}>Log In</button>
            <p>Need to <Link to="/register">register</Link>?</p>
        </div>
    )
}

export default Login;