add frontend template + GET /users/{userRef} route

This commit is contained in:
Sam 2022-05-04 16:27:16 +02:00
parent 5a75f99720
commit 580449440a
28 changed files with 1393 additions and 12 deletions

View file

@ -3,7 +3,9 @@ package auth
import (
"net/http"
"emperror.dev/errors"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"gitlab.com/1f320/pronouns/backend/server"
)
@ -13,15 +15,46 @@ type Server struct {
func Mount(srv *server.Server, r chi.Router) {
s := &Server{srv}
_ = s
r.Route("/auth/discord", func(r chi.Router) {
r.Get("/authorize", nil) // generate csrf token, returns URL
r.Get("/callback", nil) // takes code + state, validates it, returns token OR discord signup ticket
r.Get("/signup", nil) // takes discord signup ticket to register account
r.Route("/auth", func(r chi.Router) {
// generate csrf token, returns all supported OAuth provider URLs
r.Get("/urls", server.WrapHandler(s.oauthURLs))
r.Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world!"))
r.Route("/discord", func(r chi.Router) {
// takes code + state, validates it, returns token OR discord signup ticket
r.Post("/callback", nil)
// takes discord signup ticket to register account
r.Post("/signup", nil)
})
})
}
type oauthURLsRequest struct {
CallbackURL string `json:"callback_url"`
}
type oauthURLsResponse struct {
Discord string `json:"discord"`
}
func (s *Server) oauthURLs(w http.ResponseWriter, r *http.Request) error {
req, err := Decode[oauthURLsRequest](r)
if err != nil {
return server.APIError{Code: server.ErrBadRequest}
}
// generate CSRF state
state, err := s.setCSRFState(r.Context())
if err != nil {
return errors.Wrap(err, "setting CSRF state")
}
// copy Discord config and set redirect url
discordCfg := discordOAuthConfig
discordCfg.RedirectURL = req.CallbackURL
render.JSON(w, r, oauthURLsResponse{
Discord: discordCfg.AuthCodeURL(state),
})
return nil
}