How should I check if an account already exists to decide if the user should register or login?

Hello, I have login page and registration page. My users have accounts through email. Login functionality is pretty simple Client.AuthenticateEmailAsync(email, password,create: false);, but how can I make register functionality without login in when account already exists?

If I do it like this: await Client.AuthenticateEmailAsync(email, password,create: true); users can log in on my register page, if they already have an account.

1 Like

@magicbananacake We don’t have a function to check whether an account already exists and switch between register and login. I think for your use case the best option is to just attempt to register the account and if it fails because the account already exists then have your game client programmatically call login instead. This way the behaviour can follow what you want to achieve as the user experience on your register page.

Hey there, I’m having the same issue. Normally I would be content with your reply but, attempting to register with an existing account does not throw any exceptions. So if there is a way to catch that error you mentioned, I would love to learn it.

@Dosutsu Can you share some code that reproduces your issue because I believe that if you set the create=false option when you make the authentication request you will get an error back you an handle in client logic.

@novabyte Thanks for your response. I indeed get an exception when attempting with create=false. However, no exception is being caught when I set it to true.

public async void AuthenticateWithEmailAsync_Signup(string email, string username, string password)
    {
        try
        {
            _session = await _client.AuthenticateEmailAsync(email, password, username, create: true);

            Debug.Log($"Is account new = {_session.Created}");
        }
        catch (Exception e)
        {
            OnSessionCreateAttempt?.Invoke(e.Message + " " + e.GetType());
        }

        try
        {
            var account = await _client.GetAccountAsync(_session);
            var createTime = account.User.CreateTime;

            Debug.Log($"Creation time is {createTime}");
        }
        catch (Exception e)
        {
            OnSessionCreateAttempt?.Invoke(e.Message);
        }
    }

My current workaround is checking if _session.Created is true or false. Hoping it returns false when account is not a new one.

1 Like

@Dosutsu I understand your use case now I think :grinning_face_with_smiling_eyes: You want to know whether the account for the user was newly created or whether the authentication is a re-authentication for the user. The right way to handle this is as you’ve found. You can check the session.Created field which is sent in the authentication response as true only if the account created is new.

1 Like

@novabyte Thanks again for confirming, also thanks for the amazing framework. @magicbananacake I would mark this one solved as the answer above also covers your question too.

1 Like