Prototype for backend email confirmaion

This commit is contained in:
Logan 2024-10-23 16:44:49 -05:00
parent 8e6ff08eb4
commit 3462fc966c
3 changed files with 18 additions and 23 deletions

View file

@ -1,7 +1,6 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
@ -13,22 +12,11 @@ import (
const CANVAS_WIDTH = 1000
const CANVAS_HEIGHT = 1000
type Canvas struct {
Data [CANVAS_WIDTH * CANVAS_HEIGHT]byte `json:"data"`
Width int `json:"width"`
Height int `json:"height"`
}
type Server struct {
// Registered user information
Users map[string]UserData
// Login sessions
Sessions *sessions.CookieStore
Canvas Canvas
}
func (s *Server) canvas(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(s.canvas)
}
func main() {
@ -43,13 +31,6 @@ func main() {
server := Server{
Users: make(map[string]UserData),
Sessions: sessions.NewCookieStore(secret),
Canvas: Canvas{},
}
for x := 0; x < CANVAS_WIDTH; x++ {
for y := 0; y < CANVAS_HEIGHT; y++ {
server.Canvas.Data[y*CANVAS_HEIGHT+x] = byte(CANVAS_WIDTH / x)
}
}
// Host static files
@ -66,7 +47,7 @@ func main() {
http.Redirect(w, r, "/", http.StatusFound)
})
http.HandleFunc("/secret", server.secret)
http.HandleFunc("/canvas", canvas)
http.HandleFunc("/confirm-email", server.handle_confirmation)
// Start web server at 127.0.0.1:8080
fmt.Printf("Listening to %s on port %s...\n", address, port)
err := http.ListenAndServe(address+":"+port, nil)

View file

@ -12,7 +12,7 @@
<body>
<div id="h3bg" class="d-flex justify-content-center"><h3 id="r">Registration</h3></div>
<div id="formbg" class="d-flex p-2 justify-content-center align-items-center">
<form id="rform" method="post" action="./confirmation.html">
<form id="rform" method="post">
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" aria-describedby="emailHelp" pattern="^[^\s@]+@my\.utsa\.edu$" oninput="emailCheck()" required>
@ -35,7 +35,7 @@
</small>
<br><br>
<input id="submit" type="submit" class="btn btn-primary" value="Submit"></a>
<input id="submit" type="submit" class="btn btn-primary" value="Submit">
</form>
</div>
<script src="register.js"></script>

View file

@ -13,6 +13,7 @@ import (
const SESSION_COOKIE_NAME = "utsa-place-session"
const SESSION_AUTH = "auth"
const SESSION_STARTED = "age"
const SESSION_CONFIRMED = "confirmed"
const ENCRYPTION_STRENGTH = 14
@ -126,16 +127,29 @@ func (s *Server) handle_register(w http.ResponseWriter, r *http.Request) {
// Make session valid
session.Values[SESSION_AUTH] = true
session.Values[SESSION_STARTED] = now.String()
session.Values[SESSION_CONFIRMED] = false
// Send session token to browser
session.Save(r, w)
// Redirect to index.html
fmt.Println("Registered user: ", email)
http.Redirect(w, r, "/", http.StatusFound)
http.Redirect(w, r, "/confirm-email", http.StatusFound)
default:
http.Error(w, "Forbidden", http.StatusForbidden)
}
}
func (s *Server) handle_confirmation(w http.ResponseWriter, r *http.Request) {
session, err := s.Sessions.Get(r, SESSION_COOKIE_NAME)
if err != nil {
s.handle_logout(w, r)
http.Redirect(w, r, "/register", http.StatusFound)
return
}
confirmed := session.Values[SESSION_CONFIRMED]
fmt.Println("Session confirmed: ", confirmed)
http.ServeFile(w, r, "./static/confirmation.html")
}
func (s *Server) secret(w http.ResponseWriter, r *http.Request) {
session, _ := s.Sessions.Get(r, SESSION_COOKIE_NAME)