Tasks/sessions/sessions.go

31 lines
726 B
Go
Raw Normal View History

2016-05-09 10:11:05 +08:00
package sessions
import (
"net/http"
"github.com/gorilla/sessions"
)
//Store the cookie store which is going to store session data in the cookie
var Store = sessions.NewCookieStore([]byte("secret-password"))
2016-05-14 15:26:24 +08:00
var session *sessions.Session
2016-05-09 10:11:05 +08:00
//IsLoggedIn will check if the user has an active session and return True
func IsLoggedIn(r *http.Request) bool {
session, err := Store.Get(r, "session")
2016-05-14 15:26:24 +08:00
if err == nil && (session.Values["loggedin"] == "true") {
2016-05-09 10:11:05 +08:00
return true
}
return false
}
2016-05-14 15:26:24 +08:00
//GetCurrentUserName returns the username of the logged in user
func GetCurrentUserName(r *http.Request) string {
session, err := Store.Get(r, "session")
if err == nil {
return session.Values["username"].(string)
}
return ""
}