diff options
-rw-r--r-- | main.go | 28 | ||||
-rw-r--r-- | oidc.go | 16 | ||||
-rw-r--r-- | tmpl/index.tmpl | 27 | ||||
-rw-r--r-- | tmpl/index_login.tmpl | 11 |
4 files changed, 60 insertions, 22 deletions
@@ -59,8 +59,32 @@ func handle_index(w http.ResponseWriter, req *http.Request) { ))) return } - _ = session_cookie - err = tmpl.ExecuteTemplate(w, "index", nil) + var session session_t + err = db.First(&session, session_t{Cookie: session_cookie.Value}).Error + if err != nil { + err = tmpl.ExecuteTemplate( + w, + "index_login", + map[string]interface{}{ + "authUrl": generate_authorization_url(), + "notes": []string{"Cookie lookup failed. You are now unauthenticated."}, + }, + ) + if err != nil { + log.Println(err) + return + } + return + } + fmt.Println(session.User) + user := session.User + err = tmpl.ExecuteTemplate( + w, + "index", + map[string]interface{}{ + "user": user, + }, + ) if err != nil { log.Println(err) return @@ -246,7 +246,9 @@ func handle_oidc(w http.ResponseWriter, req *http.Request) { http.SetCookie(w, &cookie) w.Header().Set("Content-Type", "text/plain; charset=utf-8") + var session session_t var user user_t + err = db.FirstOrCreate(&user, user_t{Subject: claims.Subject}).Error if err != nil { w.Header().Set("Content-Type", "text/plain; charset=utf-8") @@ -261,14 +263,12 @@ func handle_oidc(w http.ResponseWriter, req *http.Request) { user.Email = claims.Email db.Save(&user) - w.WriteHeader(200) - w.Write([]byte(fmt.Sprintf( - "Name: %s\nEmail: %s\nSubject: %s\nCookie: %s\n", - claims.Name, - claims.Email, - claims.Subject, - cookie_value, - ))) + session.User = user + session.Cookie = cookie_value + err = db.Create(&session).Error + + http.Redirect(w, req, "/", 303) + return } diff --git a/tmpl/index.tmpl b/tmpl/index.tmpl index f176657..08a86af 100644 --- a/tmpl/index.tmpl +++ b/tmpl/index.tmpl @@ -1,4 +1,6 @@ {{- /* + * vim: filetype=gotmpl + * * Copyright (c) 2024 Runxi Yu * SPDX-License-Identifier: AGPL-3.0-or-later * @@ -17,16 +19,31 @@ */ -}} {{- define "index" -}} <!DOCTYPE html> -<html> +<html lang="en"> <head> <title> - Title + Home – FBFP </title> + <link rel="stylesheet" href="/static/style.css" /> + <meta charset="utf-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> </head> <body> - <p> - Paragraph - </p> + <header> + <div class="header-content"> + <div class="header-left"> + <h1><a id="site-title" href="/">FBFP</a></h1> + </div> + <div class="header-middle"> + </div> + <div class="header-right"> + <p>{{- .user.Name -}}</p> + </div> + </div> + </header> + <main> + e + </main> </body> </html> {{- end -}} diff --git a/tmpl/index_login.tmpl b/tmpl/index_login.tmpl index 819954a..a15a224 100644 --- a/tmpl/index_login.tmpl +++ b/tmpl/index_login.tmpl @@ -22,7 +22,7 @@ <html lang="en"> <head> <title> - Login required – FBFP + Authentication required – FBFP </title> <link rel="stylesheet" href="/static/style.css" /> <meta charset="utf-8" /> @@ -41,12 +41,6 @@ <h1><a id="site-title" href="/">FBFP</a></h1> </div> <div class="header-middle"> - {{- /* - <nav> - <ul> - </ul> - </nav> - */ -}} </div> <div class="header-right"> <p>Unauthenticated</p> @@ -54,6 +48,9 @@ </div> </header> <main> + <p> + {{- .notes -}} + </p> <div id="login-box"> <p> You have not authenticated. You must sign in to use this service. |