Code Monkey home page Code Monkey logo

gin-basicauth's Introduction

gin-basicauth

Gin web frameword's basic authorization middleware.Support Custom User Model Based on ORM Framework Gorm.

Usage

See the source code for specific usage.

define user model

The user model must define the methods of interface type IBasicAuth.

// IBasicAuth basic auth interface
type IBasicAuth interface {
	IsActived() bool
	CheckPassword(string) bool
	UsernameColumnName() string
}

// UserProfile model
type UserProfile struct {
	ID          uint      `gorm:"primary_key" json:"id"`
	Username    string    `gorm:"type:varchar(150);unique_index:uidx_name" json:"username,omitempty"`
	Password    string    `gorm:"type:varchar(128);default:'666'"`
	IsSuperUser bool      `gorm:"column:is_superuser;default:false"  json:"-"`
	IsStuff     bool      `gorm:"column:is_stuff;default:false"  json:"-"`
	IsActive    bool      `gorm:"column:is_active;default:false"  json:"-"`
	FirstName   string    `gorm:"column:first_name;type:varchar(30)" json:"first_name,omitempty"`
	LastName    string    `gorm:"column:last_name;type:varchar(150)" json:"last_name,omitempty"`
	Email       string    `gorm:"type:varchar(254)"`
	DateJoined  time.Time `gorm:"column:date_joined"  json:"-"`
	LastLogin   time.Time `gorm:"column:last_login"  json:"-"`
}

// TableName Set UserProfile's table name
func (UserProfile) TableName() string {
	return "users_userprofile"
}

// IsActived return true if user is active,otherwise false
func (u UserProfile) IsActived() bool {

	return u.IsActive
}

// CheckPassword Check whether the user's password is the same as the given password
func (u UserProfile) CheckPassword(pw string) bool {

	return u.Password == pw
}

// UsernameColumnName Return the table field name corresponding to the username field
func (u UserProfile) UsernameColumnName() string {

	return "username"
}

Use in Gin

import "gin-basicauth/basicauth"

app := gin.Default()
 //input *gorm.DB and user model
app.Use(basicauth.BasicAuth(db, &UserProfile{}))

In Gin Conatroller

func getUserHandler(ctx *gin.Context) {

	user, exists := ctx.Get(basicauth.AuthUserKey) 
	if !exists {
		ctx.JSON(401, "user not authenticated")
		return
	}
	u, ok := user.(*UserProfile)//important
	if ok {
		ctx.JSON(200, u)
		return
	}
	ctx.JSON(401, "user not authenticated")
}

gin-basicauth's People

Contributors

wangyushun avatar

Stargazers

 avatar  avatar

Forkers

dybxin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.