User activation
At the moment, a user can register for an account with our Greenlight API, but we don’t know for sure that the email address they provided during registration actually belongs to them.
So, in this section of the book, we’re going to confirm that a user used their own, real, email address by including ‘account activation’ instructions in their welcome email.
There are several reasons for having an account activation step, but the main benefits are that it adds an additional hoop for bots to jump through, and helps prevent abuse by people who register with a fake email address or one that doesn’t belong to them.
To give you an overview upfront, the account activation process will work like this:
- As part of the registration process for a new user we will create a cryptographically-secure random activation token that is impossible to guess.
- We will then store a hash of this activation token in a new
tokenstable, alongside the new user’s ID and an expiry time for the token. - We will send the original (unhashed) activation token to the user in their welcome email.
- The user subsequently submits their token to a new
PUT /v1/users/activatedendpoint. - If the hash of the token exists in the
tokenstable and hasn’t expired, then we’ll set theactivatedstatus for the relevant user totrue. - Lastly, we’ll delete the activation token from our
tokenstable so that it cannot be used again.
In this section of the book, you’ll learn how to:
Implement a secure ‘account activation’ workflow which verifies a new user’s email address.
Generate cryptographically-secure random tokens using Go’s
crypto/randpackage.Generate fast hashes of data using the
crypto/sha256package.Implement patterns for working with cross-table relationships in your database, including setting up foreign keys and retrieving related data via SQL
JOINqueries.