Compare commits
No commits in common. "c82cdb4f13876a329975a1e6de8c840617b9f058" and "ab1b82c680e01bd45086b023ce83029ee9fe9eb2" have entirely different histories.
c82cdb4f13
...
ab1b82c680
2
go.mod
2
go.mod
@ -4,5 +4,5 @@ go 1.21
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/lib/pq v1.10.9
|
github.com/lib/pq v1.10.9
|
||||||
golang.org/x/crypto v0.17.0
|
golang.org/x/crypto v0.14.0
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -1,4 +1,4 @@
|
|||||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
|
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||||
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
|
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||||
|
@ -62,7 +62,7 @@ func CreateSession(app *app.App, w http.ResponseWriter, userId int64, remember b
|
|||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SessionByAuthToken(app *app.App, authToken string) (Session, error) {
|
func GetSessionByAuthToken(app *app.App, authToken string) (Session, error) {
|
||||||
session := Session{}
|
session := Session{}
|
||||||
|
|
||||||
err := app.Db.QueryRow(selectSessionByAuthToken, authToken).Scan(&session.Id, &session.UserId, &session.AuthToken, &session.RememberMe, &session.CreatedAt)
|
err := app.Db.QueryRow(selectSessionByAuthToken, authToken).Scan(&session.Id, &session.UserId, &session.AuthToken, &session.RememberMe, &session.CreatedAt)
|
||||||
|
@ -27,23 +27,23 @@ const (
|
|||||||
insertUser = "INSERT INTO " + userTable + " (" + userColumnsNoId + ") VALUES ($1, $2, $3, $4) RETURNING \"Id\""
|
insertUser = "INSERT INTO " + userTable + " (" + userColumnsNoId + ") VALUES ($1, $2, $3, $4) RETURNING \"Id\""
|
||||||
)
|
)
|
||||||
|
|
||||||
// CurrentUser finds the currently logged-in user by session cookie
|
// GetCurrentUser finds the currently logged-in user by session cookie
|
||||||
func CurrentUser(app *app.App, r *http.Request) (User, error) {
|
func GetCurrentUser(app *app.App, r *http.Request) (User, error) {
|
||||||
cookie, err := r.Cookie("session")
|
cookie, err := r.Cookie("session")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return User{}, err
|
return User{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := SessionByAuthToken(app, cookie.Value)
|
session, err := GetSessionByAuthToken(app, cookie.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return User{}, err
|
return User{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return UserById(app, session.UserId)
|
return GetUserById(app, session.UserId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserById finds a User table row in the database by id and returns a struct representing this row
|
// GetUserById finds a User table row in the database by id and returns a struct representing this row
|
||||||
func UserById(app *app.App, id int64) (User, error) {
|
func GetUserById(app *app.App, id int64) (User, error) {
|
||||||
user := User{}
|
user := User{}
|
||||||
|
|
||||||
err := app.Db.QueryRow(selectUserById, id).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
|
err := app.Db.QueryRow(selectUserById, id).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
|
||||||
@ -54,8 +54,8 @@ func UserById(app *app.App, id int64) (User, error) {
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserByUsername finds a User table row in the database by username and returns a struct representing this row
|
// GetUserByUsername finds a User table row in the database by username and returns a struct representing this row
|
||||||
func UserByUsername(app *app.App, username string) (User, error) {
|
func GetUserByUsername(app *app.App, username string) (User, error) {
|
||||||
user := User{}
|
user := User{}
|
||||||
|
|
||||||
err := app.Db.QueryRow(selectUserByUsername, username).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
|
err := app.Db.QueryRow(selectUserByUsername, username).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
|
||||||
@ -82,7 +82,7 @@ func CreateUser(app *app.App, username string, password string, createdAt time.T
|
|||||||
return User{}, err
|
return User{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return UserById(app, lastInsertId)
|
return GetUserById(app, lastInsertId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthenticateUser validates the password for the specified user
|
// AuthenticateUser validates the password for the specified user
|
||||||
|
Loading…
Reference in New Issue
Block a user