website/pkg/controller/api/auth.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

39 lines
807 B
Go

package api
import (
"encoding/json"
"net/http"
"git.kirsle.net/apps/gosocial/pkg/session"
)
// LoginOK API tests the validity of a user's session cookie.
func LoginOK() http.HandlerFunc {
type Response struct {
Success bool `json:"success"`
UserID uint64 `json:"userId"`
Username string `json:"username"`
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if we're logged in.
var res Response
if user, err := session.CurrentUser(r); err == nil {
res = Response{
Success: true,
UserID: user.ID,
Username: user.Username,
}
}
buf, err := json.Marshal(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(buf)
})
}