Skip to content

Commit

Permalink
Add change-password CLI functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardolat committed Aug 5, 2024
1 parent f59f492 commit 1501fdd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
73 changes: 73 additions & 0 deletions cmd/changepw/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"context"
"database/sql"
"errors"
"fmt"

"github.com/eduardolat/pgbackweb/internal/config"
"github.com/eduardolat/pgbackweb/internal/database"
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/eduardolat/pgbackweb/internal/util/cryptoutil"
"github.com/google/uuid"
)

func main() {
env := config.GetEnv()

db := database.Connect(env)
defer db.Close()
dbg := dbgen.New(db)

fmt.Println()
fmt.Println()
fmt.Println("PG Back Web - Password Reset")
fmt.Println("---")
fmt.Print("User email: ")
var userID uuid.UUID

for {
var email string
if _, err := fmt.Scanln(&email); err != nil {
panic(err)
}

user, err := dbg.UsersServiceGetUserByEmail(
context.Background(), email,
)
if err != nil && errors.Is(err, sql.ErrNoRows) {
fmt.Print("User not found. Enter new email: ")
continue
}
if err != nil {
panic(err)
}

userID = user.ID
break
}

newPassword := uuid.NewString()
hashedPassword, err := cryptoutil.CreateBcryptHash(newPassword)
if err != nil {
panic(err)
}

err = dbg.UsersServiceChangePassword(
context.Background(), dbgen.UsersServiceChangePasswordParams{
ID: userID,
Password: hashedPassword,
},
)
if err != nil {
panic(err)
}

fmt.Println()
fmt.Println("Password reset successfully")
fmt.Println("New password: ", newPassword)
fmt.Println()
fmt.Println("You can change your password after login")
fmt.Println()
}
4 changes: 4 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ COPY . .
# Build the app
RUN task build

# Copy change-password binary
RUN cp ./dist/change-password /usr/local/bin/change-password && \
chmod 777 /usr/local/bin/change-password

# Run the app
EXPOSE 8085
CMD ["task", "migrate-serve"]
1 change: 1 addition & 0 deletions taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tasks:
cmds:
- task fmt
- go build -o ./dist/app ./cmd/app/.
- go build -o ./dist/change-password ./cmd/changepw/.

serve:
cmd: ./dist/app
Expand Down

0 comments on commit 1501fdd

Please sign in to comment.