Fixes to password provider

This commit is contained in:
Melon 2025-03-13 23:05:05 +00:00
parent 4300165ae3
commit ba76dc5371
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
3 changed files with 12 additions and 13 deletions

View File

@ -25,14 +25,13 @@ type PasswordLogin struct {
DB passwordLoginDB DB passwordLoginDB
} }
func (b *PasswordLogin) AccessState() process.State { return process.StateBase } func (p *PasswordLogin) AccessState() process.State { return process.StateBase }
func (b *PasswordLogin) Name() string { return "password" } func (p *PasswordLogin) Name() string { return "password" }
func (b *PasswordLogin) RenderTemplate(ctx authContext.TemplateContext) error { func (p *PasswordLogin) RenderTemplate(ctx authContext.TemplateContext) error {
// TODO(melon): rewrite this // TODO(melon): rewrite this
req := ctx.Request() req := ctx.Request()
un := req.FormValue("login")
redirect := req.FormValue("redirect") redirect := req.FormValue("redirect")
if redirect == "" { if redirect == "" {
redirect = "/" redirect = "/"
@ -41,24 +40,24 @@ func (b *PasswordLogin) RenderTemplate(ctx authContext.TemplateContext) error {
UserEmail string UserEmail string
Redirect string Redirect string
}{ }{
UserEmail: un, UserEmail: ctx.LoginProcessData().Email,
Redirect: redirect, Redirect: redirect,
}) })
return nil return nil
} }
func (b *PasswordLogin) AttemptLogin(ctx authContext.FormContext) error { func (p *PasswordLogin) AttemptLogin(ctx authContext.FormContext) error {
req := ctx.Request() req := ctx.Request()
un := req.FormValue("username") un := req.FormValue("email")
pw := req.FormValue("password") pw := req.FormValue("password")
if len(pw) < 8 { if len(pw) < 8 {
return auth.BasicUserSafeError(http.StatusBadRequest, "Password too short") return auth.BasicUserSafeError(http.StatusBadRequest, "Password too short")
} }
login, err := b.DB.CheckLogin(ctx.Context(), un, pw) login, err := p.DB.CheckLogin(ctx.Context(), un, pw)
switch { switch {
case err == nil: case err == nil:
user, err := b.DB.GetUser(ctx.Context(), login.Subject) user, err := p.DB.GetUser(ctx.Context(), login.Subject)
if err != nil { if err != nil {
return err return err
} }

View File

@ -9,7 +9,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
-- name: checkLogin :one -- name: checkLogin :one
SELECT subject, password, need_factor, email, email_verified SELECT subject, password, need_factor, email, email_verified
FROM users FROM users
WHERE users.subject = ? WHERE users.email = ?
LIMIT 1; LIMIT 1;
-- name: GetUser :one -- name: GetUser :one

View File

@ -219,7 +219,7 @@ func (q *Queries) changeUserPassword(ctx context.Context, arg changeUserPassword
const checkLogin = `-- name: checkLogin :one const checkLogin = `-- name: checkLogin :one
SELECT subject, password, need_factor, email, email_verified SELECT subject, password, need_factor, email, email_verified
FROM users FROM users
WHERE users.subject = ? WHERE users.email = ?
LIMIT 1 LIMIT 1
` `
@ -231,8 +231,8 @@ type checkLoginRow struct {
EmailVerified bool `json:"email_verified"` EmailVerified bool `json:"email_verified"`
} }
func (q *Queries) checkLogin(ctx context.Context, subject string) (checkLoginRow, error) { func (q *Queries) checkLogin(ctx context.Context, email string) (checkLoginRow, error) {
row := q.db.QueryRowContext(ctx, checkLogin, subject) row := q.db.QueryRowContext(ctx, checkLogin, email)
var i checkLoginRow var i checkLoginRow
err := row.Scan( err := row.Scan(
&i.Subject, &i.Subject,