Skip to content

Commit

Permalink
feat(auth): enable users to choose a custom server for api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
noetarbouriech committed Jul 8, 2023
1 parent 17e0e47 commit 6c97c7b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/paastech-cloud/cli/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var accountCmd = &cobra.Command{
Expand All @@ -19,6 +20,7 @@ var accountCmd = &cobra.Command{
}

fmt.Println("👤 You are logged in as: " + jwt.Username)
fmt.Println("🌐 Server: " + viper.GetString("server"))
timeDiff := jwt.ExpirationTime.Sub(time.Now())
if timeDiff > 0 {
fmt.Println(
Expand Down
11 changes: 9 additions & 2 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var (
email string
password string
server string
)

var loginCmd = &cobra.Command{
Expand All @@ -37,9 +38,14 @@ var loginCmd = &cobra.Command{
return errors.New("Email and password cannot be empty. Please try again.")
}

// Use PaaSTech API server by default
if server == "" {
server = "https://api.paastech.cloud"
}

// Send login request
fmt.Printf("🔐 Logging in as %s\n", email)
jwt, err := auth.Login(email, password)
jwt, err := auth.Login(server, email, password)
if err != nil {
return err
}
Expand All @@ -51,7 +57,7 @@ var loginCmd = &cobra.Command{
}

// Save jwt in auth conf
config.SetJWT(jwt)
config.SetAuth(server, jwt)
fmt.Println("✅ Login successful")

return nil
Expand All @@ -61,6 +67,7 @@ var loginCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(loginCmd)

loginCmd.Flags().StringVarP(&server, "server", "", "", "Server URL")
loginCmd.Flags().StringVarP(&email, "email", "e", "", "Email")
loginCmd.Flags().StringVarP(&password, "password", "p", "", "Password")
loginCmd.MarkFlagsRequiredTogether("email", "password")
Expand Down
2 changes: 1 addition & 1 deletion cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var logoutCmd = &cobra.Command{
}

// Save empty jwt in auth conf
config.SetJWT("")
config.SetAuth("", "")
fmt.Println("🚪 Logging out")

return nil
Expand Down
6 changes: 4 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ func LoadAuthConfig() error {
}

// Set the jwt in config file
func SetJWT(jwt string) {
func SetAuth(server string, jwt string) {
// Set auth config file as config
authConfigFile()

// Change jwt value in config file
// Change server and jwt value in config file
viper.Set("server", server)
viper.Set("jwt", jwt)
viper.WriteConfig()
}
Expand Down Expand Up @@ -92,6 +93,7 @@ func createAuthConfig(path string) error {

// Write config with no real value
viper.Set("jwt", "")
viper.Set("server", "")
err = viper.WriteConfigAs(path)
if err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/paastech-cloud/cli/pkg/utils"
)

const base_url = "http://localhost:3000/auth"

type loginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
Expand All @@ -20,7 +18,7 @@ type loginResponse struct {
}

// Send a login request to the PaasTech API and returns the Access Token if successful
func Login(email string, password string) (string, error) {
func Login(baseURL string, email string, password string) (string, error) {
// Create JSON request body
request, err := json.Marshal(loginRequest{
Email: email,
Expand All @@ -31,7 +29,7 @@ func Login(email string, password string) (string, error) {
}

// POST request to API
resp, err := http.Post(base_url+"/login", "application/json", bytes.NewReader(request))
resp, err := http.Post(baseURL+"/auth/login", "application/json", bytes.NewReader(request))
if err != nil {
return "", err
}
Expand Down

0 comments on commit 6c97c7b

Please sign in to comment.