AuthenticateWithPassword (Unreal Engine)

- Declaration

static void AuthenticateWithPassword(const FString& Server, const FString& Email, const FString& Password, CavrnusAuthRecv OnSuccess, CavrnusError OnFailure);

Delegates Options:

typedef TFunction<void(const FCavrnusAuthentication&)> CavrnusAuthRecv;

typedef TFunction<void(const FString&)> CavrnusError;

DECLARE_DYNAMIC_DELEGATE_OneParam(FCavrnusAuthRecv, FCavrnusAuthentication, Auth);

DECLARE_DYNAMIC_DELEGATE_OneParam(FCavrnusError, FString, Error);

- Description

Gets user credentials, allowing you to join valid spaces and make other requests.

OnFailure will generally trigger if the user's email or password is invalid, or if they are not connecting to a valid server.

OnSuccess provides a FCavrnusAuthentication (LINK) which you can then store/load on your local machine to use on a future run without requiring the user to re-login.

- Blueprint Sample

 

image-20240312-233637.png

- Code Sample

AuthenticationClass.h:

#include "Types/CavrnusAuthentication.h" #include "Types/CavrnusCallbackTypes.h" #include "AuthenticationClass.generated.h" UCLASS() class MODULE_API UAuthenticationClass { UAuthenticationClass(); void Authenticate(); void OnAuthSuccess(FCavrnusAuthentication Authentication); void OnAuthFailure(FString Error); private: FCavrnusAuthRecv AuthSuccess; FCavrnusError AuthFailure; UPROPERTY() FString ServerAddress; UPROPERTY() FString UserEmail; UPROPERTY() FString UserPassword; UPROPERTY() FCavrnusAuthentication Authentication; };

AuthenticationClass.cpp

#include "AuthenticationClass.h" #include "CavrnusFunctionLibrary.h" UAuthenticationClass::UAuthenticationClass() { AuthSuccess.BindUFunction(this, GET_FUNCTION_NAME_CHECKED(UAuthenticationClass, OnAuthSuccess)); AuthFailure.BindUFunction(this, GET_FUNCTION_NAME_CHECKED(UAuthenticationClass, OnAuthFailure)); } void UAuthenticationClass::Authenticate() { // Note that while sample projects may show these fields pulled from a CavrnusSpatialConnector instance, customers may want these values stored more securely. UCavrnusFunctionLibrary::AuthenticateWithPassword( ServerAddress, UserEmail, UserPassword, AuthSuccess, AuthFailure); } void UAuthenticationClass::OnAuthSuccess(FCavrnusAuthentication Auth) { Authentication = Auth; UE_LOG(LogTemp, Log, TEXT("Successfully authenticated")); } void UAuthenticationClass::OnAuthFailure(FString ErrorMessage) { Authentication = FCavrnusAuthentication(); UE_LOG(LogTemp, Error, TEXT("Failed to authenticate, error: %s"), *ErrorMessage); }