Custom Authentication - Anonymous Login

Hi Photonians

Some context of the problem description:
For our game, we currently have a multiplayer part that allows players to anonymously connect to Photon and play some rounds. This currently works very well.
In the features we're currently adding, we'd like to have some persistent identification of users across sessions (profile information, scores, ...). So we've build a back-end system with user registration and login. This is currently being tested and not live yet.

My question / problem:
Is it possible in Photon to use the custom authentication mechanism, where we have the option for users to login in with their account and benefit from the additional features, while users who don't wish to register / login can still play the parts of the game that don't require authentication?
If so, how do we need to go about player ID's being assigned for anonymous players and those that have an ID (which is just an auto-increment int) from our own service?
Any ideas on how to tackle this? Or maybe any example use-case?

So to emphasise: we wish to have an optional login, not a required login!

Thanks in advance!

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited February 2017
    Hi @juniordiscart,

    Thank you for choosing Photon!
    Your use case is interesting and is a common practice especially in mobile games.
    I guess you already found our documentation page about custom authentication.

    You can achieve this in two ways:
    1. Allow anonymous login for your Photon app from dashboard
    Use this if you can send the anonymous user's ID from client or if you are OK with GUIDs assigned by Photon Server. You can make use of the device's unique ID as userId or generate one on client.
    public void PhotonAnonymousAuthentication(string anonymousUserId = null)
        {
            AuthenticationValues authValues = new AuthenticationValues();
            authValues.AuthType = CustomAuthenticationType.None;
            authValues.UserId = anonymousUserId; // could be 1. null, 2. generated on client, 3. fetched from auth web server or 4. device ID
            // call connect method
        }
    2. Allow anonymous login for your Photon app from your web authentication provider
    return the anonymous user ID in the custom authentication response:
    {ResultCode:1, UserId: customGneratedAnonymousUserId, Nickname: optionalRandomlyGeneratedNickname}
    You can also specify if this is an anonymous authentication using a parameter in the custom auth HTTP request (query string parameter or in a query payload).
    public void PhotonCustomAuthentication(bool anonymousFlag, string userId = null)
        {
            AuthenticationValues authValues = new AuthenticationValues();
            authValues.AuthType = CustomAuthenticationType.Custom;
            authValues.UserId = userId; // could be 1. null then returned from auth web server or 2. set if used in the auth
            authValues.AddAuthParameter("anonymous", anonymousFlag.ToString()); // optional
            if (!anonymousFlag)
            {
                // add required auth params
            }
            // call connect method
        }
  • Hi JohnTube

    Thanks for your quick and helpful answer! This is exactly what I was looking for. :)
  • JohnTube
    JohnTube ✭✭✭✭✭
    You are welcome.

    I wanted to say that I recommend the 2nd approach as the first one implies that anyone who get your Photon AppId can login to your game.