Signed-off-by: Nikolai Rodionov <allanger@badhouseplants.net>
This commit was merged in pull request #5.
This commit is contained in:
@@ -32,28 +32,50 @@ type JWT struct {
|
||||
type AccountParams struct{}
|
||||
|
||||
type AccountData struct {
|
||||
Username string
|
||||
Password string
|
||||
Email string
|
||||
UUID string
|
||||
}
|
||||
|
||||
func (c *AccountController) Create(ctx context.Context, data *AccountData) (string, error) {
|
||||
log := logger.FromContext(ctx)
|
||||
data.UUID = uuid.New().String()
|
||||
|
||||
passwordHash, err := hash.HashPassword(data.Password, int(c.HashCost))
|
||||
if err != nil {
|
||||
log.Error(err, "Couldn't crate the password hash")
|
||||
return "", nil
|
||||
}
|
||||
|
||||
query := "INSERT INTO users (uuid, username, email, password_hash) VALUES ($1, $2, $3, $4)"
|
||||
if _, err := c.DB.Query(query, data.UUID, data.Username, data.Email, passwordHash); err != nil {
|
||||
query := "INSERT INTO accounts (uuid, email, password_hash) VALUES ($1, $2, $3)"
|
||||
if _, err := c.DB.Query(query, data.UUID, data.Email, passwordHash); err != nil {
|
||||
log.Error(err, "Couldn't create a user in the database")
|
||||
return "", err
|
||||
}
|
||||
|
||||
return data.UUID, nil
|
||||
}
|
||||
|
||||
func (c *AccountController) Login(ctx context.Context, email, password string) (string, error) {
|
||||
log := logger.FromContext(ctx)
|
||||
query := "SELECT uuid, password_hash FROM accounts WHERE email = $1;"
|
||||
|
||||
var passwordHash string
|
||||
var uuid string
|
||||
|
||||
if err := c.DB.QueryRow(query, email).Scan(&uuid, &passwordHash); err != nil {
|
||||
log.Error(err, "Couldn't get a user from the database")
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err := hash.CheckPasswordHash(password, passwordHash); err != nil {
|
||||
log.Error(err, "Wrong password")
|
||||
return "", err
|
||||
}
|
||||
|
||||
return uuid, nil
|
||||
}
|
||||
|
||||
func (c *AccountController) GenerateAccessToken(userID string) (string, error) {
|
||||
claims := jwt.MapClaims{
|
||||
"user_id": userID,
|
||||
@@ -95,8 +117,6 @@ func (c *AccountController) ValidateRefreshToken(ctx context.Context, tokenID, u
|
||||
log.Error(err, "Couldn't delete redis entry")
|
||||
return "", err
|
||||
}
|
||||
log.Info(userIDRedis)
|
||||
log.Info(userID)
|
||||
if userID != userIDRedis {
|
||||
return "", errors.New("user id doesn't match")
|
||||
}
|
||||
|
||||
@@ -68,7 +68,6 @@ func (app *Application) Create(ctx context.Context) error {
|
||||
}
|
||||
|
||||
controller, err := ctrl.NewManager(conf, ctrl.Options{})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -90,7 +89,7 @@ func (app *Application) Create(ctx context.Context) error {
|
||||
)[0:20]
|
||||
|
||||
goPath := os.TempDir() + "/softplayer/" + formattedName
|
||||
if err := os.MkdirAll(goPath, 0777); err != nil {
|
||||
if err := os.MkdirAll(goPath, 0o777); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user