Enabling the Creation of Temporary Lists with Guest Users
02 Apr 2020
The creation of user accounts invites a new question: what do we do if someone does not want to make an account? While I could fairly easily arrange it so that a user must make an account to use the app, I think it would be better to allow the creation of temporary accounts that allow the creation of just one list. In order to save the list or to create additional lists, the user would have to register, which would upgrade the account into a fully-fledged one. While logged on as a guest user, certain pages and features would not be available.
I decided to implement this by making use of my User model, and adding a new line to it to check if an account was “temporary” or not:
Designating this as default=False ensures that, unless notified otherwise, the model will make all accounts “real.” In order to initialize a guest user, I created a new function in my constructors.py folder, which assigns random strings to the username, password, and email (since they are all required for an account).
With this created, it was time to add this functionality to the list. I wanted the guest account to be automatically generated if someone tried to create a list when they weren’t logged on. To do so, I added a check in my list creation function to determine if a user was logged on. If they were not, it generated a new user and assigned the list to this guest user, like so:
Also, as a manner of debugging and to see if everything works, I created a small note at the bottom of my layout.html to see who is logged in:
Here you can see an example of a guest account, using the same secrets.token.token_urlsafe() function as the rest of my identifiers in this app:
Now that guest lists existed, it was time to show the difference between them and a regular account. I rewrote parts of the navbar to hide the “My Lists” dropdown and the “Create List” button, and added a “Welcome” line that would refer to the guest as “Guest” if the account was temporary. I also did a bit of work to justify some of the navbar contents to the right.
A bit later, I went back and added a single link to the guest’s account, since I realized that if a different page was clicked, there wouldn’t be any way back to the list. I also changed the “Logout” button to a “Register” button:
This resulted in the following navbar for the guest user:
Now, it was time to add the ability to upgrade a guest account to a regular one. I first added a redirect from the homepage to the registration page if the user was a temporary user. Then, on the registration page, I added a check after the user validated the form, which checked if the user was creating an account or was already logged in as a guest. If the user was logged in as a guest, then the username, password, and email of the already existing guest account were changed, rather than a new user being created. This way, I didn’t have to migrate the list to a new account.
One nice aspect of forcing usernames and emails to be unique is that I can keep most of my try/except code that already existed in the registration route:
Next, I added a quick <div> to the list page to show guests that their list isn’t permanent, and encourage them to register an account to save the list.
That’s all very well and good, but there’s still one area left: what about deleting temporary accounts? I’ve thought about this, and I’m not entirely sure I want to. Obviously if space becomes an issue I may change my mind, but the random username and password makes it pretty hard for anyone to get back into a guest account after it’s made, and I might want to implement a way for a guest to retrieve a list in the future. If I change my mind, implementing it should be pretty easy; just return and delete all accounts with temporary set to true, and delete all lists associated with them. But for now, the temporary accounts can stay in.
Next Steps
“settings” page (with ability to request password reset/change info)
stricter requirements for passwords (min 8 characters, etc.)
make sure that a user can only edit their own lists