53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"text/template"
|
|
|
|
"astraltech.xyz/calendar/v2/auth"
|
|
)
|
|
|
|
type CalendarData struct {
|
|
DisplayName string
|
|
}
|
|
|
|
func ParseOAuthLogin(w http.ResponseWriter, r *http.Request) CalendarData {
|
|
token, err := auth.TestTokenStore.Load()
|
|
if err != nil {
|
|
http.Error(w, "Not authenticated", http.StatusUnauthorized)
|
|
return CalendarData{}
|
|
}
|
|
|
|
client := auth.DefaultProvider.Config.Client(context.Background(), token)
|
|
|
|
resp, err := client.Get(auth.DefaultProvider.Provider.UserInfoEndpoint())
|
|
if err != nil {
|
|
http.Error(w, "Failed to fetch user: "+err.Error(), http.StatusInternalServerError)
|
|
return CalendarData{}
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var usersInfo auth.UserInfo
|
|
if err := json.NewDecoder(resp.Body).Decode(&usersInfo); err != nil {
|
|
http.Error(w, "Failed to decode response", http.StatusInternalServerError)
|
|
return CalendarData{}
|
|
}
|
|
return CalendarData{
|
|
DisplayName: usersInfo.Name,
|
|
}
|
|
}
|
|
|
|
func HandleCalandarEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
var OauthLogin = true
|
|
var data CalendarData
|
|
if OauthLogin {
|
|
data = ParseOAuthLogin(w, r)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
tmpl := template.Must(template.ParseFiles("main/pages/calendar.html"))
|
|
tmpl.Execute(w, data)
|
|
}
|