Loging in to a gues...
 
Notifications
Clear all

Loging in to a guest account does not work

6 Posts
2 Users
0 Reactions
398 Views
(@bennik)
Posts: 7
Active Member
Topic starter
 

I use this code to autologin or if that is not available create a guest account:

CombuManager.instance.rememberCredentials = true;
string storedUsername, storedPassword;
if (User.CanAutoLogin(out storedUsername, out storedPassword)) {
	Debug.Log ("Try to auto-login with username '" + storedUsername + "' and password '" + storedPassword + "'");
	User.AutoLogin ((bool loginSuccess, string loginError) => {
		Debug.Log ("AutoLogin: " + loginSuccess + " -- " + loginError);
		if (loginSuccess) {
			Debug.Log("Logged in");
		} else {
			// If autologin fails then create a guest account
			var user = new User ();
			user.CreateGuest ((bool success, string error) => {
				if (success) {
					Debug.Log ("CreateGuest - Success=" + success + " > Error=" + error);
				}
			});
		}
		
	});
}

However every time I restart my unity scene it creates a new guest account again.

As far as I can tell from the combu code for guests there is no password stored on the server.
And therefore autologin will not work.

My current workaround is to store a random password for the guest user after creating it.

private string randomString(int length)
{
	var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var stringChars = new char[length];
	var random = new Random();

	for (int i = 0; i < stringChars.Length; i++)
	{
		stringChars[i] = chars[random.Next(chars.Length)];
	}

	var finalString = new String(stringChars);
	return finalString;
}
	
CombuUser user = new CombuUser ();	
user.CreateGuest ((bool success, string error) => {
	if (success) {
		Debug.Log ("CreateGuest - Success=" + success + " > Error=" + error);
		Debug.Log("guest " + user.userName + " " + user.password);		

		string guestPass = randomString(8);
		Debug.Log("set pass for guest: " + guestPass);
		user.ChangePassword(guestPass, (bool success, string error) => {
			Debug.Log("ChangePassword " + success + " " + error);
			if (success) {
				PlayerPrefs.SetString("LoginPassword", guestPass);
				Debug.Log("Saved guest status");
			}
		});
	}
});	
 
Posted : 11/02/2021 9:04 am
(@skaredcreations)
Posts: 805
Prominent Member Admin
 

Hi,

that is correct because allowing the login of guests without a password would be a security hole if another user knows the guest's username. I see that I forgot to update the stored credentials in the callback of ChangePassword so I'll add it there for the next update.

About setting the password for guest users, you could try a small trick: edit the script User.cs in Unity and replace the method CreateGuest with the following code

public virtual void CreateGuest (Action<bool, string> callback)
{
    if (!CombuManager.isInitialized)
        throw ExceptionCombuNotInitialized;
    var form = CombuManager.instance.CreateForm ();
    form.AddField ("action", "create_guest");
    form.AddField ("Password", password);
    DoAuthenticate (form, (bool success, string error) => {
        if (success)
            StoreUserCredentials(CombuManager.localUser.userName, password);
        if (callback != null)
            callback(success, error);
    });
}

If it works (it should accordingly to the server code), you'll be able to set the password on the User object before calling CreateGuest and so you avoid the call to ChangePassword.

FRANCESCO CROCETTI @ SKARED CREATIONS

 
Posted : 11/02/2021 10:35 am
(@bennik)
Posts: 7
Active Member
Topic starter
 

I tried your solution but it doesnt work.
The server doesnt return a password for the guest nor is there a password stored in the mysql database.
Therefore when I try autologin later (after I created a guest account) it doesn't work.

It is fine for me if you say autologin should not work for guests (I will then continue to use my solution that creates a pass for guests too).
But then the help should clearly state that.

 
Posted : 11/02/2021 4:46 pm
(@bennik)
Posts: 7
Active Member
Topic starter
 

Or did you mean alter the CreateGuest funtion like this?

public virtual void CreateGuest (string password, Action<bool, string> callback)
{
    if (!CombuManager.isInitialized)
        throw ExceptionCombuNotInitialized;
    var form = CombuManager.instance.CreateForm ();
    form.AddField ("action", "create_guest");
    form.AddField ("Password", password);
    DoAuthenticate (form, (bool success, string error) => {
        if (success)
            StoreUserCredentials(CombuManager.localUser.userName, password);
        if (callback != null)
            callback(success, error);
    });
}

And then I call it using my own random password?

 

 
Posted : 11/02/2021 4:54 pm
(@skaredcreations)
Posts: 805
Prominent Member Admin
 

It may be that you misunderstood my answer, the code that I provided in the previous post was only meant as workaround to avoid the call to ChangePassword but it still required you to create a random password (to let Autologin work with empty-password guests I'll need to make a change, that is changing the login webservice action to check the error for empty password only on accounts that have a password set in the database, I'll do this in the next update).

So to use the code change above you should call it like this (following your sample code):

CombuUser user = new CombuUser ();
user.password = randomString(8);
user.CreateGuest ((bool success, string error) => {
	if (success) {
		Debug.Log ("CreateGuest - Success=" + success + " > Error=" + error);
		Debug.Log("guest " + user.userName + " " + user.password);		
	}
});

FRANCESCO CROCETTI @ SKARED CREATIONS

 
Posted : 11/02/2021 4:56 pm
(@bennik)
Posts: 7
Active Member
Topic starter
 

Yes, you are right!
Thanks for the fast responses.

 
Posted : 11/02/2021 5:02 pm
Share: