Add ability to get session given an AuthToken, fix GetCurrentUser()
This commit is contained in:
parent
1451abcca4
commit
71d3bd77d0
@ -64,6 +64,18 @@ func CreateSession(app *app.App, w http.ResponseWriter, userId int64, remember b
|
|||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSessionByAuthToken(app *app.App, authToken string) (Session, error) {
|
||||||
|
session := Session{}
|
||||||
|
|
||||||
|
err := app.Db.QueryRow(selectSessionByAuthToken, authToken).Scan(&session.Id, &session.UserId, &session.AuthToken, &session.RememberMe, &session.CreatedAt)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error getting session by auth token")
|
||||||
|
return Session{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return session, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Generates a random 64-byte string
|
// Generates a random 64-byte string
|
||||||
func generateAuthToken(app *app.App) string {
|
func generateAuthToken(app *app.App) string {
|
||||||
// Generate random bytes
|
// Generate random bytes
|
||||||
|
@ -37,16 +37,13 @@ func GetCurrentUser(app *app.App, r *http.Request) (User, error) {
|
|||||||
return User{}, err
|
return User{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var userId int64
|
session, err := GetSessionByAuthToken(app, cookie.Value)
|
||||||
|
|
||||||
// Query row by AuthToken
|
|
||||||
err = app.Db.QueryRow(selectSessionIdByAuthToken, cookie.Value).Scan(&userId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error querying session row with session: " + cookie.Value)
|
log.Println("Error getting session by auth token")
|
||||||
return User{}, err
|
return User{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return GetUserById(app, userId)
|
return GetUserById(app, session.UserId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserById 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
|
||||||
|
Loading…
Reference in New Issue
Block a user