Get an Authentication system up and running with Go(Golang) in less than 30secs

iqquee

Mathias Jiya

Posted on March 12, 2022

Get an Authentication system up and running with Go(Golang) in less than 30secs

Good afternoon fellow developers, this is my first article on Dev.to and I am so excited about it.
Today I want to show you an alternative to getting a user authentication (sign-up and sign-in) up and running in Go(Golang) with just 3 lines of code. Sounds impossible right ? yeah I know but at the end of this article you would see that it is true and it works. you can check out the package on github github.com/iqquee/auth

Now, lets get into the interesting stuffs. We would be capitalizing on a Go package I created which handles literally all the logic for a users sign-up and sign-in under the hood. The code base of the package is easy to read and understand as you can get to go through it. The good news is that you can have an authentication system up and running with just about 3 lines of code as we would see below. The database used here is mongodb.

Init your Go mod with

go mod init <your package name e.g github.com/<you username/<package name>>>
Enter fullscreen mode Exit fullscreen mode

Download the package using

go get github.com/iqquee/auth
Enter fullscreen mode Exit fullscreen mode

Create a .env file and add this to it

DATABASE_NAME=<database name>
USER_COL=<collection name>
PORT=<port number>
SECRET_KEY=<secret key>
MongoDB_URI=<mondodb uri>
Enter fullscreen mode Exit fullscreen mode

Download the Gin framework as we would be using it for routing with

go get github.com/gin-gonic/gin
Enter fullscreen mode Exit fullscreen mode

Create your main.go file

package main

import (
    "log"
    "os"

    "github.com/gin-gonic/gin"
    "github.com/iqquee/auth/auth"
    "github.com/joho/godotenv"
)

//func init runs before the main function
func init() {
    if err := godotenv.Load(); err != nil {
        log.Println("Error loading .env file")
    }
}
func main() {

    port := os.Getenv("PORT")
    if port == "" {
        port = "3000"
    }
    router := gin.Default()
    //user routes
    user := router.Group("user")
    {
        user.POST("/signup", auth.SignUp)
        user.POST("/signin", auth.SignIn)
    }
    router.Run(":" + port)
}
Enter fullscreen mode Exit fullscreen mode

To test the endpoints you would have to start the server with

go run main.go
Enter fullscreen mode Exit fullscreen mode

and then on postman hit the signup endpoint with

127.0.0.1/user/signup
Enter fullscreen mode Exit fullscreen mode

input the users information using the user model below

    First_Name    string             `json:"first_name"`
    Last_Name     string             `json:"last_name"`
    Email         string             `json:"email"`
    Phone_Number  int               `json:"phone_number"`
    Password      string             `json:"password"`
Enter fullscreen mode Exit fullscreen mode

after inputting the users details then you can proceed to hit the signup endpoint with a POST request. Now there you have it, the user has been signup successfully.

You can also test the users sign-in endpoint with

127.0.0.1/user/signin
Enter fullscreen mode Exit fullscreen mode

then input the just created user email and password using model below

    Email         string             `json:"email"`
    Password      string             `json:"password"`
Enter fullscreen mode Exit fullscreen mode

after inputting the users details then you can proceed to hit the signin endpoint with a POST request and the user has been signed in successfully.

Image description

It really is that easy and from what you can see above, you would realize that we only literally wrote just 3 lines of code which are :

"github.com/iqquee/auth/auth"
user.POST("/signup", auth.SignUp)
user.POST("/signin", auth.SignIn)
Enter fullscreen mode Exit fullscreen mode

the first imported the package and the other two only called the sign-up and sign-in function from the package and attaching it to the endpoint.
I was excited creating this Go package and I hope you find it helpful.
Contributions to this package to further improve it is very much welcome as its open source and so just anyone can contribute to it.

💖 💪 🙅 🚩
iqquee
Mathias Jiya

Posted on March 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related