Check out a free preview of the full Build Go Apps That Scale on AWS course

The "Logging In Users" Lesson is part of the full, Build Go Apps That Scale on AWS course featured in this preview video. Here's what you'd learn in this lesson:

Melkey begins adding the ability to log in users to the applications. First, a GetUser function is created to return a user from the database.


Transcript from the "Logging In Users" Lesson

>> Now I'm not gonna deploy this just yet, we always say that at the end, got a little treat. Everything works well, and if it doesn't, that's where I sweat and panic, but we're gonna pretend like that's not gonna happen. Next thing we're gonna do is we have the hash, we have the CDK.

I think we should define the function that's gonna interact with our database on logging in the user, so getting that user, retrieving that user. Because like I said, I like working from the lowest level all the way up. It just makes it easier for me to think about it in a weird way.

And also, there's a fun little gotcha that we're gonna showcase here, cool. So we have the DoesUserExist, this is where also, if you remember what I said, we could have made the first function essentially return this. You could still do that. Honestly, if you want, go ahead and do that.

I'm gonna keep it separate and it's gonna be a lot of repeating code. Please, clean code enthusiasts, I apologize, sincerely, sincerely, sincerely. But this is just for me to kind of showcase how we can defined functions under this method, call them. But of course, there's a way to make it better.

So here we're gonna do a func u, it's gonna be our DynamoDBClient and it's gonna be called GetUser. What GetUser is going to take is a username, string. And what is it gonna return? It's gonna return type.user and an error, right? Cuz we're no longer gonna be handling those registered users.

So we have this, and just like the code we wrote above, it's gonna be very similar. We're gonna call our database store, we're gonna get the item. We're gonna see if it exists if it's no, whatever. And then we're gonna go unmarshal it to our user struct. So I'm gonna go into what that really means, okay?

So here let's go ahead, first declare or instantiate a user var. So var user is gonna be types user and that's it. So remember now it's a nil value, because now it's a zero value of our struct, okay? So now, we can go ahead and basically retrieve the item.

So we can do result error is going to be u.databaseStore.Getitem, okay? And this get item is DynamoDB.GetItemInput. First field, let's put in our table name just to get that out of the way, aws.String(TABLE_NAME). The second value is gonna be a key, could be map(string). And it's going to be pointer to dynamodb.attribute value.

All right, I'm gonna assemble the exact same thing as we did before. I'm gonna assemble this key as a string, where we're gonna pass in that username as the main field that we're trying to retrieve from. So here we're gonna put username, struct string aws.String and put that username that we get as the argument in GetUser.

All right, and here I'm gonna do if the error does not equal to nil. What we're gonna do is return that user that we have defined right above here. Okay, we're gonna return that and the error, right? Then we're gonna check if result.item is nil. This is basically saying, this user does not exist, okay?

So we're gonna again return the user. I'm gonna do format.Errorf, I'm gonna say user not found. And this is where the difference comes from the previous DoesUserExists function. We're gonna create something called a Dynamo DB attribute to unmarshal the value that's stored in our Dynamo DB to the struct we have created above here, to our user struct.

So we need to modify our import, I'm gonna paste this. And at the end, it's gonna be, what is it? dynamodbattribute, dynamodbattribute. Go all the way down. We're basically gonna say error is going to be that attribute value, so dynamodbattribute's already populating for us. dynamodbattribute, I'm gonna do unmarshall.

And it's not gonna just a regular unmarshall, it's gonna be an unmartial map. I'm gonna take the result, so result.item, I'm gonna unmarshal it into our var. Or not var sorry, our user. Okay, and then we can lastly do if error does not, oops does not equal to nil, return user and that error, otherwise, let's return user.

So it's basically the exact same function that we wrote for DoesUserExist, but with this added unmarshaling. I'm gonna go into details of what unmarshaling truly means in just a little bit.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now