Skip to content

Commit 1501fdd

Browse files
committed
Add change-password CLI functionality
1 parent f59f492 commit 1501fdd

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

cmd/changepw/main.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"errors"
7+
"fmt"
8+
9+
"github.com/eduardolat/pgbackweb/internal/config"
10+
"github.com/eduardolat/pgbackweb/internal/database"
11+
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
12+
"github.com/eduardolat/pgbackweb/internal/util/cryptoutil"
13+
"github.com/google/uuid"
14+
)
15+
16+
func main() {
17+
env := config.GetEnv()
18+
19+
db := database.Connect(env)
20+
defer db.Close()
21+
dbg := dbgen.New(db)
22+
23+
fmt.Println()
24+
fmt.Println()
25+
fmt.Println("PG Back Web - Password Reset")
26+
fmt.Println("---")
27+
fmt.Print("User email: ")
28+
var userID uuid.UUID
29+
30+
for {
31+
var email string
32+
if _, err := fmt.Scanln(&email); err != nil {
33+
panic(err)
34+
}
35+
36+
user, err := dbg.UsersServiceGetUserByEmail(
37+
context.Background(), email,
38+
)
39+
if err != nil && errors.Is(err, sql.ErrNoRows) {
40+
fmt.Print("User not found. Enter new email: ")
41+
continue
42+
}
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
userID = user.ID
48+
break
49+
}
50+
51+
newPassword := uuid.NewString()
52+
hashedPassword, err := cryptoutil.CreateBcryptHash(newPassword)
53+
if err != nil {
54+
panic(err)
55+
}
56+
57+
err = dbg.UsersServiceChangePassword(
58+
context.Background(), dbgen.UsersServiceChangePasswordParams{
59+
ID: userID,
60+
Password: hashedPassword,
61+
},
62+
)
63+
if err != nil {
64+
panic(err)
65+
}
66+
67+
fmt.Println()
68+
fmt.Println("Password reset successfully")
69+
fmt.Println("New password: ", newPassword)
70+
fmt.Println()
71+
fmt.Println("You can change your password after login")
72+
fmt.Println()
73+
}

docker/Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ COPY . .
103103
# Build the app
104104
RUN task build
105105

106+
# Copy change-password binary
107+
RUN cp ./dist/change-password /usr/local/bin/change-password && \
108+
chmod 777 /usr/local/bin/change-password
109+
106110
# Run the app
107111
EXPOSE 8085
108112
CMD ["task", "migrate-serve"]

taskfile.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ tasks:
2222
cmds:
2323
- task fmt
2424
- go build -o ./dist/app ./cmd/app/.
25+
- go build -o ./dist/change-password ./cmd/changepw/.
2526

2627
serve:
2728
cmd: ./dist/app

0 commit comments

Comments
 (0)