In almost every game, a player needs an identity. They assume this identity and can access resources they have access to.
This is done by authentication.
For this tutorial, we're going to have a very simple authentication process. For other purposes, this might not be ideal and proper auth services should be utilized like Auth0, Firebase or a custom backend.
Interfacing Unity with these services is a tutorial in itself so for this, we're going to use a simple GraphQL service I created and hosted on Glitch
You'll clone this service and add in your own project secrets!
The service has one Query,
GetJWT(id: Int!, username: String, password: String) which takes in a users
password, cross checks it with our Hasura backend and returns a signed
We shall then use this token in all our subsequent requests to our Hasura backend. Without a valid token, our Hasura backend will not return any data.
The whole authentication process goes thus:
For a new user
- User starts the game and Unity queries
GetJWTwith a mock user's id, username and password.
- The token returned is set as the
Authenticationheader for all our further requests,
- Before being allowed to play online, User is asked to create a profile.
- User provides their preferred username.
- A random password is generated and the username and password are used to create a new entry in our
userstable in our Hasura backend.
idof the newly created user is returned and the
passwordare sent to
GetJWTto get a new token for this new user.
passwordare also stored in
- The new token is set as our new
Authenticationheader for subsequent requests.
For a returning user
- We check
passwordof the user.
- We send these to
GetJWTand get a jwt returned. = The token is set as our
Authentticationheader for subsequent requests.
Of course, this method of authentication has some problems but it is ideal for this tutorial.
You can learn a bit more about Authentication with Hasura