website/pkg/controller/api/auth.go

39 lines
809 B
Go
Raw Normal View History

package api
import (
"encoding/json"
"net/http"
2022-08-26 04:21:46 +00:00
"code.nonshy.com/nonshy/website/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)
})
}