Users & Authentication
Subject responses are associated with a username. This is either a randomly-generated string, or it’s based off an auth0 userID. The value of the username is maintained in the redux store as UserID
.
The userID is set by a redux saga:
Note that the action needs to be passed isAuthenticated
and user
, both of which come from the AuthProvidor (see below). For convenience, this saga is triggered every time the Header is loaded (which is on every page):
Note that userEffect
is a React lifecycle hook for functional components. It gets triggered when the component is loaded, as well as any time the values of isAuthenticated, props.userID
or user
change.
In principle, this saga could be triggered elsewhere. One thing to keep track of is that because it is asynchronous, components (including the header) may load before the user has been set.
Generating UserIDs
Randomly-generated string
If authenticated with auth0 is not enabled, then the value of props.userID
comes from a cookie. For that code, see front-end/src/utils/session.js
. The purpose of the cookie is to enable the userID to persist across browser refreshes (browser refresh re-initializes the Redux store.) Maximum life of the cookie is 2 days. (We aren’t in the business of tracking people without opt-in consent.)
Note that if authentication is not enabled, then the value of isAuthenticated
will always be false
. (See the variable definitions in the Header component.)
Note that the action triggering the creation (or checking) of the cookie (getSessionUser()
) is handled by a Redux Saga (see front-end/src/sagas
).
Auth0
If authentication is enabled, then userIDs can be supplied by auth0. We use code from the auth0 SPA quickstart, which is found in front-end/src/utils/react-auth0-spa.js
. This code provides a component that wraps the entire application in front-end/src/index.js
:
As you can see, it looks a lot like the Redux provider. It works similarly: children get access to a few useful variables and methods. You can see this in react-auth0-spa.js
:
You will notice user
. By default, the value of user
is the username from whatever social media application (etc.) the user used to authenticate. This is often the user’s actual name, which we don’t want. So the auth0 quickstart code was modified to retrieve the auth0 numerical ID instead, which is presumably (??) unique to your application. We then immediately run this through a salted hash.
Why? It makes it harder to link data in a Pushkin database to a person. That is, someone who had access to your database and to your auth0 account still wouldn’t be able to match the user data to an individual in auth0. They would also need the salt string, which is stored separately. Even if they had it, it would be a pain to use, because you can’t easily decrypt something that has been hashed. The only option would be to encrypt every auth0 ID and then see what in the database matches.
They key code is in utils/react-auth0-spa.js
. First, we define a helper function:
There are several different places where userIDs are retrieved. In each case, we encrypt:
Finally, note that when users log out of auth0, the userID
is set to null
:
This will trigger the assignment of a new userID
via the cookie method.
Using UserIDs
userID
is automatically available to any component that is connected to the Redux store. This unfortunately does not include the quizzes themselves. Instead, the TakeQuiz component passes the entire Redux store as a prop:
(Note that TakeQuiz is a connected component, so its props include the entire Redux store.)
Most of the methods for Pushkin-Client expect to be explicitly sent the userID. This can be done from within the QuizComponent. For example:
The exception is any Pushkin Client method that is called directly by jsPsych’s onFinish
function. This is because this function takes a single argument, which is trial data. Here is an example:
You will see that this expects user_id
to be passed as part of the data. The easiest way to make this happen is to use jsPsych’s handy data.addProperties method. Here is a code snippet from one of the template experiments:
Last updated