From d6fd8ee974746f3244ca6d2e9232c67ff5b5ae92 Mon Sep 17 00:00:00 2001 From: Aldino Kemal Date: Sat, 13 Jul 2024 22:40:20 +0700 Subject: [PATCH] feat: wip pair code login feat(app): add LoginWithCode method to IAppService interface for handling phone number login feat(app): implement LoginWithCode method in App struct to handle phone number login logic feat(app): implement LoginWithCode method in serviceApp to pair phone number with WhatsApp client --- src/domains/app/app.go | 1 + src/internal/rest/app.go | 14 ++++++++++++++ src/services/app.go | 11 +++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/domains/app/app.go b/src/domains/app/app.go index 8c00e8f..1537fe9 100644 --- a/src/domains/app/app.go +++ b/src/domains/app/app.go @@ -7,6 +7,7 @@ import ( type IAppService interface { Login(ctx context.Context) (response LoginResponse, err error) + LoginWithCode(ctx context.Context, phoneNumber string) (err error) Logout(ctx context.Context) (err error) Reconnect(ctx context.Context) (err error) FirstDevice(ctx context.Context) (response DevicesResponse, err error) diff --git a/src/internal/rest/app.go b/src/internal/rest/app.go index 19775d4..a976c90 100644 --- a/src/internal/rest/app.go +++ b/src/internal/rest/app.go @@ -14,6 +14,7 @@ type App struct { func InitRestApp(app *fiber.App, service domainApp.IAppService) App { rest := App{Service: service} app.Get("/app/login", rest.Login) + app.Get("/app/login-with-code", rest.LoginWithCode) app.Get("/app/logout", rest.Logout) app.Get("/app/reconnect", rest.Reconnect) app.Get("/app/devices", rest.Devices) @@ -36,6 +37,19 @@ func (handler *App) Login(c *fiber.Ctx) error { }) } +func (handler *App) LoginWithCode(c *fiber.Ctx) error { + err := handler.Service.LoginWithCode(c.UserContext(), c.Query("phone")) + utils.PanicIfNeeded(err) + + return c.JSON(utils.ResponseData{ + Status: 200, + Code: "SUCCESS", + Message: "Login with code success", + Results: nil, + }) + +} + func (handler *App) Logout(c *fiber.Ctx) error { err := handler.Service.Logout(c.UserContext()) utils.PanicIfNeeded(err) diff --git a/src/services/app.go b/src/services/app.go index 9cf9d40..d3fef2b 100644 --- a/src/services/app.go +++ b/src/services/app.go @@ -90,6 +90,17 @@ func (service serviceApp) Login(_ context.Context) (response domainApp.LoginResp return response, nil } +func (service serviceApp) LoginWithCode(ctx context.Context, phoneNumber string) (err error) { + phone, err := service.WaCli.PairPhone(phoneNumber, true, whatsmeow.PairClientChrome, "Chrome (Linux)") + if err != nil { + return err + } + + logrus.Info("Phone: ", phone) + + return nil +} + func (service serviceApp) Logout(_ context.Context) (err error) { // delete history files, err := filepath.Glob(fmt.Sprintf("./%s/history-*", config.PathStorages))