add login multi api & redirect

This commit is contained in:
2024-06-12 09:22:42 +07:00
parent dd980b4430
commit 9f822c5514
11 changed files with 197 additions and 40 deletions

View File

@@ -1,6 +1,14 @@
package public_services
import "cpone/db"
import (
"bytes"
"cpone/db"
"cpone/models"
"encoding/json"
"fmt"
"io"
"net/http"
)
type ServicesLogin struct {
CompanyStore db.AppStore
@@ -12,3 +20,39 @@ func NewServicesLogin(uStore db.AppStore) *ServicesLogin {
CompanyStore: uStore,
}
}
func (ls *ServicesLogin) Login(username string, password string) (models.ResponseApi, error) {
url := "https://devcpone.aplikasi.web.id/one-api/v1/system/auth/login"
var resp models.ResponseApi
// Create a JSON payload with the username and password
payload := []byte(fmt.Sprintf(`{"username": "%s", "password": "%s"}`, username, password))
// Make the POST request
request, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
if err != nil {
return resp, err
}
if request.StatusCode != http.StatusOK {
return resp, fmt.Errorf("server returned non-OK status: %d", request.StatusCode)
}
defer request.Body.Close()
// Read the response body
body, err := io.ReadAll(request.Body)
if err != nil {
return resp, err
}
if err := json.Unmarshal(body, &resp); err != nil {
return resp, err
}
// Check if status is "ERR"
if resp.Status == "ERR" {
return resp, fmt.Errorf("login failed: %s", resp.Message)
}
return resp, nil
}