Pass response writer to createSessionCookie()

This commit is contained in:
max 2022-10-21 20:17:11 -05:00
parent df8e8f0426
commit 3417622841

View File

@ -7,12 +7,13 @@ import (
"database/sql" "database/sql"
"encoding/gob" "encoding/gob"
"encoding/hex" "encoding/hex"
"golang.org/x/crypto/bcrypt"
"log" "log"
"math" "math"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
"golang.org/x/crypto/bcrypt"
) )
type User struct { type User struct {
@ -77,7 +78,7 @@ func CreateUser(app *app.App, username string, password string, createdAt time.T
} }
// AuthenticateUser validates the password for the specified user if it matches a session cookie is created and returned // AuthenticateUser validates the password for the specified user if it matches a session cookie is created and returned
func AuthenticateUser(app *app.App, username string, password string) (string, error) { func AuthenticateUser(app *app.App, w http.ResponseWriter, username string, password string) (string, error) {
var hashedPassword []byte var hashedPassword []byte
// Query row by username, scan password column // Query row by username, scan password column
@ -105,12 +106,12 @@ func AuthenticateUser(app *app.App, username string, password string) (string, e
log.Println(err) log.Println(err)
return "", err return "", err
} else { } else {
return createSessionCookie(app, username) return createSessionCookie(app, w, username)
} }
} }
// CreateSessionCookie creates a new session token and cookie and returns the token value // CreateSessionCookie creates a new session token and cookie and returns the token value
func createSessionCookie(app *app.App, username string) (string, error) { func createSessionCookie(app *app.App, w http.ResponseWriter, username string) (string, error) {
// Generate random 64 character string (alpha-numeric) // Generate random 64 character string (alpha-numeric)
buff := make([]byte, int(math.Ceil(float64(64)/2))) buff := make([]byte, int(math.Ceil(float64(64)/2)))
_, err := rand.Read(buff) _, err := rand.Read(buff)