Display calendar page with name of login, also add in README.md
This commit is contained in:
@@ -3,17 +3,21 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"text/template"
|
||||
|
||||
"astraltech.xyz/calendar/v2/auth"
|
||||
)
|
||||
|
||||
func HandleCalandarEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
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
|
||||
return CalendarData{}
|
||||
}
|
||||
|
||||
client := auth.DefaultProvider.Config.Client(context.Background(), token)
|
||||
@@ -21,15 +25,28 @@ func HandleCalandarEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
resp, err := client.Get(auth.DefaultProvider.Provider.UserInfoEndpoint())
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to fetch user: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
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
|
||||
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)
|
||||
}
|
||||
|
||||
fmt.Printf("The users email is %s\n", usersInfo.Email)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
tmpl := template.Must(template.ParseFiles("main/pages/calendar.html"))
|
||||
tmpl.Execute(w, data)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Astral Calendar</title>
|
||||
</head>
|
||||
<body>
|
||||
Welcome to your calendar {{.DisplayName}}
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user