]>
code.octet-stream.net Git - broadcaster/blob - broadcaster-server/session.go
11 func generateSession() string {
13 _
, err
:= rand
.Read(b
)
17 return hex
.EncodeToString(b
)
20 func currentUser(_ http
.ResponseWriter
, r
*http
.Request
) (User
, error
) {
21 cookie
, e
:= r
.Cookie("broadcast_session")
26 return users
.GetUserForSession(cookie
.Value
)
29 func createSessionCookie(w http
.ResponseWriter
, username
string) {
30 sess
:= generateSession()
31 log
.Println("Generated a random session", sess
)
32 expiration
:= time
.Now().Add(365 * 24 * time
.Hour
)
33 cookie
:= http
.Cookie
{Name
: "broadcast_session", Value
: sess
, Expires
: expiration
, SameSite
: http
.SameSiteLaxMode
}
34 db
.InsertSession(username
, sess
, expiration
)
35 http
.SetCookie(w
, &cookie
)
38 func clearSessionCookie(w http
.ResponseWriter
) {
40 Name
: "broadcast_session",