Login form in Unity

Hello,
I have a problem I want to make a login and register form. I made it but when I login it changes the password and If I register a new account it will remplace the account that have that username.

Is there a way to create an account verifing that account not exists and when I login only login in an accont previously created?.

My code is this:


using Nakama;

using Nakama.TinyJson;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using UnityEngine;

using UnityEngine.UI;

using TMPro;

public class AccountManager : MonoBehaviour

{

    public ServerConnect ServerConnect;

    public Animator LoginScreen;

    public TMP_InputField LoginEmailField;

    public TMP_InputField LoginNameField;

    public TMP_InputField LoginPassword;

    

    public TMP_InputField RegisterEmailField;

    public TMP_InputField RegisterNameField;

    public TMP_InputField RegisterPassword;

    public IClient Client;

    public ISession Session;

    /// <summary>

    /// Called by Unity when the GameObject starts.

    /// </summary>

    private async void Start()

    {

        // Connect to the Nakama server.

        await ServerConnect.Connect();

    }

    /// <summary>

    /// Login account

    /// </summary>

    public async void LoginAccount()

    {

        string email = LoginEmailField.text;

        string username = LoginNameField.text;

        string password = LoginPassword.text;

        Session = await ServerConnect.Client.AuthenticateEmailAsync(email, password, username);

        var account = await ServerConnect.Client.GetAccountAsync(Session);

        Debug.LogFormat("User id '{0}' username '{1}'", account.User.Id, account.User.Username);

        Debug.LogFormat("User wallet: '{0}'", account.Wallet);

        Debug.LogFormat("New login: {0}, {1}", Session.Created, Session);

        if (account.User.Username != null) {

            LoginScreen.Play("Login to Loading");

        }

    }

    /// <summary>

    /// Create an account

    /// </summary>

    public async void CreateAccount()

    {  

        string email = RegisterEmailField.text;

        string password = RegisterPassword.text;

        string username = RegisterNameField.text;

        Session = await ServerConnect.Client.AuthenticateEmailAsync(email, password, username);

        var account = await ServerConnect.Client.GetAccountAsync(Session);

        Debug.LogFormat("User id '{0}' username '{1}'", account.User.Id, account.User.Username);

        Debug.LogFormat("User wallet: '{0}'", account.Wallet);

        Debug.LogFormat("New register: {0}, {1}", Session.Created, Session);

        if (account.User.Username != null) {

            LoginScreen.Play("Sign Up to Login");

        }

    }

    public async void GetName()

    {  

        var account = await ServerConnect.Client.GetAccountAsync(Session);

        

        }

    }

}

Hi @Jose welcome :wave:

Is there a way to create an account verifing that account not exists and when I login only login in an accont previously created?

Yep. Based on what I can see of your code; In your login panel you make the call to the Nakama Unity client with these parameters:

var createAccount = false;
Session = await ServerConnect.Client.AuthenticateEmailAsync(email, password, username, createAccount);
1 Like

Oh thank you very much!

I’ve a little question more: Is there a native way to login with email or user?

I’ve a little question more: Is there a native way to login with email or user?

What do you mean by “native” way to login? We have support in the server for Google Play, Facebook Instant, Facebook, Apple Sign-in, Game Center, and more. Many of these options have a silent sign-in mode. Is this what you mean by “native”?