From 769a24dff3acd32f21dc0cbe8b53aea27f89f2d4 Mon Sep 17 00:00:00 2001 From: vinchent Date: Tue, 13 Aug 2024 22:46:26 +0200 Subject: [PATCH] Saving token to local storage --- cmd/web/templates/login.page.gohtml | 67 ++++++++++++++++------------- internal/models/tokens.go | 12 +++++- static/js/common.js | 23 +++++----- static/js/login.js | 12 ++++++ static/js/stripe-plan.js | 8 ++-- static/js/stripe.js | 8 ++-- 6 files changed, 78 insertions(+), 52 deletions(-) diff --git a/cmd/web/templates/login.page.gohtml b/cmd/web/templates/login.page.gohtml index 0c6adfb..2ab90bf 100644 --- a/cmd/web/templates/login.page.gohtml +++ b/cmd/web/templates/login.page.gohtml @@ -3,42 +3,47 @@ Login {{ end }} {{ define "content" }} -
-

Login

-
-
- - +
+
+
+ +

Login

+
+
+ + +
+
+ + +
+
+ Login +
-
- - -
-
- Login - +
{{ end }} -{{define "js"}} +{{ define "js" }} -{{end}} +{{ end }} diff --git a/internal/models/tokens.go b/internal/models/tokens.go index 4848dff..81a5ecc 100644 --- a/internal/models/tokens.go +++ b/internal/models/tokens.go @@ -46,11 +46,19 @@ func (m *DBModel) InsertToken(t *Token, u User) error { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - stmt := `INSERT INTO tokens + // delete existing tokens + stmt := `DELETE FROM tokens WHERE user_id = ?` + + _, err := m.DB.ExecContext(ctx, stmt, u.ID) + if err != nil { + return err + } + + stmt = `INSERT INTO tokens (user_id, name, email, token_hash, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)` - _, err := m.DB.ExecContext(ctx, stmt, + _, err = m.DB.ExecContext(ctx, stmt, u.ID, u.LastName, u.Email, diff --git a/static/js/common.js b/static/js/common.js index 028525f..c43bed5 100644 --- a/static/js/common.js +++ b/static/js/common.js @@ -1,6 +1,5 @@ export let card; export let stripe; -const cardMessages = document.getElementById("card-messages"); const payButton = document.getElementById("pay-button"); export const processing = document.getElementById("processing-payment"); @@ -14,18 +13,20 @@ export function showPayButton() { processing.classList.add("d-none"); } -export function showCardError(msg) { - cardMessages.classList.add("alert-danger"); - cardMessages.classList.remove("alert-success"); - cardMessages.classList.remove("d-none"); - cardMessages.innerText = msg; +export function showError(element, msg) { + const messages = document.getElementById(element); + messages.classList.add("alert-danger"); + messages.classList.remove("alert-success"); + messages.classList.remove("d-none"); + messages.innerText = msg; } -export function showCardSuccess() { - cardMessages.classList.add("alert-success"); - cardMessages.classList.remove("alert-danger"); - cardMessages.classList.remove("d-none"); - cardMessages.innerText = "Trasaction successful"; +export function showSuccess(element, msg) { + const messages = document.getElementById(element); + messages.classList.add("alert-success"); + messages.classList.remove("alert-danger"); + messages.classList.remove("d-none"); + messages.innerText = msg; } export function stripeInit(pubKey) { diff --git a/static/js/login.js b/static/js/login.js index a3a2b2f..9ce596a 100644 --- a/static/js/login.js +++ b/static/js/login.js @@ -1,3 +1,8 @@ +import { + showError, + showSuccess, +} from './common.js'; + export function val(api) { let form = document.getElementById("login-form"); @@ -27,6 +32,13 @@ export function val(api) { .then(response => response.json()) .then(response => { console.log(response) + if (response.error === false) { + localStorage.setItem("token", response.authentication_token.token); + localStorage.setItem("token_expiry", response.authentication_token.expiry); + showSuccess("login-messages", "Login successful.") + } else { + showError("login-messages", response.message) + } }); } diff --git a/static/js/stripe-plan.js b/static/js/stripe-plan.js index 048ec77..797e50e 100644 --- a/static/js/stripe-plan.js +++ b/static/js/stripe-plan.js @@ -1,8 +1,8 @@ import { hidePayButton, showPayButton, - showCardError, - showCardSuccess, + showError, + showSuccess, stripe, card, processing, @@ -35,7 +35,7 @@ export function val(plan_id, api) { function stripePaymentMethodHandler(result, plan_id, api) { if (result.error) { - showCardError(result.error.message); + showError("card-messages", result.error.message); } else { // create a customer and subscribe to plan let payload = { @@ -66,7 +66,7 @@ function stripePaymentMethodHandler(result, plan_id, api) { .then(function (data) { console.log(data); processing.classList.add("d-none"); - showCardSuccess(); + showSuccess("card-messages", "Transaction successful!"); sessionStorage.first_name = document.getElementById("first-name").value; sessionStorage.last_name = document.getElementById("last-name").value; sessionStorage.amount = document.getElementById("amount").value; diff --git a/static/js/stripe.js b/static/js/stripe.js index 4fa2c85..da34caa 100644 --- a/static/js/stripe.js +++ b/static/js/stripe.js @@ -1,8 +1,8 @@ import { hidePayButton, showPayButton, - showCardError, - showCardSuccess, + showError, + showSuccess, stripe, card, processing, @@ -52,7 +52,7 @@ export function val(api) { }).then(function (result) { if (result.error) { // card declined, or sth went wrong with the card - showCardError(result.error.message); + showError("card-messages", result.error.message); showPayButton(); } else if (result.paymentIntent) { if (result.paymentIntent.status === "succeeded") { @@ -62,7 +62,7 @@ export function val(api) { document.getElementById("payment_amount").value = result.paymentIntent.amount; document.getElementById("payment_currency").value = result.paymentIntent.currency; processing.classList.add("d-none"); - showCardSuccess(); + showSuccess("card-messages", "Trasaction successful!"); document.getElementById("charge_form").submit(); } }