Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Fixing the login

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, let's go try to log in. Actually, before we even try that let me do a quick cleanup here. Remember we put those users in
0:08 and they don't have things like hashed passwords and so on and that's going to cause some trouble. So let me just go over here
0:15 and we can just say remove all documents. The only way we really want users in here is they go and register. So let's try that.
0:22 Let's go over here and try to register. We'll put our name, our email, our password and then I guess we still have this around here.
0:30 I'm not really 57, but whatever. Oh, that's a problem. What's going on here? So, apparently zero is not a valid ObjectId.
0:39 Where is this error happening? Way, way down here on this query right there. So when we're trying to find the user we're passing in an integer
0:51 and it's expecting an ObjectId. Well, why is that? What is happening here? It turns out, it has to do with the fact that we're using a cookie
1:00 for the user_id, remember that? What we're putting in here is an integer and what it needs to be is an ObjectId. So I'll say bson comes from a Mongo
1:08 PyMongo, technically. We want an ObjectId like that. This is what we want this to return and it doesn't yet, but it's going to in a moment.
1:19 One of these, I'm going to come down here and the part that's problematic is this. So let's just say try to do this else. Return. And then we have it.
1:33 That should actually do it. So we should be able to come over here rerun our app. Here notice the login actually worked
1:41 and it tried to redirect us to our account and now, guess what, our account works. We logged in, this is great.
1:48 Let's log out and just show that we can login again. We went through the register before. But look; login, logout. Works beautifully.
1:57 So we had to make that little tiny change because we're now using a bson.ObjectId as the primary key. We want to store that
2:05 instead of just using an integer before. Okay, so this little minor change was just all we needed to do to sort of sync up with the data types
2:14 and I guess we could have gotten around it but it's really better to leverage MongoDB's ObjectId for the ObjectId's if you don't care.
2:24 But if they're auto generated, go with this. This is nice.

Talk Python's Mastodon Michael Kennedy's Mastodon