website/pkg/session/current_user.go
Noah dd1e6c2918 Initial commit
* Initial codebase (lot of work!)
* Uses vanilla Go net/http and implements by hand: session cookies
  backed by Redis; log in/out; CSRF protection; email verification flow;
  initial database models (User table)
2022-08-09 22:32:19 -07:00

20 lines
396 B
Go

package session
import (
"errors"
"net/http"
"git.kirsle.net/apps/gosocial/pkg/models"
)
// CurrentUser returns the current logged in user via session cookie.
func CurrentUser(r *http.Request) (*models.User, error) {
sess := Get(r)
if sess.LoggedIn {
// Load the associated user ID.
return models.GetUser(sess.UserID)
}
return nil, errors.New("request session is not logged in")
}