Email and Password
Allow users to sign in with a password connected to their email address in your Unity game.
You can update the server sending email notifications and the email templates through your dashboard. Visit the guide on how to update password authentication to learn more.
Setting up authentication#
First, create a manager class to handle Openfort authentication:
_16using Openfort.OpenfortSDK;_16using Openfort.OpenfortSDK.Model;_16_16public class OpenfortAuthManager : MonoBehaviour _16{_16 private OpenfortSDK openfort;_16 _16 private async void Start()_16 {_16 if (OpenfortSDK.Instance != null)_16 {_16 openfort = OpenfortSDK.Instance;_16 }_16 openfort = await OpenfortSDK.Init("YOUR_OPENFORT_PUBLISHABLE_KEY");_16 }_16}
Sign up a new user#
To register a new user with email and password:
_31public class OpenfortAuthManager : MonoBehaviour _31{_31 // ... previous code ..._31_31 public async Task SignUpNewUser(string email, string password)_31 {_31 try _31 {_31 await openfort.SignUpWithEmailPassword(email, password);_31 Debug.Log("User signed up successfully");_31 }_31 catch (Exception e)_31 {_31 Debug.LogError($"Error signing up user: {e.Message}");_31 }_31 }_31_31 // Optional: Send email verification_31 public async Task RequestEmailVerification(string email)_31 {_31 try _31 {_31 await openfort.RequestEmailVerification(email);_31 Debug.Log("Verification email sent");_31 }_31 catch (Exception e)_31 {_31 Debug.LogError($"Error sending verification email: {e.Message}");_31 }_31 }_31}
Log in a user#
To authenticate an existing user:
_24public class OpenfortAuthManager : MonoBehaviour _24{_24 // ... previous code ..._24_24 public async Task LogInUser(string email, string password)_24 {_24 try _24 {_24 var response = await openfort.LogInWithEmailPassword(email, password);_24 Debug.Log("User logged in successfully");_24 _24 // The response contains:_24 // - response.player: Player information_24 // - response.token: Authentication token_24 // - response.refreshToken: Token for refreshing authentication_24 _24 // Store these tokens as needed for your game_24 }_24 catch (Exception e)_24 {_24 Debug.LogError($"Error logging in: {e.Message}");_24 }_24 }_24}
Authentication response#
Upon successful authentication, you'll receive a response containing:
_17{_17 "player": {_17 "id": "pla_cc9ed2b7-c5f5-4c43-8dca-c4b104ba1762",_17 "object": "player",_17 "createdAt": 1710976453,_17 "linkedAccounts": [_17 {_17 "provider": "email",_17 "disabled": false,_17 "verified": true,_17 "email": "hello@example.com"_17 }_17 ]_17 },_17 "token": "eyJhbGci...",_17 "refreshToken": "eyJhbGci..."_17}
Log out#
To log out a user:
_10await openfort.Logout();
Password reset flow#
For implementing a password reset flow in your Unity game:
_30public class OpenfortAuthManager : MonoBehaviour _30{_30 // ... previous code ..._30_30 public async Task RequestPasswordReset(string email)_30 {_30 try _30 {_30 await openfort.RequestResetPassword(email);_30 Debug.Log("Password reset email sent");_30 }_30 catch (Exception e)_30 {_30 Debug.LogError($"Error requesting password reset: {e.Message}");_30 }_30 }_30_30 public async Task ResetPassword(string email, string newPassword, string verificationState)_30 {_30 try _30 {_30 await openfort.ResetPassword(email, newPassword, verificationState);_30 Debug.Log("Password reset successfully");_30 }_30 catch (Exception e)_30 {_30 Debug.LogError($"Error resetting password: {e.Message}");_30 }_30 }_30}
UI integration example#
Here's a basic example of how to integrate this with Unity UI:
_27public class AuthUIManager : MonoBehaviour_27{_27 [SerializeField] private TMP_InputField emailInput;_27 [SerializeField] private TMP_InputField passwordInput;_27 [SerializeField] private Button signUpButton;_27 [SerializeField] private Button loginButton;_27 _27 private OpenfortAuthManager authManager;_27_27 private void Start()_27 {_27 authManager = GetComponent<OpenfortAuthManager>();_27 _27 signUpButton.onClick.AddListener(HandleSignUp);_27 loginButton.onClick.AddListener(HandleLogin);_27 }_27_27 private async void HandleSignUp()_27 {_27 await authManager.SignUpNewUser(emailInput.text, passwordInput.text);_27 }_27_27 private async void HandleLogin()_27 {_27 await authManager.LogInUser(emailInput.text, passwordInput.text);_27 }_27}