Skip to content

Commit

Permalink
chore: added core logic
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-cardenas-coding committed Jun 23, 2024
1 parent aa21401 commit 64b8c75
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
31 changes: 28 additions & 3 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"text/template"
"time"

"github.com/karl-cardenas-coding/mywhoop/internal"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -66,12 +67,36 @@ func login() error {

}

redirectHandler := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Redirected"))
submitHandler := func(w http.ResponseWriter, r *http.Request) {
username := r.FormValue("username")
password := r.FormValue("password")

slog.Info("Username and password received", "username", username, "password", password)
rsp, err := w.Write([]byte(`<div class="container">
<div class="message">
<p>You have successfully authenticated with the Whoop API 🎉.</p>
<p>A file was created in the local directory titled <strong>token.json</strong>. Use the button below to close the application.</p> <p> ⚠️ You must manually close this window - Sorry browser security settings 🔐</p>
</div>
<button hx-post="/close" hx-trigger="click" class="close-button">Close CLI Application</button>
</div>`))
if err != nil {
slog.Error("unable to write response", "error", err)
}
slog.Info("Response written", "response", rsp)

}

closeAppHandler := func(w http.ResponseWriter, r *http.Request) {
slog.Info("Closing login application helper")
time.Sleep(2 * time.Second)
w.Write([]byte("Closing application..."))

Check failure on line 92 in cmd/login.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `w.Write` is not checked (errcheck)
os.Exit(0)

}

http.HandleFunc("/", landingPageHandler)
http.HandleFunc("/redirect", redirectHandler)
http.HandleFunc("/submit", submitHandler)
http.HandleFunc("/close", closeAppHandler)

slog.Info("Listening on port 8080. Visit http://localhost:8080 to autenticate with the Whoop API and get an access token.")
err = http.ListenAndServe(":8080", nil)
Expand Down
4 changes: 2 additions & 2 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<link rel="manifest" href="/static/site.webmanifest">
</head>
<body>
<div class="container" role="main" aria-labelledby="loginTitle">
<div class="container" role="main" aria-labelledby="loginTitle" id="main-container">
<h1 id="loginTitle">Login</h1>
<form action="/submit" method="POST" aria-describedby="loginDescription">
<p id="loginDescription">Enter your Whoop username and password to log in.</p>
Expand All @@ -25,7 +25,7 @@ <h1 id="loginTitle">Login</h1>
<label for="password">Password</label>
<input type="password" id="password" name="password" required aria-required="true">
</div>
<button type="submit">Submit</button>
<button type="submit" hx-post="/submit" hx-trigger="click" hx-swap="outerHTML" hx-target="#main-container">Submit</button>
</form>
</div>
</body>
Expand Down
21 changes: 19 additions & 2 deletions html/static/styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ body {
max-width: 600px; /* Increased max-width for the container */
width: 100%;
margin: 0 auto; /* Center the container horizontally */
display: flex; /* Use flexbox for layout */
flex-direction: column; /* Stack children vertically */
}

/* Heading styles */
Expand Down Expand Up @@ -67,6 +69,10 @@ button {
cursor: pointer;
}

button.red {
background-color: #DC3545; /* Red color for the button */
}

button:hover {
background-color: #0056b3;
}
Expand All @@ -81,8 +87,19 @@ button:active {
background-color: #004494;
}


/* Paragraph styles */
p {
margin-bottom: 15px; /* Space after the <p> tag */
}
}

/* Additional styling for the message container */
.message {
margin-bottom: 20px;
flex: 1; /* Grow to fill available space */
}

/* Styling for the close button */
.close-button {
align-self: flex-start; /* Align button to the start (left) */
width: auto; /* Allow button to size based on content */
}

0 comments on commit 64b8c75

Please sign in to comment.