All Projects → usmanhalalit → hutplate

usmanhalalit / hutplate

Licence: MIT license
A Go library over standard net/http library with auth, session, err handling and more.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to hutplate

rust cms
使用Rust编写一个CMS(内容管理系统)可以做为个人博客,公司网站
Stars: ✭ 32 (+14.29%)
Mutual labels:  session, auth
session
A session service, PSR-15 session middleware, and a flash message service which helps use one-time messages.
Stars: ✭ 14 (-50%)
Mutual labels:  flash-messages, session
koa-better-error-handler
A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
Stars: ✭ 51 (+82.14%)
Mutual labels:  flash-messages, error-handling
iron-session
🛠 Node.js stateless session utility using signed and encrypted cookies to store data. Works with Next.js, Express, NestJs, Fastify, and any Node.js HTTP framework.
Stars: ✭ 1,729 (+6075%)
Mutual labels:  session, auth
Tlog
Terminal I/O logger
Stars: ✭ 170 (+507.14%)
Mutual labels:  session
Thinkgo
A lightweight MVC framework written in Go (Golang).
Stars: ✭ 184 (+557.14%)
Mutual labels:  session
Gta5online Private public lobby
A tool to setup your GTA5 firewall rules easily. Adding IPs to your whitelist enables your friends to stay in your session.
Stars: ✭ 170 (+507.14%)
Mutual labels:  session
Redux React Session
🔑 Simple Session API storage for Redux and React
Stars: ✭ 140 (+400%)
Mutual labels:  session
requests auth
Authentication classes to be used with requests
Stars: ✭ 28 (+0%)
Mutual labels:  auth
teapot
Utilities for working with HTTP status codes, errors, and more
Stars: ✭ 14 (-50%)
Mutual labels:  error-handling
flash
Flash messages between views ⚡️
Stars: ✭ 34 (+21.43%)
Mutual labels:  flash-messages
Aura.session
Tools for managing sessions, including session segments and read-once messages
Stars: ✭ 185 (+560.71%)
Mutual labels:  session
sails-hook-flash
Flash message for Sails applications
Stars: ✭ 14 (-50%)
Mutual labels:  flash-messages
mst-persist
Persist and hydrate MobX-state-tree stores (in < 100 LoC)
Stars: ✭ 75 (+167.86%)
Mutual labels:  session
Responders
A set of Rails responders to dry up your application
Stars: ✭ 1,925 (+6775%)
Mutual labels:  flash-messages
modern-rails-flash-messages
Modern Rails flash messages - Example App
Stars: ✭ 21 (-25%)
Mutual labels:  flash-messages
Next Session
Simple promise-based session middleware for Next.js, micro, Express, and more
Stars: ✭ 161 (+475%)
Mutual labels:  session
Wwdc
You don't have the time to watch all the WWDC session videos yourself? No problem me and many contributors extracted the gist for you 🥳
Stars: ✭ 2,561 (+9046.43%)
Mutual labels:  session
php-flasher
🔔 Flasher is a powerful and flexible flash notifications system for PHP, Laravel, Symfony
Stars: ✭ 68 (+142.86%)
Mutual labels:  flash-messages
CSRF-tutorial
Use Django To Introduce CSRF and Cookies , Session 📝
Stars: ✭ 49 (+75%)
Mutual labels:  session

HutPlate

Build Status

HutPlate is a Library over your standard net/http library which makes your life easier.

It handles Authentication, Session, HTTP Error Handling, Flash Messages and Redirection. All these things are a breeze to do with HutPlate which usually takes a lot of your time.

Some examples:

// Use any router, also using hutplate.Handler is optional
router.Handle("/", hutplate.Handler(CreatePost))

func CreatePost(hp hutplate.Http) interface{} {
	if ! hp.Auth.Check() {
	    // Will redirect and set flash message in session
	    return hp.Response.Redirect("/login").With("notice", "You have to login first!")
	}
	
	// hutplate.Http extends http.Request, so everything is still there
	if err := createPost(hp.FormValue("content")); err =! nil {
	    // Will show a generic error message to user with 500 status.
	    //  You can also customize the behaviour
	    return err
	}
	
	// The http.ResponseWriter is also there
	hp.Response.Write([]byte("Success!"))
}

Table of Contents

Setup

Just go get:

go get github.com/usmanhalalit/hutplate

and import the package.

Configuration

The only mandatory configuration for HutPlate is that you let it know where to find your user (any database/datastore is fine). It will give you email or whatever you log your user in with, you just return that user's id and hashed password. For example, a GORM example would look like this:

hutplate.Config.GetUserWithCred = func(credential interface{}) (interface{}, string) {
    // credential will be email, username or whatever you log in with
    user := models.User{}
    db.Orm.Find(&user, "email='" + credential.(string) + "'")
    
    return user.ID, user.Password
}

Another highly recommended configuration is that you set a session secret key:

hutplate.Config.SessionSecretKey = "a_random_secret_key"

Creating a HutPlate Instance

HutPlate comes with an HTTP handler, which gives you some extra power. But it's optional to use. If you want to keep using the default handler then you just call hutplate.NewHttp and give it your Request and ResponseWriter. Here's how:

func MyHandler(w http.ResponseWriter, r *http.Request,) {
	hp := hutplate.NewHttp(w, r)
}

If you use the HutPlate handler, then it is even easier:

router.Handle("/", hutplate.Handler(CreatePost))

func CreatePost(hp hutplate.Http) interface{} {
	// hutplate.Http extends http.Request, so everything is still there
	// like hp.FormValue("content")

	// The http.ResponseWriter is also there
	hp.Response.Write([]byte("Success!"))
}

Authentication

Login

success, _ := hp.Auth.Login(email, password)

It will let you know if user logging in succeeded or not by returning true or false and if there is an error (in rare case) it'll be in the second return value.

Login requires you to store bcrypt hashed password when you register/save your user. Here is an example of how you hash you password using bcrypt

hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(password), 14)

Check if Logged In

hp.Auth.Check()

Will return true or false.

Get Logged in User ID

hp.Auth.UserId()

Get the Logged in User

It gives you the whole user object, not just id. To use this feature, you need to set a configuration to let HutPlate know how to get the user from your database/datastore.

user, err = hp.Auth.User()

Configuration

hutplate.Config.GetUserWithId = func(userId interface{}) interface {} {
    // You will receive the userId just return the user with corresponding id  
}

A GORM example:

hutplate.Config.GetUserWithId = func(userId interface{}) interface {} {
    user := models.User{}
    if userId == nil {
        return user
    }
    db.Orm.Find(&user, userId)
    return user
}

Logout

hp.Auth.Logout()

HutPlate HTTP Handler

HutPlate also comes with an HTTP handler, using it is optional but you get so much power by using it.

  • It automatically creates an HutPlate instance for you.
  • You can return an error from the handler, so you do much less if err != nil.
  • You can configure how to handle all errors or group of errors. There is a sensible default.
  • You can return a Redirect or a plain string response (text, HTML, etc.).
// Use any router
router.Handle("/", hutplate.Handler(CreatePost))

func CreatePost(hp hutplate.Http) interface{} {
	if ! hp.Auth.Check() {
	    // Will redirect and set flash message in session
	    return hp.Response.Redirect("/login").With("notice", "You have to login first!")
	}
	
	// hutplate.Http extends http.Request, so everything is still there
	if err := createPost(hp.FormValue("content")); err =! nil {
	    // Will show a generic error message to user with 500 status.
	    //  You can also customize the behaviour
	    return err
	}
	
	// The http.ResponseWriter is also there
	hp.Response.Write([]byte("Success!"))
}

Configure the error handler

hutplate.Config.ErrorHandler = func(err error, hut hutplate.Http) {
        // TODO log the error
		http.Error(hut.Response, err.Error(), 404)
}

Redirect

// Plain redirect
hp.Response.Redirect("/admin")

// Redirect with a flash message
hp.Response.Redirect("/login").With("error", "Please do login!")

// Redirect with a different status code
hp.Response.Redirect("/login", 301)

Session

Set a session value and it will persist.

err := hp.Session.Set("test_key", "test_value")

Get the session value

err := hp.Session.Get("test_key")

Flash Messages

err = hut.Session.SetFlash("test_key", "value")

Getting a flash message is same as getting normal session value:

err := hp.Session.Get("test_key")

As mentioned above you can also easily set a flash message while you redirect.

Session Config

Storage Path

By default Session uses file system storage. You can optionally configure the storage directory by setting:

hutplate.Config.SessionDirectory = "path/to/dir"

By default it uses operating system's temp directory.

Secret Key

It is recommended that you set a session secret key by setting:

hutplate.Config.SessionSecretKey = "your_key"

Configure Session Store

As noted above, by default Session uses file system storage. But you can use an entire different session storage system. HutPlate uses Gorilla Sessions. So you can use any session storage supported by Gorilla Sessions, i.e. the store that implements Gorilla's sessions.Store interface.

Here is a list of session stores supported by Gorilla Sessions.

To use your own session store, use the SessionStore config, for example:

import "github.com/gorilla/sessions"
hutplate.Config.SessionStore = sessions.NewCookieStore(...)

Clear Context

Important Note: If you aren't using gorilla/mux router, you need to wrap your handlers with context.ClearHandler as or else you will leak memory! An easy way to do this is to wrap the top-level mux when calling http.ListenAndServe:

	http.ListenAndServe(":8080", context.ClearHandler(http.DefaultServeMux))

The ClearHandler function is provided by the gorilla/context package.

More examples are available on the Gorilla website.

List of Config

hutplate.Config.GetUserWithId = func(userId interface{}) interface {} {

}

hutplate.Config.GetUserWithCred = func(credential interface{}) (interface{}, string) {
    
}

hutplate.Config.ErrorHandler = func(err error, hp hutplate.Http) {

}

hutplate.Config.SessionSecretKey = "a_secret_key"
hutplate.Config.SessionDirectory = "path/to/dir"

© 2020 Muhammad Usman. Licensed under MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].