Add e-mail based login

This commit is contained in:
2025-03-25 17:58:26 +01:00
parent 7aff2a2ead
commit 3fe0b43372
17 changed files with 499 additions and 305 deletions

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"html/template"
"net/http"
"time"
"fornaxian.tech/log"
"fornaxian.tech/pixeldrain_api_client/pixelapi"
@@ -70,185 +69,6 @@ func (wc *WebController) serveLogout(
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form) {
var err error
// This only runs on the first request
if wc.captchaSiteKey == "" {
capt, err := td.PixelAPI.GetMiscRecaptcha()
if err != nil {
log.Error("Error getting recaptcha key: %s", err)
f.SubmitMessages = []template.HTML{
"An internal server error had occurred. Registration is " +
"unavailable at the moment. Please return later",
}
return f
}
if capt.SiteKey == "" {
wc.captchaSiteKey = "none"
} else {
wc.captchaSiteKey = capt.SiteKey
}
}
// Construct the form
f = Form{
Name: "register",
Title: "Register a new pixeldrain account",
Fields: []Field{
{
Name: "username",
Label: "Username",
Description: "used for logging into your account",
Type: FieldTypeUsername,
}, {
Name: "email",
Label: "E-mail address",
Description: `not required. your e-mail address will only be
used for password resets and important account
notifications`,
Type: FieldTypeEmail,
}, {
Name: "password",
Label: "Password",
Type: FieldTypeNewPassword,
}, {
Name: "password2",
Label: "Password verification",
Description: "you need to enter your password twice so we " +
"can verify that no typing errors were made, which would " +
"prevent you from logging into your new account",
Type: FieldTypeNewPassword,
},
},
SubmitLabel: "Register",
}
if f.ReadInput(r) {
if f.FieldVal("password") != f.FieldVal("password2") {
f.SubmitMessages = []template.HTML{
"Password verification failed. Please enter the same " +
"password in both password fields"}
return f
}
// Register the user
if err = td.PixelAPI.UserRegister(
f.FieldVal("username"),
f.FieldVal("email"),
f.FieldVal("password"),
); err != nil {
formAPIError(err, &f)
return f
}
// Registration successful. Log the user in
session, err := td.PixelAPI.PostUserLogin(
f.FieldVal("username"),
f.FieldVal("password"),
"website login",
)
if err != nil {
log.Debug("Login failed: %s", err)
formAPIError(err, &f)
return
}
f.Extra.SetCookie = wc.sessionCookie(session)
f.Extra.RedirectTo = wc.loginRedirect(r)
// Request was a success
f.SubmitSuccess = true
f.SubmitMessages = []template.HTML{
`Registration completed! You can now <a href="/login">log in ` +
`to your account</a>.<br/>We're glad to have you on ` +
`board, have fun sharing!`}
}
return f
}
func (wc *WebController) loginForm(td *TemplateData, r *http.Request) (f Form) {
f = Form{
Name: "login",
Title: "Log in to your pixeldrain account",
Fields: []Field{
{
Name: "username",
Label: "Username",
Type: FieldTypeUsername,
}, {
Name: "password",
Label: "Password",
Type: FieldTypeCurrentPassword,
},
},
SubmitLabel: "Login",
PostFormHTML: template.HTML(
`<p>If you don't have a pixeldrain account yet, you can ` +
`<a href="/register">register here</a>. No e-mail address is ` +
`required.</p>` +
`<p>Forgot your password? If your account has a valid e-mail ` +
`address you can <a href="/password_reset">request a new ` +
`password here</a>.</p>`,
),
}
// If the user is already logged in we redirect to the target page
// immediately
if td.Authenticated {
f.Extra.RedirectTo = wc.loginRedirect(r)
return f
}
if f.ReadInput(r) {
if session, err := td.PixelAPI.PostUserLogin(
f.FieldVal("username"),
f.FieldVal("password"),
"website login",
); err != nil {
log.Debug("Login failed: %s", err)
formAPIError(err, &f)
} else {
// Request was a success
f.SubmitSuccess = true
f.SubmitMessages = []template.HTML{"Success!"}
// Set the autentication cookie
f.Extra.SetCookie = wc.sessionCookie(session)
f.Extra.RedirectTo = wc.loginRedirect(r)
}
}
return f
}
func (wc *WebController) sessionCookie(session pixelapi.UserSession) *http.Cookie {
return &http.Cookie{
Name: "pd_auth_key",
Value: session.AuthKey.String(),
Path: "/",
Expires: time.Now().AddDate(50, 0, 0),
Domain: wc.config.SessionCookieDomain,
// Strict means the Cookie will only be sent when the user
// reaches a page by a link from the same domain. Lax means any
// page on the domain gets the cookie and None means embedded
// content also gets the cookie.
//
// Users who see pixeldrain links in iframes also expect their
// accounts to be logged in so we need to use None
SameSite: http.SameSiteNoneMode,
Secure: true,
}
}
func (wc *WebController) loginRedirect(r *http.Request) string {
if redirect := r.URL.Query().Get("redirect"); redirect == "checkout" {
return "/user/prepaid/deposit#deposit"
} else {
return "/user"
}
}
func (wc *WebController) passwordResetForm(td *TemplateData, r *http.Request) (f Form) {
f = Form{
Name: "password_reset",