Skip to content

Email and Password

Allow users to sign in with a password connected to their email address in your Unity game.

Setting up authentication

First, create a manager class to handle Openfort authentication:

using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class OpenfortAuthManager : MonoBehaviour 
{
    private OpenfortSDK openfort;
    
    private async void Start()
    {
        if (OpenfortSDK.Instance != null)
        {
            openfort = OpenfortSDK.Instance;
        }
        openfort = await OpenfortSDK.Init("YOUR_OPENFORT_PUBLISHABLE_KEY");
    }
}

Sign up a new user

To register a new user with email and password:

public class OpenfortAuthManager : MonoBehaviour 
{
    // ... previous code ...
 
    public async Task SignUpNewUser(string email, string password)
    {
        try 
        {
            await openfort.auth.signUpWithEmailPassword(email, password);
            Debug.Log("User signed up successfully");
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Error signing up user: {e.Message}");
        }
    }
 
    // Optional: Send email verification
    public async Task RequestEmailVerification(string email)
    {
        try 
        {
            await openfort.auth.requestEmailVerification(email);
            Debug.Log("Verification email sent");
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Error sending verification email: {e.Message}");
        }
    }
}

Log in a user

To authenticate an existing user:

public class OpenfortAuthManager : MonoBehaviour 
{
    // ... previous code ...
 
    public async Task LogInUser(string email, string password)
    {
        try 
        {
            var response = await openfort.auth.logInWithEmailPassword(email, password);
            Debug.Log("User logged in successfully");
            
            // The response contains:
            // - response.player: user information
            // - response.token: Authentication token
            // - response.refreshToken: Token for refreshing authentication
            
            // Store these tokens as needed for your game
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Error logging in: {e.Message}");
        }
    }
}

Authentication response

Upon successful authentication, you'll receive a response containing:

{
  "player": {
      "id": "pla_cc9ed2b7-c5f5-4c43-8dca-c4b104ba1762",
      "object": "player",
      "createdAt": 1710976453,
      "linkedAccounts": [
          {
              "provider": "email",
              "disabled": false,
              "verified": true,
              "email": "hello@example.com"
          }
      ]
  },
  "token": "eyJhbGci...",
  "refreshToken": "eyJhbGci..."
}

Log out

To log out a user:

await openfort.auth.logout();

Password reset flow

For implementing a password reset flow in your Unity game:

public class OpenfortAuthManager : MonoBehaviour 
{
    // ... previous code ...
 
    public async Task RequestPasswordReset(string email)
    {
        try 
        {
            await openfort.auth.requestResetPassword(email);
            Debug.Log("Password reset email sent");
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Error requesting password reset: {e.Message}");
        }
    }
 
    public async Task ResetPassword(string email, string newPassword, string verificationState)
    {
        try 
        {
            await openfort.auth.resetPassword(email, newPassword, verificationState);
            Debug.Log("Password reset successfully");
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Error resetting password: {e.Message}");
        }
    }
}

UI integration example

Here's a basic example of how to integrate this with Unity UI:

public class AuthUIManager : MonoBehaviour
{
    [SerializeField] private TMP_InputField emailInput;
    [SerializeField] private TMP_InputField passwordInput;
    [SerializeField] private Button signUpButton;
    [SerializeField] private Button loginButton;
    
    private OpenfortAuthManager authManager;
 
    private void Start()
    {
        authManager = GetComponent<OpenfortAuthManager>();
        
        signUpButton.onClick.AddListener(HandleSignUp);
        loginButton.onClick.AddListener(HandleLogin);
    }
 
    private async void HandleSignUp()
    {
        await authManager.SignUpNewUser(emailInput.text, passwordInput.text);
    }
 
    private async void HandleLogin()
    {
        await authManager.LogInUser(emailInput.text, passwordInput.text);
    }
}