Skip to content

Commit

Permalink
fix: user login creation of identity file
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Ramos committed Aug 5, 2022
1 parent aeade3e commit 2556b91
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
"args": [
"login",
"--email",
"miguel@websublime.dev",
"miguel.ramos@websublime.dev",
"--password",
"my_secret_password"
"secret_password"
]
},
{
Expand Down
69 changes: 69 additions & 0 deletions api/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright © 2022 Websublime.dev organization@websublime.dev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package api

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"

"github.com/websublime/sublime-cli/models"
)

func (ctx *Supabase) GetUser(token string) (models.User, error) {
model := models.User{}

uri := fmt.Sprintf("%s/%s/user", ctx.BaseURL, AuthEndpoint)

req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return model, err
}
req.Header.Add("Content-Type", "application/json; charset=UTF-8")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Add("apikey", ctx.ApiKey)

response, err := ctx.HTTPClient.Do(req)
if err != nil {
return model, err
}

defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
if err != nil {
return model, err
}

if response.StatusCode >= 400 {
return model, errors.New(string(body))
}

err = json.Unmarshal(body, &model)
if err != nil {
return model, err
}

return model, err
}
21 changes: 18 additions & 3 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func init() {
loginFlags := &LoginFlags{}
loginCmd := NewLoginCmd(loginFlags)

//loginCmd.Flags().StringVar(&loginFlags.Email, "email", "", "Email")
//loginCmd.MarkFlagRequired("email")

//loginCmd.Flags().StringVar(&loginFlags.Password, "password", "", "Password")
//loginCmd.MarkFlagRequired("password")

rootCommand.AddCommand(loginCmd)
}

Expand Down Expand Up @@ -109,11 +115,20 @@ func (ctx *LoginFlags) LoginAuthor() {

author.Expires = expires

user, err := supabase.GetUser(author.Token)
if err != nil {
ctx.CommandError(err.Error(), utils.ErrorInvalidAuthor)
}

config.UpdateProgress(utils.MessageCommandLoginAuthor, 2)
err = app.UpdateAuthorMetadata(&models.AuthorFileProps{
Expire: author.Expires,
Token: author.Token,
Refresh: author.RefreshToken,
Expire: author.Expires,
Token: author.Token,
Refresh: author.RefreshToken,
Name: user.UserMetadata.Name,
Username: user.UserMetadata.Author,
Email: user.Email,
ID: user.ID,
})
if err != nil {
ctx.CommandError(err.Error(), utils.ErrorInvalidAuthor)
Expand Down
15 changes: 12 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,19 @@ func executeTokenExpirationValidation() {

refresh.Expires = expires

user, err := supabase.GetUser(refresh.Token)
if err != nil {
utils.ErrorOut(err.Error(), utils.ErrorInvalidAuthor)
}

err = app.UpdateAuthorMetadata(&models.AuthorFileProps{
Expire: refresh.Expires,
Token: refresh.Token,
Refresh: refresh.RefreshToken,
Expire: refresh.Expires,
Token: refresh.Token,
Refresh: refresh.RefreshToken,
Name: user.UserMetadata.Name,
Username: user.UserMetadata.Author,
Email: user.Email,
ID: user.ID,
})
if err != nil {
utils.ErrorOut(err.Error(), utils.ErrorInvalidAuthor)
Expand Down
17 changes: 3 additions & 14 deletions core/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,12 @@ func (ctx *App) InitAuthor() error {
func (ctx *App) UpdateAuthorMetadata(author *models.AuthorFileProps) error {
config := GetConfig()
rcFile := filepath.Join(config.HomeDir, ".sublime/rc.json")
rcJson, err := os.ReadFile(rcFile)
_, err := os.ReadFile(rcFile)
if err != nil {
return errors.New(utils.MessageErrorAuthorFileMissing)
utils.WarningOut(utils.MessageErrorAuthorFileMissing)
}

authorMetadata := models.AuthorFileProps{}

err = json.Unmarshal(rcJson, &authorMetadata)
if err != nil {
return errors.New(utils.MessageErrorParseFile)
}

authorMetadata.Token = author.Token
authorMetadata.Expire = author.Expire
authorMetadata.Refresh = author.Refresh

data, err := json.MarshalIndent(authorMetadata, "", " ")
data, err := json.MarshalIndent(author, "", " ")
if err != nil {
return errors.New(utils.MessageErrorIndentFile)
}
Expand Down
18 changes: 18 additions & 0 deletions models/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,21 @@ type AuthorFileProps struct {
Expire int64 `json:"expire"`
Refresh string `json:"refresh"`
}

type User struct {
EmailConfirmedAt string `json:"email_confirmed_at"`
Phone string `json:"phone"`
LastSignInAt string `json:"last_sign_in_at"`
Identities []struct {
ID string `json:"id"`
UserID string `json:"user_id"`
} `json:"identities"`
ConfirmedAt string `json:"confirmed_at"`
Email string `json:"email"`
UserMetadata struct {
Author string `json:"author"`
Name string `json:"name"`
} `json:"user_metadata"`
ID string `json:"id"`
Role string `json:"role"`
}

0 comments on commit 2556b91

Please sign in to comment.