Compare commits
No commits in common. "6c6a6dd3296935f02d6c4486b740ca7b5bf45403" and "fe81b57a3457ee9a76678caaafac94d35f366071" have entirely different histories.
6c6a6dd329
...
fe81b57a34
65
api/auth.go
65
api/auth.go
@ -205,7 +205,7 @@ func (api *API) appAuthRegister(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Generate auth hash
|
||||
// Generate Auth Hash
|
||||
rawAuthHash, err := utils.GenerateToken(64)
|
||||
if err != nil {
|
||||
log.Error("Failed to generate user token: ", err)
|
||||
@ -214,41 +214,31 @@ func (api *API) appAuthRegister(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get current users
|
||||
currentUsers, err := api.db.Queries.GetUsers(api.db.Ctx)
|
||||
if err != nil {
|
||||
log.Error("Failed to check all users: ", err)
|
||||
templateVars["Error"] = "Failed to Create User"
|
||||
c.HTML(http.StatusBadRequest, "page/login", templateVars)
|
||||
return
|
||||
}
|
||||
|
||||
// Determine if we should be admin
|
||||
isAdmin := false
|
||||
if len(currentUsers) == 0 {
|
||||
isAdmin = true
|
||||
}
|
||||
|
||||
// Create user in DB
|
||||
// Create User in DB
|
||||
authHash := fmt.Sprintf("%x", rawAuthHash)
|
||||
if rows, err := api.db.Queries.CreateUser(api.db.Ctx, database.CreateUserParams{
|
||||
rows, err := api.db.Queries.CreateUser(api.db.Ctx, database.CreateUserParams{
|
||||
ID: username,
|
||||
Pass: &hashedPassword,
|
||||
AuthHash: &authHash,
|
||||
Admin: isAdmin,
|
||||
}); err != nil {
|
||||
})
|
||||
|
||||
// SQL Error
|
||||
if err != nil {
|
||||
log.Error("CreateUser DB Error:", err)
|
||||
templateVars["Error"] = "Registration Disabled or User Already Exists"
|
||||
c.HTML(http.StatusBadRequest, "page/login", templateVars)
|
||||
return
|
||||
} else if rows == 0 {
|
||||
}
|
||||
|
||||
// User Already Exists
|
||||
if rows == 0 {
|
||||
log.Warn("User Already Exists:", username)
|
||||
templateVars["Error"] = "Registration Disabled or User Already Exists"
|
||||
c.HTML(http.StatusBadRequest, "page/login", templateVars)
|
||||
return
|
||||
}
|
||||
|
||||
// Get user
|
||||
// Get User
|
||||
user, err := api.db.Queries.GetUser(api.db.Ctx, username)
|
||||
if err != nil {
|
||||
log.Error("GetUser DB Error:", err)
|
||||
@ -257,7 +247,7 @@ func (api *API) appAuthRegister(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Set session
|
||||
// Set Session
|
||||
auth := authData{
|
||||
UserName: user.ID,
|
||||
IsAdmin: user.Admin,
|
||||
@ -299,7 +289,6 @@ func (api *API) koAuthRegister(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Generate password hash
|
||||
hashedPassword, err := argon2.CreateHash(rUser.Password, argon2.DefaultParams)
|
||||
if err != nil {
|
||||
log.Error("Argon2 Hash Failure:", err)
|
||||
@ -307,7 +296,7 @@ func (api *API) koAuthRegister(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Generate auth hash
|
||||
// Generate Auth Hash
|
||||
rawAuthHash, err := utils.GenerateToken(64)
|
||||
if err != nil {
|
||||
log.Error("Failed to generate user token: ", err)
|
||||
@ -315,32 +304,20 @@ func (api *API) koAuthRegister(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get current users
|
||||
currentUsers, err := api.db.Queries.GetUsers(api.db.Ctx)
|
||||
if err != nil {
|
||||
log.Error("Failed to check all users: ", err)
|
||||
apiErrorPage(c, http.StatusBadRequest, "Failed to Create User")
|
||||
return
|
||||
}
|
||||
|
||||
// Determine if we should be admin
|
||||
isAdmin := false
|
||||
if len(currentUsers) == 0 {
|
||||
isAdmin = true
|
||||
}
|
||||
|
||||
// Create user
|
||||
authHash := fmt.Sprintf("%x", rawAuthHash)
|
||||
if rows, err := api.db.Queries.CreateUser(api.db.Ctx, database.CreateUserParams{
|
||||
rows, err := api.db.Queries.CreateUser(api.db.Ctx, database.CreateUserParams{
|
||||
ID: rUser.Username,
|
||||
Pass: &hashedPassword,
|
||||
AuthHash: &authHash,
|
||||
Admin: isAdmin,
|
||||
}); err != nil {
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("CreateUser DB Error:", err)
|
||||
apiErrorPage(c, http.StatusBadRequest, "Invalid User Data")
|
||||
return
|
||||
} else if rows == 0 {
|
||||
}
|
||||
|
||||
// User Exists
|
||||
if rows == 0 {
|
||||
log.Error("User Already Exists:", rUser.Username)
|
||||
apiErrorPage(c, http.StatusBadRequest, "User Already Exists")
|
||||
return
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.25.0
|
||||
// sqlc v1.21.0
|
||||
|
||||
package database
|
||||
|
||||
|
@ -1,10 +1,12 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.25.0
|
||||
// sqlc v1.21.0
|
||||
|
||||
package database
|
||||
|
||||
import ()
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Activity struct {
|
||||
ID int64 `json:"id"`
|
||||
@ -119,23 +121,23 @@ type UserStreak struct {
|
||||
}
|
||||
|
||||
type ViewDocumentUserStatistic struct {
|
||||
DocumentID string `json:"document_id"`
|
||||
UserID string `json:"user_id"`
|
||||
Percentage float64 `json:"percentage"`
|
||||
LastRead interface{} `json:"last_read"`
|
||||
ReadPercentage *float64 `json:"read_percentage"`
|
||||
TotalTimeSeconds *float64 `json:"total_time_seconds"`
|
||||
TotalWordsRead interface{} `json:"total_words_read"`
|
||||
TotalWpm int64 `json:"total_wpm"`
|
||||
YearlyTimeSeconds *float64 `json:"yearly_time_seconds"`
|
||||
YearlyWordsRead interface{} `json:"yearly_words_read"`
|
||||
YearlyWpm interface{} `json:"yearly_wpm"`
|
||||
MonthlyTimeSeconds *float64 `json:"monthly_time_seconds"`
|
||||
MonthlyWordsRead interface{} `json:"monthly_words_read"`
|
||||
MonthlyWpm interface{} `json:"monthly_wpm"`
|
||||
WeeklyTimeSeconds *float64 `json:"weekly_time_seconds"`
|
||||
WeeklyWordsRead interface{} `json:"weekly_words_read"`
|
||||
WeeklyWpm interface{} `json:"weekly_wpm"`
|
||||
DocumentID string `json:"document_id"`
|
||||
UserID string `json:"user_id"`
|
||||
Percentage float64 `json:"percentage"`
|
||||
LastRead interface{} `json:"last_read"`
|
||||
ReadPercentage sql.NullFloat64 `json:"read_percentage"`
|
||||
TotalTimeSeconds sql.NullFloat64 `json:"total_time_seconds"`
|
||||
TotalWordsRead interface{} `json:"total_words_read"`
|
||||
TotalWpm int64 `json:"total_wpm"`
|
||||
YearlyTimeSeconds sql.NullFloat64 `json:"yearly_time_seconds"`
|
||||
YearlyWordsRead interface{} `json:"yearly_words_read"`
|
||||
YearlyWpm interface{} `json:"yearly_wpm"`
|
||||
MonthlyTimeSeconds sql.NullFloat64 `json:"monthly_time_seconds"`
|
||||
MonthlyWordsRead interface{} `json:"monthly_words_read"`
|
||||
MonthlyWpm interface{} `json:"monthly_wpm"`
|
||||
WeeklyTimeSeconds sql.NullFloat64 `json:"weekly_time_seconds"`
|
||||
WeeklyWordsRead interface{} `json:"weekly_words_read"`
|
||||
WeeklyWpm interface{} `json:"weekly_wpm"`
|
||||
}
|
||||
|
||||
type ViewUserStreak struct {
|
||||
|
@ -26,8 +26,8 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
RETURNING *;
|
||||
|
||||
-- name: CreateUser :execrows
|
||||
INSERT INTO users (id, pass, auth_hash, admin)
|
||||
VALUES (?, ?, ?, ?)
|
||||
INSERT INTO users (id, pass, auth_hash)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- name: DeleteDocument :execrows
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.25.0
|
||||
// sqlc v1.21.0
|
||||
// source: query.sql
|
||||
|
||||
package database
|
||||
@ -113,8 +113,8 @@ func (q *Queries) AddMetadata(ctx context.Context, arg AddMetadataParams) (Metad
|
||||
}
|
||||
|
||||
const createUser = `-- name: CreateUser :execrows
|
||||
INSERT INTO users (id, pass, auth_hash, admin)
|
||||
VALUES (?, ?, ?, ?)
|
||||
INSERT INTO users (id, pass, auth_hash)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT DO NOTHING
|
||||
`
|
||||
|
||||
@ -122,16 +122,10 @@ type CreateUserParams struct {
|
||||
ID string `json:"id"`
|
||||
Pass *string `json:"-"`
|
||||
AuthHash *string `json:"auth_hash"`
|
||||
Admin bool `json:"-"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (int64, error) {
|
||||
result, err := q.db.ExecContext(ctx, createUser,
|
||||
arg.ID,
|
||||
arg.Pass,
|
||||
arg.AuthHash,
|
||||
arg.Admin,
|
||||
)
|
||||
result, err := q.db.ExecContext(ctx, createUser, arg.ID, arg.Pass, arg.AuthHash)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user