]>
code.octet-stream.net Git - broadcaster/blob - server/session.go
a097989a774248ec405b4c95f88415cf22196215
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",