Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

currentUser.profile is not persistent after app restart #3561

Closed
AdriaanRoets opened this issue Feb 5, 2021 · 5 comments · Fixed by #3895
Closed

currentUser.profile is not persistent after app restart #3561

AdriaanRoets opened this issue Feb 5, 2021 · 5 comments · Fixed by #3895

Comments

@AdriaanRoets
Copy link

AdriaanRoets commented Feb 5, 2021

## Goals

When logging in a user I use metadata which is added to their currentUser.profile property which contain their partitionKey values needed to open a realm.
On app.login() the user object contains the profile property which contains all the needed metadata.
After the app is restarted, the user object persists and is still logged in, but the currentUser.profile property is empty.

Expected Results

app.currentUser should persist when the app is restarted, including the profile property.

Actual Results

app.currentUser.profile is empty after app restarts.

Steps to Reproduce

Code Sample

import Realm from 'realm';
import {getRealmApp} from './getRealmApp';

const app = getRealmApp();
const AuthContext = React.createContext(null);

const AuthProvider = ({children}) => {
  const [user, setUser] = useState(app.currentUser);

const signIn = async ({username, password}) => {
       getCustomJwt({username, password})
      .then((jwt) => {
        const credentials = Realm.Credentials.jwt(jwt);
        // Authenticate the user
        app
          .logIn(credentials)
          .then((loggedInUser) => {
            setUser(loggedInUser);
          })
          .catch((err) => console.error('Failed to log in', err));
      })
      .catch((err) => console.log(err));
  };

  return (
    <AuthContext.Provider
      value={{
        signIn,
        user,
      }}>
      {children}
    </AuthContext.Provider>
  );
};
const useAuth = () => {
  const auth = useContext(AuthContext);
  if (auth == null) {
    throw new Error('useAuth() called outside of a AuthProvider?');
  }
  return auth;
};

export {AuthProvider, useAuth};

Version of Realm and Tooling

  • Realm JS SDK Version: 10.1.4
  • Node or React Native: React Native
  • Client OS & Version: Android 8.0.0
  • Which debugger for React Native: Chrome DevTools
@kneth
Copy link
Contributor

kneth commented Feb 8, 2021

As Realm.User.profile is populated server-side, and it is a read-only property. You can use Realm.User.customData but it is also populated server-side. You can use Realm.User.apiKeys() for this.

@nirinchev
Copy link
Member

The profile data should be persisted between app restarts - I'm fairly certain I've seen code in OS that does that, so either we're not loading it correctly in OS or there's an issue with how JS is proxying it.

@AdriaanRoets
Copy link
Author

I just need some way to persist an ID value in the User object that I use to open a realm for the user. I have a multi-tenant app and I partition the realms according to each tenant ID, which I store in the profile data (I use Custom JWT for authenticating), but every time the app restarts the User object loses it's profile data so I can't open a realm with the tenant ID.

@kneth
Copy link
Contributor

kneth commented Aug 10, 2021

@yourstruly22 It is fixed by #3895 and will be included in the next release.

@AdriaanRoets
Copy link
Author

@kneth Thank you, I appreciate

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants