Added session variables, added way to get session from client packages
This commit is contained in:
+20
-1
@@ -22,11 +22,28 @@ type Session struct {
|
|||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
UserID string `json:"user"`
|
UserID string `json:"user"`
|
||||||
Time time.Time `json:"time"`
|
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
|
// OpenSession creates a new session from the supplied user's ID
|
||||||
func OpenSession(user *User) *Session {
|
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()
|
sessionMutex.Lock()
|
||||||
sessions[session.ID] = session
|
sessions[session.ID] = session
|
||||||
@@ -38,6 +55,8 @@ func OpenSession(user *User) *Session {
|
|||||||
go cleanup()
|
go cleanup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user.Session = session
|
||||||
|
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
|
// User represents a logged-in user of the website or app.
|
||||||
type User struct {
|
type User struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Level string `json:"level"`
|
Level string `json:"level"`
|
||||||
Data map[string]string `json:"data"`
|
Data map[string]string `json:"data"`
|
||||||
|
Session *Session `json:"-"`
|
||||||
|
|
||||||
method Authenticator
|
method Authenticator
|
||||||
loggedOut bool
|
loggedOut bool
|
||||||
|
|||||||
Reference in New Issue
Block a user