What is Gologin

Gologin is an easy to setup professional login manager for Go web applications. It helps you protect your application resources from unattended, unauthenticated or unauthorized access. Currently it works with SQL databases authentication. It is flexible, you can use it with any user/roles table structure in database.

How to setup

Get the package with following command :

go get github.com/birddevelper/gologin

How to use

You can easily setup and customize login process with configure() function. You should specify following paramters to make the Gologin ready to start:

  • Login page : path to html template. Default path is ./template/login.html, note that the template must be defined as “login” with {{define “login”}} at the begining line

  • Login path : login http path. Default path is /login

  • Session timeout : Number of seconds before the session expires. Default value is 120 seconds.

  • SQL connection, and SQL query to authenticate user and fetch roles : 2 SQL queries to retrieve user and its roles by given username and password. The authentication query must return only single arbitary column, it must have a where clause with two placeholder ::username and ::password. And the query for retrieving user’s roles must return only the text column of role name.

  • Wrap desired endpoints to protect : You should wrap the endpoints you want to protect with gologin.LoginRequired or gologin.RolesRequired function in the main function.( see the example)

gologin.LoginRequired requires user to be authenticated for accessing the wrapped endpoint/page.

gologin.RolesRequired requires user to have specified roles in addition to be authenticated.

See the example :

package main

import (

	_ "github.com/go-sql-driver/mysql"

// static assets like CSS and JavaScript
func public() http.Handler {
	return http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))

// a page in our application, it needs user only be authenticated
func securedPage() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hi! Welcome to secured page.")

// another page in our application, it needs user be authenticated and have ADMIN role
func securedPage2() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hi! Welcome to very secured page.")

func main() {
	// create connection to database
	db, err := sql.Open("mysql", "root:[email protected](")
	if err != nil {

	// Gologin configuration
		SetLoginPage("./template/login.html"). // set login page html template path
		SetSessionTimeout(90).                 // set session expiration time in seconds
		SetLoginPath("/login").                // set login http path
		// set database connection and sql query
			"select id from users where username = ::username and password = ::password", // authentication query
			"select role from user_roles where userid = (select id from users where username = ::username)") // fetch user's roles

	// instantiate http server
	mux := http.NewServeMux()

	mux.Handle("/static/", public())

	// use Gologin login handler for /login endpoint
	mux.Handle("/login", gologin.LoginHandler())

	// use Gologin logout handler for /logout endpoint
	mux.Handle("/logout", gologin.LogoutHandler())

	// the pages/endpoints that we need to protect should be wrapped with gologin.LoginRequired
	mux.Handle("/mySecuredPage", gologin.LoginRequired(securedPage()))

	mux.Handle("/mySecuredPage2", gologin.RolesRequired(securedPage2()),"ADMIN")

	// server configuration
	addr := ":8080"
	server := http.Server{
		Addr:         addr,
		Handler:      mux,
		ReadTimeout:  15 * time.Second,
		WriteTimeout: 15 * time.Second,
		IdleTimeout:  15 * time.Second,

	// start listening to network
	if err := server.ListenAndServe(); err != nil {
		log.Fatalf("main: couldn't start simple server: %v\n", err)

It is mandatory to set the login form’s username input as “username” and password input as “password”. Note that the form must send form data as post to the same url (set no action attribute).

Html template for login page :

{{define "login"}}
            Login Page
        <form method="post">
            <!-- username input with "username" name -->
            <input type="text" name="username" />
            <input type="password" name="password" />
            <input type="submit" value="Login" />


You can also store data in in-memory session storage in any type during user’s session with SetSession function, and retrieve it back by GetSession function.

func securedPage2() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// get the session data, the request parameter is *http.Request
		age, err : = gologin.GetSession("age", request)

		// as the GetSession returns type is interface{}, we should specify the exact type of the session entry
		fmt.Printf("Your age is " + age.(int))

GetDataReturnedByAuthQuery function returns the data of the column you specified in authentication SQL query. And with GetCurrentUsername you can get the current user’s username.

func securedPage2() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			// get the current user's username, the request parameter is *http.Request
			username : = gologin.GetCurrentUsername(request)

			fmt.Printf("Welcome " + username)

Todo list

  • mongoDB support


View Github