gologin

package module
v0.0.0-...-21af988 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2015 License: MIT Imports: 3 Imported by: 0

README

GoLogin

GoLogin is a login middleware for Go web apps inspired by Flask login.

Example usage (main.go):

package main

import (
	"github.com/veegee/gologin"
	"github.com/gorilla/mux"
)

var CookieStore *sessions.CookieStore
var LoginManager *gologin.GoLogin

func LoadUser(userid string) gologin.User {
	user := &dbmodels.User{}

	// ... query the database and populate the user object ...

	return user
}

func PermissionDenied(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "error: not logged in")
}

func Index(w http.ResponseWriter, r *http.Request) {
	if !LoginManager.IsLoggedIn(r) {
		LoginManager.PermissionDeniedHandler(w, r)
		return
	}
}

func Login(w http.ResponseWriter, r *http.Request) {
	currentUser, ok := context.Get(r, gologin.CV_CURRENTUSER).(*dbmodels.Entity)

	if currentUser != nil && ok {
		// already logged in
	}

	user := ... look up user by email and verify password ...

	if user != nil {
		LoginManager.LoginUser(user, w, r)
		fmt.Fprintf(w, "Login success")
	} else {
		http.Error(w, "Login failed", http.StatusForbidden)
	}
}

func main() {
	CookieStore = sessions.NewCookieStore([]byte("something-very-secret"))
	LoginManager = &gologin.GoLogin{CookieStore, LoadUser, PermissionDenied}

	router := mux.NewRouter()
	router.HandleFunc("/", Index)
	router.HandleFunc("/login", Login)

	n := negroni.New()
	n.Use(globals.LoginManager)
	n.UseHandler(router)
	n.Run("0.0.0.0:8002")
}

Documentation

Index

Constants

View Source
const (
	SESSION_NAME = "user"     // gorilla sessions session name
	SV_USERID    = "userid"   // stored user id (string)
	SV_LOGGEDIN  = "loggedin" // logged in status (bool)
)

session cookie variable names

View Source
const (
	CV_CURRENTUSER = "currentuser" // currently logged-in user (User)
)

request context variable names

View Source
const VERSION = "0.1.0"

Variables

This section is empty.

Functions

This section is empty.

Types

type GoLogin

type GoLogin struct {
	CookieStore             *sessions.CookieStore
	LoadUser                func(userid string) User
	PermissionDeniedHandler http.HandlerFunc
}

func (*GoLogin) IsLoggedIn

func (gl *GoLogin) IsLoggedIn(r *http.Request) bool

Check if the user for this request is logged in

func (*GoLogin) LoginUser

func (gl *GoLogin) LoginUser(user User, w http.ResponseWriter, r *http.Request) error

Log the user in

This stores data in a session cookie. The request context's "currentuser" variable is set to the user object that is passed.

func (*GoLogin) LogoutUser

func (gl *GoLogin) LogoutUser(w http.ResponseWriter, r *http.Request) error

func (*GoLogin) ServeHTTP

func (gl *GoLogin) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

HTTP middleware function

This sets the request context's "currentuser" variable to the user object returned by LoadUser().

type User

type User interface {
	GetId() string // get the user id
}

Jump to

Keyboard shortcuts

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