
Lesson Description
The "Managing User Data" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:
Melkey begins scaffolding the user store, defining user and password structures. The bcrypt package is added to the project for hashing passwords.
Transcript from the "Managing User Data" Lesson
[00:00:00]
>> Melkey: So we just wrapped up testing on a database layer. We're gonna quickly shift gears to user and authentication methods, and we're going to kind of explore how that's going to look for a Go back-end application. So the first thing is, this time, we actually start from the lowest level and move our way up.
[00:00:20]
So we're gonna create a user_store.go file in the store package we have up here.
>> Melkey: Okay, and in this package it's going to be responsible for everything to do with our users and the database. Now, like I've said before, every time you open a new file, you're gonna ask yourself, is it gonna hold data, right?
[00:00:43]
The answer is, yes, there's gonna be user data. How are gonna handle that user data? What are we gonna do with that user data? So with that, we can go ahead and create our very first user struct. So here we're to type user, there's going to be a struct, like so.
[00:00:57]
It's gonna have an ID of int, json:"id", right? It's gonna have our username string.
>> Melkey: Username, email.
>> Melkey: Email like so. We're gonna have, our next part is the password_hash. All right, now, password_hash isn't going to be a string, but I'm gonna leave it as a string right now, okay?
[00:01:29]
You'll see why in just a second. And then here we're actually going to omit anything in the JSON field. And what this dash does, basically says, no matter what we encode, decode, or write, we are going to ignore the value in this struct. Just so it completely voting passed from our end to the client end, okay?
[00:01:48]
Next, we're gonna have our bio to be a string, can do bio like so. And then we can add the other fields, as well as create at, which will be of a type time, .time. Okay, so import the time package, new JSON created at like this. You can go ahead and copy and paste this to make updated at.
[00:02:20]
>> Melkey: And another field we are very interested in is the Postgres struck, that will handle the database communication. So we can do type PostgresUserStore, in itself is going to be a struck, and this struck is going to have a field for sql.db, like so. Import DB.
>> Melkey: All right, and with our struct, let's make a new constructor.
[00:02:56]
So we can do func NewPostgresUserStore. Okay, this is gonna take one argument, which would be DB, pointer to SQL DB, and it's going to return a pointer to PostgresUserStore. I'm gonna return this, like this. I'm going to say, oops, not like that. Return PostgresUserStore, put db as db, all right?
[00:03:24]
So pretty fundamentally, the thing we've seen before, the structs, the constructors, and the Postgres struct as well. And with that, let's go ahead and create the interface. So type, we'll say, UserStore. It's going to be an interface. And this interface is gonna have three methods. One's going to be create user.
[00:03:49]
It's going to take a type of user to return with an error, and have GetUser by user name. It's gonna take a username as a string. It's gonna turn a pointer to user and an error. And last is gonna be UpdateUser(), okay? And this is going to take a pointer to a user and respond with an error.
[00:04:14]
Now, before we continue, I wanna bring in another package, it's gonna help us kind of, handle hashing the passwords. So here if you open up your terminal and you go get bcrypt, this is not from GitHub, so let's go get golang.org/x/crypto/bcrypt.
>> Melkey: Okay, should install without any problems.
[00:04:43]
[COUGH] Let's go ahead and bring in bcrypt into our project. So it's again, golang.org/x/crypto/bcrypt, okay? Input not used, this always a decent sign. And now we're going to actually handle the password, cuz right now it's a string, we don't want this. I actually want my password to be its own struct.
[00:05:09]
So here I'm gonna define password lower capitalization, so lowercase. It's gonna be a struct. It's gonna have a plain text. Okay, I should type it like this. It's gonna be a pointer to a string. And I'm gonna have the hash, which is going to be a slice of bytes.
[00:05:29]
>> Melkey: Okay, so then we can replace password hash from a string to a password type. So we're gonna do some nested structs here.
>> Melkey: I'm gonna leave this for right now. So we can actually go back, if the compiler is annoying you, put a little underscore there, cuz we will come back to bcrypt.
[00:05:45]
I just wanna put a pin on this for right now.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops