Skip to content

bdfp/auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Auth

This handles authentication for your application.

Features

  • username-password based login and register
  • schema creation for user
  • jwt based token management

Usage

In your main function, call auth.Setup() as shown,

package main

import "github.com/bdfp/auth"

func main() {
    db, err := sql.Open("mysql", "root:morning_star@/diary")
	if err != nil {
		panic(err.Error())
	}
	defer db.Close()

	err = db.Ping()
	if err != nil {
		panic(err.Error())
	}
	log.Println("Database connection opened")

	// Set up auth
	go auth.Setup(db)
}

Database changes

the following user table is created in the passwed database

| user          |
| ------------- |
| id
| username      |
| password      |

API

This will also register login and register http handlers as described.Those will run on port 8484.

Path /login

  • Method POST
  • Request Object
    {
        "username": "your_username",
        "password": "your_password"
    }
  • Response Object
    {
        "token": "eiofgheriuygij wer",
        "user": {
            "username": "your_username"
        }
    }

Path /register

  • Method POST
  • Request Object
{
    "username": "your_username",
    "password": "your_password"
}
  • Response Object
{
	"message": "success"
}

Middleware

In order to secure any route with jwt, just use the auth.Secure(yourHandler) while registering your router Example:

func startHTTPServer(db *sql.DB) {
	router := httprouter.New()
	router.POST("/tasks", auth.Secure(myHandler))
	log.Fatal(http.ListenAndServe(":8484", router))
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages