auth finished

This commit is contained in:
ju09279
2024-09-06 22:56:08 +02:00
parent b1be8502c9
commit f26529b1a3
11 changed files with 397 additions and 35 deletions

View File

@@ -12,17 +12,29 @@ func run() error {
port := os.Getenv("PORT")
mux := http.NewServeMux()
mux.Handle("/me", AuthMiddleware(http.HandlerFunc(MeHandler)))
mux.Handle("/login", http.HandlerFunc(LoginRedirect))
mux.Handle("GET /me", AuthMiddleware(http.HandlerFunc(MeHandler)))
mux.Handle("GET /login", http.HandlerFunc(LoginMiddlePage))
mux.Handle("GET /oauth/code", http.HandlerFunc(Oauth))
mux.Handle("POST /settings", AuthMiddleware(http.HandlerFunc(Settings)))
mux.Handle("/oauth/code", http.HandlerFunc(Oauth))
// mux.Handle("POST /setting", );
fmt.Println("Starting Server on", port)
//global middleware
handler := CookieMiddleware(mux)
handler := CORS(CookieMiddleware(Logger(mux)))
return http.ListenAndServe(port, handler)
finalMux := http.NewServeMux()
finalMux.Handle("/", handler)
finalMux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("pong"))
})
return http.ListenAndServe(port, finalMux)
}
func MeHandler(w http.ResponseWriter, r *http.Request) {
@@ -37,6 +49,37 @@ func MeHandler(w http.ResponseWriter, r *http.Request) {
JSONResponse(w, http.StatusOK, user)
}
func Settings(w http.ResponseWriter, r *http.Request) {
type settings struct {
Sharing *bool `json:"sharing"`
Endpoint string `json:"endpoint"`
}
user, ok := r.Context().Value("user").(*User)
if !ok || user == nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
var s settings
if err := json.NewDecoder(r.Body).Decode(&s); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
if s.Endpoint != "" {
UpdateUserEndPoint(user.UserID, s.Endpoint)
}
if s.Sharing != nil {
UpdateUserShare(user.UserID, *s.Sharing)
}
w.WriteHeader(http.StatusOK)
return
}
func JSONResponse(w http.ResponseWriter, statusCode int, data interface{}) {
w.Header().Set("Content-Type", "application/json")