GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
feat(device): implement per-device login (QR & code) endpoints
Aldino Kemald1ac267f3347
diff --git a/readme.md b/readme.mdindex 6ebbe215..43c35630 100644--- a/readme.md+++ b/readme.md@@ -583,8 +583,8 @@ You can fork or edit this source code !| ✅ | Add Device | POST | /devices || ✅ | Get Device Info | GET | /devices/:device_id || ✅ | Remove Device | DELETE | /devices/:device_id |-| ❌ | Login Device (QR, reserved) | GET | /devices/:device_id/login |-| ❌ | Login Device (Code, reserved) | POST | /devices/:device_id/login/code |+| ✅ | Login Device (QR) | GET | /devices/:device_id/login |+| ✅ | Login Device (Code) | POST | /devices/:device_id/login/code || ✅ | Logout Device | POST | /devices/:device_id/logout || ✅ | Reconnect Device | POST | /devices/:device_id/reconnect || ✅ | Get Device Status | GET | /devices/:device_id/status |@@ -671,7 +671,6 @@ You can fork or edit this source code !- `*User My Groups`: Returns a maximum of 500 groups due to WhatsApp protocol limitation. This is enforced by WhatsApp servers, not this API. See [whatsmeow source](https://github.com/tulir/whatsmeow/blob/main/group.go) for details.- `/health` is public and always registered at the root path, even when `APP_BASE_PATH` is set.-- Device-specific login routes are reserved by the router but not implemented by the current usecase. Use `/app/login` or `/app/login-with-code` with `X-Device-Id` or `device_id` to pair a specific slot.- Chatwoot routes are registered only when `CHATWOOT_ENABLED=true`.## User Interfacediff --git a/src/cmd/root.go b/src/cmd/root.goindex 23c528d3..73c6ec32 100644--- a/src/cmd/root.go+++ b/src/cmd/root.go@@ -617,7 +617,7 @@ func initApp() {messageUsecase = usecase.NewMessageService(chatStorageRepo)groupUsecase = usecase.NewGroupService()newsletterUsecase = usecase.NewNewsletterService()- deviceUsecase = usecase.NewDeviceService(dm)+ deviceUsecase = usecase.NewDeviceService(dm, appUsecase)}// Execute adds all child commands to the root command and sets flags appropriately.diff --git a/src/domains/device/interfaces.go b/src/domains/device/interfaces.goindex e355e8e7..0b907ff5 100644--- a/src/domains/device/interfaces.go+++ b/src/domains/device/interfaces.go@@ -3,6 +3,7 @@ package deviceimport ("context"+ "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/app""github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage")@@ -14,7 +15,7 @@ type IDeviceUsecase interface {// webhook configuration (url, secret, events, insecure_skip_verify) at creation time.AddDevice(ctx context.Context, deviceID string, webhook *chatstorage.DeviceWebhookConfig) (*Device, error)RemoveDevice(ctx context.Context, deviceID string) error- LoginDevice(ctx context.Context, deviceID string) error+ LoginDevice(ctx context.Context, deviceID string) (app.LoginResponse, error)LoginDeviceWithCode(ctx context.Context, deviceID string, phone string) (string, error)LogoutDevice(ctx context.Context, deviceID string) errorReconnectDevice(ctx context.Context, deviceID string) errordiff --git a/src/ui/rest/device.go b/src/ui/rest/device.goindex a3a1f0d1..30078168 100644--- a/src/ui/rest/device.go+++ b/src/ui/rest/device.go@@ -1,6 +1,9 @@package restimport (+ "fmt"++ "github.com/aldinokemal/go-whatsapp-web-multidevice/config""github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage""github.com/aldinokemal/go-whatsapp-web-multidevice/domains/device""github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/utils"@@ -124,14 +127,18 @@ func (handler *Device) RemoveDevice(c *fiber.Ctx) error {func (handler *Device) LoginDevice(c *fiber.Ctx) error {deviceID := c.Params("device_id")- err := handler.Service.LoginDevice(c.UserContext(), deviceID)+ response, err := handler.Service.LoginDevice(c.UserContext(), deviceID)utils.PanicIfNeeded(err)return c.JSON(utils.ResponseData{Status: 200,Code: "SUCCESS",- Message: "Login started",- Results: map[string]any{"device_id": deviceID},+ Message: "Login success",+ Results: map[string]any{+ "device_id": deviceID,+ "qr_link": fmt.Sprintf("%s://%s%s/%s", c.Protocol(), c.Hostname(), config.AppBasePath, response.ImagePath),+ "qr_duration": response.Duration,+ },})}diff --git a/src/usecase/device.go b/src/usecase/device.goindex 59570714..23277ae8 100644--- a/src/usecase/device.go+++ b/src/usecase/device.go@@ -4,6 +4,7 @@ import ("context""fmt"+ domainApp "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/app""github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage"domainDevice "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/device""github.com/aldinokemal/go-whatsapp-web-multidevice/infrastructure/whatsapp"@@ -15,11 +16,13 @@ import (type serviceDevice struct {manager *whatsapp.DeviceManager+ app domainApp.IAppUsecase}-func NewDeviceService(manager *whatsapp.DeviceManager) domainDevice.IDeviceUsecase {+func NewDeviceService(manager *whatsapp.DeviceManager, app domainApp.IAppUsecase) domainDevice.IDeviceUsecase {return &serviceDevice{manager: manager,+ app: app,}}@@ -95,12 +98,18 @@ func (s *serviceDevice) RemoveDevice(ctx context.Context, deviceID string) errorreturn nil}-func (s *serviceDevice) LoginDevice(_ context.Context, _ string) error {- return fmt.Errorf("device login per ID is not implemented yet")+func (s *serviceDevice) LoginDevice(ctx context.Context, deviceID string) (domainApp.LoginResponse, error) {+ if s.app == nil {+ return domainApp.LoginResponse{}, fmt.Errorf("app usecase not initialized")+ }+ return s.app.Login(ctx, deviceID)}-func (s *serviceDevice) LoginDeviceWithCode(_ context.Context, _ string, _ string) (string, error) {- return "", fmt.Errorf("device login with code is not implemented yet")+func (s *serviceDevice) LoginDeviceWithCode(ctx context.Context, deviceID string, phone string) (string, error) {+ if s.app == nil {+ return "", fmt.Errorf("app usecase not initialized")+ }+ return s.app.LoginWithCode(ctx, deviceID, phone)}func (s *serviceDevice) LogoutDevice(ctx context.Context, deviceID string) error {