go_login

package module
v0.0.0-...-c1085e8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2017 License: MIT Imports: 7 Imported by: 0

README

go_login

A go HTTP login package for net/http.

Usage

Define a LoginManager

var(
	LoginConfig = go_login.NewConfig("hello")
	LoginManager = go_login.NewLoginManager(LoginConfig)
)

Define user model

mixin go_login.UserMixin


type User struct {
	go_login.UserMixin
	name string
	password string
}

Login and Logout
login

func LoginHandle(w http.ResponseWriter, r *http.Request) {
   	if r.Method == "POST" {
   		u, islogin := LoginManager.Current(r)
   		if u != nil{
   			currentUser,_ := u.(*User)
   			if islogin == true {
   				fmt.Fprint(w,"you already login ", currentUser.name)
   				return
   			}
   			if islogin == false{
   				LoginManager.Login(currentUser, &w)
   			}
   			return
   		}
   		username := r.FormValue("username")
   		password := r.FormValue("password")
   		if username == "admin" && password == "123"{
   			user := &User{
   				name:     username,
   				password: password,
   			}
   			LoginManager.Login(user,&w)
   			fmt.Println("user login")
   		}
   		fmt.Fprint(w,"login")
   	}

   }

logout

func LogoutHandle(w http.ResponseWriter, r *http.Request) {
	user,ok := LoginManager.Current(r)
	if user == nil || ok == false{
		fmt.Fprint(w,"No one logout")
		return
	}
	LoginManager.Logout(user,r, &w)
	fmt.Fprint(w,"logout")
}

Auth and Current User

func TestHandle(w http.ResponseWriter, r *http.Request){
	user,ok := LoginManager.Auth(r)
	if !ok {
		fmt.Fprint(w,"None")
		return
	}
	myUser,_ := user.(*User)
	fmt.Fprint(w,"Hello ",myUser.name)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenSessionId

func GenSessionId(identity string) string

func GenToken

func GenToken(identity string) string

func GetSessionId

func GetSessionId(r *http.Request) (string, error)

func GetToken

func GetToken(r *http.Request) (string, error)

func NewConfig

func NewConfig(secret string) *config

Types

type BaseUser

type BaseUser interface {
	// contains filtered or unexported methods
}

type LoginManager

type LoginManager struct {
	UserMap map[string]BaseUser
	// contains filtered or unexported fields
}

func NewLoginManager

func NewLoginManager(config *config) *LoginManager

func (*LoginManager) Auth

func (manager *LoginManager) Auth(request *http.Request) (BaseUser, bool)

func (*LoginManager) Current

func (manager *LoginManager) Current(request *http.Request) (BaseUser, bool)

func (*LoginManager) GC

func (manager *LoginManager) GC()

func (*LoginManager) Login

func (manager *LoginManager) Login(user BaseUser, w *http.ResponseWriter)

func (*LoginManager) Logout

func (manager *LoginManager) Logout(user BaseUser, r *http.Request, w *http.ResponseWriter)

type UserMixin

type UserMixin struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL