question

gage-1 avatar image
gage-1 asked

JS and HTML | SendEmailFromTemplate

Essentially i just need to call the send email from template from the ServerApi via HTML. I have some code already for login, register and account recovery, however for some reason any server call i make wont even post an error response via the callback. I read in the sample HTML file that server calls require a DeveloperSecretKey but i dont know where to put it nor how to call it or use it in my case. Any help is appreciated.

HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>The Arena Authentication</title>
<script type="text/javascript" src="https://download.playfab.com/PlayFabClientApi.js"></script>;
<script type="text/javascript" src="https://download.playfab.com/PlayFabServerApi.js"></script>;
<script type="text/javascript" src="TheArenaAuthentication.js"></script>
</head>
<body>
<strong>Registration</strong><br />
Username: <input type="text" id="registerUsername" value=""><br />
Password: <input type="password" id="registerPassword" value=""><br />
Email: <input type="text" id="registerEmail" value=""><br />
<input type="button" value="Submit" onclick="RegisterAccount()"><br />
Result:<br />
<textarea id="registrationResult" cols="60" rows="5"></textarea><br />
<br>
<br>
<strong>Login</strong><br />
Username: <input type="text" id="loginUsername" value=""><br />
Password: <input type="password" id="loginPassword" value=""><br />
<input type="button" value="Submit" onclick="LoginAccount()"><br />
Result:<br />
<textarea id="loginResult" cols="60" rows="5"></textarea><br />
<br>
<br>
<strong>Resend Contact Email</strong><br />
<input type="button" value="Submit" onclick="ResendContactEmail()"><br />
Result:<br />
<textarea id="resendContactEmailResult" cols="60" rows="5"></textarea><br />
<br>
<br>
<strong>Reset Password</strong><br />
Email: <input type="text" id="resetPasswordEmail" value=""><br />
<input type="button" value="Submit" onclick="ResetPassword()"><br />
Result:<br />
<textarea id="resetPasswordResult" cols="60" rows="5"></textarea><br />
</body>
</html>





JAVASCRIPT

var _titleId = "3BC0";
var _emailTemplateId = "8A99F61CE507FDB2";
var _playFabId = "null";

function RegisterAccount(){
var request = {
TitleId: _titleId,
Username: document.getElementById("registerUsername").value,
Password: document.getElementById("registerPassword").value,
Email: document.getElementById("registerEmail").value
};
PlayFabClientSDK.RegisterPlayFabUser(request, RegisterCallback);
}

var RegisterCallback = function (result, error) {
if (result !== null) {
document.getElementById("registrationResult").innerHTML = "Registration successful";
} else if (error !== null) {
document.getElementById("registrationResult").innerHTML =
"Registration failure.\n" +
"Info: " + PlayFab.GenerateErrorReport(error);
}
}

function LoginAccount(){
var request = {
TitleId: _titleId,
Username: document.getElementById("loginUsername").value,
Password: document.getElementById("loginPassword").value
};
PlayFabClientSDK.LoginWithPlayFab(request, LoginCallback);
}

var LoginCallback = function (result, error) {
if (result !== null) {
_playFabId = result.data.PlayFabId;
document.getElementById("loginResult").innerHTML = "Login successful\n" + "User '" + _playFabId + "' active";
} else if (error !== null) {
document.getElementById("loginResult").innerHTML =
"Login failure.\n" +
"Info: " + PlayFab.GenerateErrorReport(error);
}
}

function ResendContactEmail(){
var request = {
EmailTemplateId: _emailTemplateId,
PlayFabId: _playFabId
};
PlayFabServerSDK.SendEmailFromTemplate(request, ResendContactEmailCallback);
}

var ResendContactEmailCallback = function (result, error) {
if (result !== null) {
document.getElementById("resendContactEmailResult").innerHTML = "Resend Contact Email successful";
} else if (error !== null) {
document.getElementById("resendContactEmailResult").innerHTML =
"Resend Contact Email failure.\n" +
"Info: " + PlayFab.GenerateErrorReport(error);
}
}

function ResetPassword(){
var request = {
TitleId: _titleId,
Email: document.getElementById("resetPasswordEmail").value
};
PlayFabClientSDK.SendAccountRecoveryEmail(request, ResetPasswordCallback);
}

var ResetPasswordCallback = function (result, error) {
if (result !== null) {
document.getElementById("resetPasswordResult").innerHTML = "Reset Password successful";
} else if (error !== null) {
document.getElementById("resetPasswordResult").innerHTML =
"Reset Password failure.\n" +
"Info: " + PlayFab.GenerateErrorReport(error);
}
}

apissdks
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Seth Du avatar image
Seth Du answered

A quick fix for your issue is to implement your functions ( where server APIs are involved) in Cloud Script and then call client API ExecuteCloudScript instead.

In normal circumstance, we do no suggest using Server API in any client, because it may have safety concerns. Hence, make use of Cloud Script will be a better choice.

6 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

gage-1 avatar image gage-1 commented ·

@SethDu Thank you for that advice, however i still would like to know how to do this normally, for the case of knowing.

0 Likes 0 ·
Andy avatar image Andy ♦♦ gage-1 commented ·

To make a server call without using one of our SDKs, you need to add an X-SecretKey header and add the value of a secret key configured for your title. That should be visible in the sample requests in all of our service API reference docs. (example: https://api.playfab.com/documentation/server/method/GetPlayerProfile)

There's more info abotu secret keys here: https://api.playfab.com/docs/tutorials/devsecret

0 Likes 0 ·
gage-1 avatar image gage-1 Andy ♦♦ commented ·

Hi, thanks for the response, i dont see a place or know a place to put the "header". I keep seeing it in the code example and in the docs but in the code you see above i linked, i dont know where to put it. Does it go in the JS file or the HTML file, if so where exactly do i put it?

I see the quote below, but i dont know plysically where it goes not the syntax or where itll go.

X-SecretKey: <developer_secret_key
0 Likes 0 ·
Show more comments
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.