Skip to content

Email and Password

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

Methods

SignUpWithEmailPassword

Creates a new user account with email and password.

Method Signature:
public async UniTask<AuthResponse> SignUpWithEmailPassword(string email, string password, string name = null)
Parameters:
  • string email - User's email address
  • string password - User's password
  • string name - Optional display name for the user
Returns:
  • UniTask<AuthResponse> - Authentication response containing user info and tokens
AuthResponse Structure:
public class AuthResponse
{
    public AuthPlayerResponse Player { get; set; }
    public string Token { get; set; }
    public string RefreshToken { get; set; }
}
Example:
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class EmailSignUp : MonoBehaviour
{
    private OpenfortSDK openfort;
    
    private async void Start()
    {
        openfort = await OpenfortSDK.Init(
            "YOUR_OPENFORT_PUBLISHABLE_KEY",
            "YOUR_SHIELD_PUBLISHABLE_KEY" // Optional
        );
    }
    
    public async UniTask<AuthResponse> CreateNewAccount(string email, string password, string displayName = null)
    {
        try
        {
            var response = await openfort.SignUpWithEmailPassword(email, password, displayName);
            
            Debug.Log(quot;User created successfully: {response.Player.Id}");
            Debug.Log(quot;Email: {GetUserEmail(response.Player)}");
            
            return response;
        }
        catch (OpenfortException e)
        {
            Debug.LogError(quot;Signup failed: {e.Message}, Type: {e.ErrorType}");
            throw;
        }
    }
    
    private string GetUserEmail(AuthPlayerResponse player)
    {
        foreach (var account in player.LinkedAccounts)
        {
            if (account.Provider == "email" && !string.IsNullOrEmpty(account.Email))
                return account.Email;
        }
        return "No email found";
    }
}

LogInWithEmailPassword

Authenticate an existing user with email and password.

Method Signature:
public async UniTask<AuthResponse> LogInWithEmailPassword(string email, string password)
Parameters:
  • string email - User's email address
  • string password - User's password
Returns:
  • UniTask<AuthResponse> - Authentication response containing user info and tokens
Example:
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class EmailLogin : MonoBehaviour
{
    private OpenfortSDK openfort;
    
    private async void Start()
    {
        openfort = await OpenfortSDK.Init(
            "YOUR_OPENFORT_PUBLISHABLE_KEY",
            "YOUR_SHIELD_PUBLISHABLE_KEY"
        );
    }
    
    public async UniTask<AuthResponse> LoginUser(string email, string password)
    {
        try
        {
            var response = await openfort.LogInWithEmailPassword(email, password);
            
            Debug.Log(quot;Login successful: {response.Player.Id}");
            
            // Access linked accounts
            foreach (var account in response.Player.LinkedAccounts)
            {
                Debug.Log(quot;Provider: {account.Provider}, Email: {account.Email}");
            }
            
            return response;
        }
        catch (OpenfortException e)
        {
            Debug.LogError(quot;Login failed: {e.Message}, Type: {e.ErrorType}");
            throw;
        }
    }
}

RequestEmailVerification

Send an email verification to the user.

Method Signature:
public async UniTask RequestEmailVerification(RequestEmailVerificationRequest request)
Parameters:
  • RequestEmailVerificationRequest request - Email verification request
Returns:
  • UniTask - Completes when verification email is sent
RequestEmailVerificationRequest Structure:
public class RequestEmailVerificationRequest
{
    public string Email { get; set; }
}
Example:
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class EmailVerification : MonoBehaviour
{
    private OpenfortSDK openfort;
    
    private async void Start()
    {
        openfort = await OpenfortSDK.Init(
            "YOUR_OPENFORT_PUBLISHABLE_KEY",
            "YOUR_SHIELD_PUBLISHABLE_KEY"
        );
    }
    
    public async UniTask SendVerificationEmail(string email)
    {
        try
        {
            var request = new RequestEmailVerificationRequest
            {
                Email = email
            };
            
            await openfort.RequestEmailVerification(request);
            Debug.Log(quot;Verification email sent to {email}");
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Failed to send verification email: {e.Message}");
            throw;
        }
    }
}

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..."
}

RequestResetPassword

Request a password reset email for the user.

Method Signature:
public async UniTask RequestResetPassword(ResetPasswordRequest request)
Parameters:
  • ResetPasswordRequest request - Password reset request containing email
Returns:
  • UniTask - Completes when reset email is sent
Example:
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class PasswordResetRequest : MonoBehaviour
{
    private OpenfortSDK openfort;
    
    private async void Start()
    {
        openfort = await OpenfortSDK.Init(
            "YOUR_OPENFORT_PUBLISHABLE_KEY",
            "YOUR_SHIELD_PUBLISHABLE_KEY"
        );
    }
    
    public async UniTask RequestPasswordReset(string email)
    {
        try
        {
            var request = new ResetPasswordRequest
            {
                Email = email
            };
            
            await openfort.RequestResetPassword(request);
            Debug.Log(quot;Password reset email sent to {email}");
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Failed to request password reset: {e.Message}");
            throw;
        }
    }
}

ResetPassword

Reset the user's password using the verification state from email.

Method Signature:
public async UniTask<AuthPlayerResponse> ResetPassword(ResetPasswordRequest request)
Parameters:
  • ResetPasswordRequest request - Complete reset request with email, password, and state
Returns:
  • UniTask<AuthPlayerResponse> - Updated user information
ResetPasswordRequest Structure:
public class ResetPasswordRequest
{
    public string Email { get; set; }
    public string Password { get; set; }  // New password
    public string State { get; set; }     // From verification email
}
Example:
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class PasswordReset : MonoBehaviour
{
    private OpenfortSDK openfort;
    
    private async void Start()
    {
        openfort = await OpenfortSDK.Init(
            "YOUR_OPENFORT_PUBLISHABLE_KEY",
            "YOUR_SHIELD_PUBLISHABLE_KEY"
        );
    }
    
    public async UniTask<AuthPlayerResponse> ResetUserPassword(string email, string newPassword, string verificationState)
    {
        try
        {
            var request = new ResetPasswordRequest
            {
                Email = email,
                Password = newPassword,
                State = verificationState  // From the reset email link
            };
            
            var response = await openfort.ResetPassword(request);
            Debug.Log(quot;Password reset successfully for user: {response.Id}");
            
            return response;
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Password reset failed: {e.Message}");
            throw;
        }
    }
}

Logout

Log out the current user and clear stored credentials.

Method Signature:
public async UniTask Logout()
Parameters:
  • None
Returns:
  • UniTask - Completes when logout is finished
Example:
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Openfort.OpenfortSDK;
 
public class UserLogout : MonoBehaviour
{
    private OpenfortSDK openfort;
    
    private async void Start()
    {
        openfort = await OpenfortSDK.Init(
            "YOUR_OPENFORT_PUBLISHABLE_KEY",
            "YOUR_SHIELD_PUBLISHABLE_KEY"
        );
    }
    
    public async UniTask LogoutUser()
    {
        try
        {
            await openfort.Logout();
            Debug.Log("User logged out successfully");
            
            // Navigate to login screen or perform cleanup
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Logout failed: {e.Message}");
        }
    }
}

UI integration example

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

using TMPro;
using UnityEngine.UI;
using Cysharp.Threading.Tasks;
 
public class AuthUIManager : MonoBehaviour
{
    [SerializeField] private TMP_InputField emailInput;
    [SerializeField] private TMP_InputField passwordInput;
    [SerializeField] private TMP_InputField nameInput; // Optional for signup
    [SerializeField] private Button signUpButton;
    [SerializeField] private Button loginButton;
    [SerializeField] private GameObject loadingIndicator;
    
    private OpenfortAuthManager authManager;
 
    private void Start()
    {
        authManager = GetComponent<OpenfortAuthManager>();
        
        signUpButton.onClick.AddListener(() => HandleSignUp().Forget());
        loginButton.onClick.AddListener(() => HandleLogin().Forget());
    }
 
    private async UniTaskVoid HandleSignUp()
    {
        SetUIEnabled(false);
        try
        {
            string name = string.IsNullOrEmpty(nameInput?.text) ? null : nameInput.text;
            await authManager.SignUpNewUser(emailInput.text, passwordInput.text, name);
            // Navigate to next scene or show success
        }
        catch (Exception e)
        {
            // Show error to user
            Debug.LogError(quot;Sign up failed: {e.Message}");
        }
        finally
        {
            SetUIEnabled(true);
        }
    }
 
    private async UniTaskVoid HandleLogin()
    {
        SetUIEnabled(false);
        try
        {
            var response = await authManager.LogInUser(emailInput.text, passwordInput.text);
            // Navigate to game scene
            Debug.Log(quot;Login successful for player: {response.Player.Id}");
        }
        catch (Exception e)
        {
            // Show error to user
            Debug.LogError(quot;Login failed: {e.Message}");
        }
        finally
        {
            SetUIEnabled(true);
        }
    }
 
    private void SetUIEnabled(bool enabled)
    {
        signUpButton.interactable = enabled;
        loginButton.interactable = enabled;
        emailInput.interactable = enabled;
        passwordInput.interactable = enabled;
        if (loadingIndicator != null)
            loadingIndicator.SetActive(!enabled);
    }
}