Authenticate JWT with Golang (P1)

Tram Ho

I. Introduction

  • I usually write articles to find out if the viblo already exists, if there is one I will not write it anymore, but in this series for you to have an overview, I will again write an article about authenticate jwt with golang.
  • This article I will introduce you to the base that I built by myself is also very delicious

II. Deployment

First, I will introduce the folder structure for you to easily imagine in advance and I will present from a-> z respectively.

Looks like laravel, right?

  • First, I will create a helper to customize the response

  • As everyone knows, I will run the command go run index.go to start the server so see what this file contains (I use the gin framework to implement this series)

As the code, you see this file will call the router and connect database,

  • Setup database in config

  • In the routes folder, create an index.go file

Here I create 2 APIs that are login , register for authenticate authentication. continue to see what authController sẽ xử lý gì nhé . As initialized, you can see that I have used the service , repository … so let’s see what these instances do in turn.

As in the routes I imported, in this controller I called the server , the repository to use. The first is to implement the Register function, the first thing is to validate, and I have created a misty validate file.

Next, after passing the validate, go to the processing step, here I check duplicate mail and then create an account.

Function to check duplicate mail

Now, let’s test it with the default port 8080. This is the case without pass validate

And this is the pass validate case

III. End

Since I write quite in detail both about the code and the project folder structure, it is quite long, so I will contribute login and crud for the next post.

Share the news now

Source : Viblo