Added session variables, added way to get session from client packages

This commit is contained in:
2017-10-06 21:12:09 +02:00
parent 95eae1f797
commit 446c6e4ef9
2 changed files with 25 additions and 4 deletions
+20 -1
View File
@@ -22,11 +22,28 @@ type Session struct {
ID string `json:"id"`
UserID string `json:"user"`
Time time.Time `json:"time"`
data map[string]string
}
// SetData sets session data
func (sess *Session) SetData(key, value string) {
sess.data[key] = value
}
// GetData gets session data for the following key
func (sess *Session) GetData(key string) string {
return sess.data[key]
}
// HasData checks if the session data contains the key
func (sess *Session) HasData(key string) bool {
_, ok := sess.data[key]
return ok
}
// OpenSession creates a new session from the supplied user's ID
func OpenSession(user *User) *Session {
session := &Session{generate.SessionID(), user.FullID(), time.Now()}
session := &Session{generate.SessionID(), user.FullID(), time.Now(), make(map[string]string)}
sessionMutex.Lock()
sessions[session.ID] = session
@@ -38,6 +55,8 @@ func OpenSession(user *User) *Session {
go cleanup()
}
user.Session = session
return session
}
+5 -3
View File
@@ -1,9 +1,11 @@
package auth
// User represents a logged-in user of the website or app.
type User struct {
ID string `json:"id"`
Level string `json:"level"`
Data map[string]string `json:"data"`
ID string `json:"id"`
Level string `json:"level"`
Data map[string]string `json:"data"`
Session *Session `json:"-"`
method Authenticator
loggedOut bool