Go & Vanilla JS: Fullstack Without Frameworks

User Account Handlers

Maximiliano Firtman
Independent Consultant
Go & Vanilla JS: Fullstack Without Frameworks

Lesson Description

The "User Account Handlers" Lesson is part of the full, Go & Vanilla JS: Fullstack Without Frameworks course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano explains the importance of handlers, including how they interact with HTTP requests, communicate with repositories, and manage data flow. He also demonstrates the process of creating account handlers and integrating them into the application alongside repositories.

Preview
Close

Transcript from the "User Account Handlers" Lesson

[00:00:00]
>> Maximiliano Firtman: So apart from repositories, remember repositories are talking SQL and models. Now we need to add, going back to our structure, the architecture we are here we have the model and the model repository, and of course, we have the table. Now we need the handlers. So the handlers are the one that are talking HTTP, okay?

[00:00:23]
So they can receive data from the client, they can talk to the repository and send the data back, those are the handlers. So for that, they are large, not as large as the repository, but they have some things there. So of course, I already have the handlers as well that g2 so we need to create account handler.

[00:00:52]
So right now we do have in the handlers, folder, movie handlers. So we need now to create account handlers, dot go. So here we have this, they're not so big, but it's still big enough, okay. So let me move them here, okay, no errors, which is pretty good.

[00:01:19]
So something that I started doing, typically, I'm creating a structure for the request, the data that I'm receiving from the client, and also for the data I'm sending back. Okay, because that's then we can easily Marshal and UN Marshal those into JSON. By the way, Marshall and non marshaling has to do with JSON conversion to and from that structure.

[00:01:44]
So, for example, for authentication, so logging, we have the email and the password that we are getting from the user. For registration, we have name, email and password, and the response is actually success, the bool and add an optional message. That's right now what we are responding later, maybe we will add something else, but for now, that will be good enough, and we use also this idea of creating a structure that will have the storage and the logger.

[00:02:17]
The storage is the previous data repository that we created. We have the same UTD function that we used before. Actually, we can even reuse the same one that we have from the other handlers. This is the one that is writing the JSON response with the header and everything that is needed.

[00:02:36]
This is handling errors, so if we have any errors that it's coming back from the data repository and doing things differently, okay? So, for example, if we have an error on authentication validation on the user already exists, or registration validation, so this is we couldn't register you. Let me do this.

[00:03:04]
So, we could register you, you already exist in the database, so I can register you. Or if there is any login problem, I'm just responding to the client with status on authorize. That's a probably well known 401. So when you are trying to access something on the web, and you see a 401 that means you don't have access to that unauthorized.

[00:03:31]
So we couldn't do that, okay. That's one option, there are other developers that are just always returning 200, and then they have an internal code. But by default, I mean, if it's not one of the well known errors, I'm just sending an internal server error. I mean, if the database is down, if the query couldn't be executed, whatever happens that is not one of the well known errors.

[00:04:01]
I will just say internal error, okay, and I'm sending 500. That is, I don't know what happened, so okay. Then we have the handlers one by one for registration. The only thing that the handlers need to do is they need to decode the body. So we are going to send from the client, that we haven't done this so far, we need to send from the client data in the body in the form of json.

[00:04:33]
And then in the backend we need to receive that json, decode the json into our option, in this case, is the register request that actually is this structure. So the JSON should have name, email, password, and this is going to populate an instance of this value, or a value of this structure with name, email and password, with capital letters, okay.

[00:05:02]
That's make sense? So we populate this we were here on register, and then we call our storage and see what happens. And based on if there is an error or not, we are just sending the response back to the writer that is actually their HTTP response that goes back to the user.

[00:05:24]
Okay, does it make sense? Login is something similar actually, we are getting the data, also working with the body, and sending that to our storage or the entity, okay, and that's all we have. So those are the hamsters, any question? On the process or everything that needs to be done here.

[00:05:52]
It's not working yet because we need to register these hundreds server side. And of course we need to then call them from the client, okay, but, yeah, let's go step by step. So now, we need to go to main.go,
>> Maximiliano Firtman: Main.go, and at some point, we were creating the movie repository, so we need to do something similar for the account repository, okay?

[00:06:21]
So we need to initialize the account repository for users. So we are going to create another variable, account repo, with the error we call data new account repository. It needs a database and a log instance, it's similar to the previous one. And if we do have an error, we will do fatal, low, oops, that fatal with format failed.

[00:06:54]
Actually, we're not sending any value, so any format failed to initialize. This is the account rep repository, and this is the movie repository, I know I have two parentheses there. Now it's completely because I'm not using it, which is okay. Because now I have the movie handler, I need to create the account handler and say it's gonna be handlers.new account handler.

[00:07:23]
And I'm going to patch the repo and the log in, okay, like that. So I'm now creating instances of everything I have done. So we have the data repository, we have the handlers, and then I need to register the URLs. Dot load, so handle func, and we will have API account register, and that goes to the account handler, dot register, I wanna call that handler.

[00:08:01]
Handlefunk API account, it can be login, or it can be authentication or authenticate or whatever, just try to keep consistent with this, like so.

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