http response tooling written
This commit is contained in:
+24
-28
@@ -3,44 +3,41 @@ package site
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"ruben/inventory2/internal/site/response"
|
||||
)
|
||||
|
||||
// GET /login
|
||||
func (s *Server) loginPage(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) loginPage(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
|
||||
state, err := s.auth.NewState(ctx)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to generate random state: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return nil, response.Errorf("failed to generate random state: %w", err)
|
||||
}
|
||||
|
||||
base64EncodedState := fmt.Sprintf("%x", state[:])
|
||||
|
||||
http.Redirect(w, r, s.auth.AuthCodeURL(base64EncodedState), http.StatusTemporaryRedirect)
|
||||
return response.TemporaryRedirect(s.auth.AuthCodeURL(base64EncodedState)), nil
|
||||
}
|
||||
|
||||
// POST /login
|
||||
func (s *Server) login(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) login(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
email := r.FormValue("email")
|
||||
if email == "" {
|
||||
http.Error(w, "no email provided", http.StatusBadRequest)
|
||||
return
|
||||
return nil, response.BadRequest().Msg("no email provided")
|
||||
}
|
||||
|
||||
acct, err := s.accts.GetAccountByEmail(ctx, email)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create account: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return nil, response.Errorf("failed to create account: %w", err)
|
||||
}
|
||||
|
||||
//http.Redirect(w, r, fmt.Sprintf("/site/accounts/%d", acct.ID), http.StatusSeeOther)
|
||||
http.Redirect(w, r, fmt.Sprintf("/accounts/%d", acct.ID), http.StatusSeeOther)
|
||||
return response.SeeOther(fmt.Sprintf("/accounts/%d", acct.ID)), nil
|
||||
}
|
||||
|
||||
// GET /login/callback
|
||||
func (s *Server) loginCallback(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) loginCallback(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
q := r.URL.Query()
|
||||
|
||||
@@ -48,38 +45,37 @@ func (s *Server) loginCallback(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
accessToken, expiration, err := s.auth.Exchange(ctx, q.Get("state"), q.Get("code"))
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to exchange an authorization code for a token: %v", err), http.StatusUnauthorized)
|
||||
return
|
||||
return nil, response.Unauthorized().
|
||||
Msg(fmt.Sprintf("Failed to exchange an authorization code for a token")).
|
||||
Wrap(err)
|
||||
}
|
||||
|
||||
// set access_token cookie and redirect to a reasonable place
|
||||
|
||||
w.Header().Set("Set-Cookie", (&http.Cookie{
|
||||
Name: "access_token",
|
||||
Value: accessToken,
|
||||
Path: "/",
|
||||
Expires: expiration,
|
||||
MaxAge: 0, // using Expiration instead
|
||||
Secure: true,
|
||||
}).String())
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||
return response.TemporaryRedirect("/").
|
||||
Cookie(http.Cookie{
|
||||
Name: "access_token",
|
||||
Value: accessToken,
|
||||
Path: "/",
|
||||
Expires: expiration,
|
||||
MaxAge: 0, // using Expiration instead
|
||||
Secure: true,
|
||||
}), nil
|
||||
}
|
||||
|
||||
// GET /logout
|
||||
func (s *Server) logoutPage(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) logoutPage(r *http.Request) (response.Response, error) {
|
||||
host := r.Header.Get("X-Forwarded-Host")
|
||||
if host == "" {
|
||||
host = r.Host
|
||||
}
|
||||
|
||||
deleteCookieInResponse(w, "access_token")
|
||||
|
||||
if ck, err := r.Cookie("access_token"); err == nil && ck != nil {
|
||||
if err := s.auth.DeleteOAuthTokens(r.Context(), ck.Value); err != nil {
|
||||
fmt.Println("[ERROR] failed to delete auth token:", err)
|
||||
}
|
||||
}
|
||||
|
||||
http.Redirect(w, r, s.auth.GetLogoutURL(host).String(), http.StatusTemporaryRedirect)
|
||||
return response.TemporaryRedirect(s.auth.GetLogoutURL(host).String()).
|
||||
Cookie(getExpiredCookie("access_token")), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user