diff --git a/appview/server/index.go b/appview/server/index.go index c38219e..a59a739 100644 --- a/appview/server/index.go +++ b/appview/server/index.go @@ -7,6 +7,20 @@ import ( var tmpl = template.Must(template.ParseFiles("appview/templates/index.html")) +type PageData struct { + LoggedIn bool + Username string +} + func (srv *Server) IndexHandler(w http.ResponseWriter, r *http.Request) { - tmpl.Execute(w, nil) + + sess, _ := srv.cookieStore.Get(r, "jobsite") + accountDID, ok := sess.Values["account_did"].(string) + + data := PageData{ + LoggedIn: ok && accountDID != "", + Username: accountDID, // or look up a display name if you have one + } + + tmpl.Execute(w, data) } diff --git a/appview/server/oauth.go b/appview/server/oauth.go index 40ef3a8..5e70324 100644 --- a/appview/server/oauth.go +++ b/appview/server/oauth.go @@ -3,6 +3,7 @@ package server import ( "encoding/json" "fmt" + "log/slog" "net/http" ) @@ -54,3 +55,33 @@ func (srv *Server) HandleJWKS(w http.ResponseWriter, r *http.Request) { return } } + +func (srv *Server) HandleOAuthCallback(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + sessData, err := srv.oauthApp.ProcessCallback(ctx, r.URL.Query()) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + slog.Error("OAuth callback error", "error", err) + } + + // _oauthSess, err := srv.oauthApp.ResumeSession(ctx, sessData.AccountDID, sessData.SessionID) + // if err != nil { + // http.Error(w, "not authenticated", http.StatusUnauthorized) + // return + // } + + _ = sessData.AccountDID + sess, _ := srv.cookieStore.Get(r, "jobsite") // WARN: this should be changed when we have a project name + sess.Values["account_did"] = sessData.AccountDID.String() + sess.Values["session_id"] = sessData.SessionID + + if err := sess.Save(r, w); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + slog.Error("Session save error", "error", err) + return + } + + slog.Info("User logged in", "did", sessData.AccountDID.String()) + http.Redirect(w, r, "/", http.StatusFound) +} diff --git a/appview/server/server.go b/appview/server/server.go index 97428f3..7190e9e 100644 --- a/appview/server/server.go +++ b/appview/server/server.go @@ -1,13 +1,20 @@ package server -import "github.com/bluesky-social/indigo/atproto/auth/oauth" +import ( + "os" + + "github.com/bluesky-social/indigo/atproto/auth/oauth" + "github.com/gorilla/sessions" +) type Server struct { - oauthApp *oauth.ClientApp + oauthApp *oauth.ClientApp + cookieStore *sessions.CookieStore } func NewServer(oauthApp *oauth.ClientApp) *Server { return &Server{ oauthApp: oauthApp, + cookieStore: sessions.NewCookieStore([]byte(os.Getenv("SESSION_SECRET"))), } } diff --git a/appview/templates/index.html b/appview/templates/index.html index 8b454d2..91f1fc1 100644 --- a/appview/templates/index.html +++ b/appview/templates/index.html @@ -6,10 +6,11 @@ -

Hello, HTMX + Go!

- -
+

Jobsite. Well it will be

+ {{ if .LoggedIn }} +

Welcome, {{ .Username }}!

+ {{ else }}

Login