Tasks/sessions/sessions.go

31 lines
726 B
Go
Raw Normal View History

2016-05-09 07:41:05 +05:30
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 12:56:24 +05:30
var session *sessions.Session
2016-05-09 07:41:05 +05:30
//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 12:56:24 +05:30
if err == nil && (session.Values["loggedin"] == "true") {
2016-05-09 07:41:05 +05:30
return true
}
return false
}
2016-05-14 12:56:24 +05:30
//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 ""
}