Skip to content

Commit c7c3c93

Browse files
fix(GIST-32): token creation replacement (#11)
1 parent 70b64bc commit c7c3c93

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

organizations/model.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (o *OrganizationSQL) GetByMember(user_id string) ([]Organization, error) {
8585
}
8686

8787
func (o *OrganizationSQL) GetByID(user_id string, org_id string) (*Organization, error) {
88-
query := "SELECT o.org_id, o.name, gists.gist_id FROM organization o JOIN gists ON o.org_id = gists.org_id WHERE o.org_id=$1 AND owner=$2"
88+
query := "SELECT o.org_id, o.name, gists.gist_id FROM organization o JOIN gists ON o.org_id = gists.org_id WHERE o.org_id=$1 AND gists.owner=$2"
8989
log.Info("user is ", user_id)
9090

9191
rows, err := storage.Database.Query(query, org_id, user_id)

user/auth_service.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,27 @@ func (a *AuthServiceImpl) LocalAuth(email string) (TokenSQL, error) {
4949
_, err := token_model.Save()
5050

5151
if err != nil {
52-
return token_model, err
52+
log.Info(err.Error())
53+
if strings.Contains(err.Error(), "token_keyword_key") {
54+
token, err := token_model.GetByType(LocalAuth)
55+
log.Info(token)
56+
57+
if err != nil {
58+
return token_model, err
59+
}
60+
61+
err = token.Delete()
62+
63+
if err != nil {
64+
return token_model, err
65+
}
66+
67+
_, err = token_model.Save()
68+
69+
if err != nil {
70+
return token_model, err
71+
}
72+
}
5373
}
5474

5575
err = utils.SendEmail("Gistapp: Local Auth", "Your token is: "+token_val, email)

user/token_model.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ type Token struct {
3131
func (t *TokenSQL) Save() (*Token, error) {
3232
row, err := storage.Database.Query("INSERT INTO token(type, value, keyword) VALUES ($1, $2, $3) RETURNING token_id, type, value, keyword", t.Type.String, t.Value.String, t.Keyword.String)
3333
if err != nil {
34-
log.Error(err)
35-
return nil, errors.New("couldn't create token")
34+
return nil, err
3635
}
3736
var token Token
3837
row.Next()
@@ -60,6 +59,22 @@ func (t *TokenSQL) Get() (*Token, error) {
6059
return &token, nil
6160
}
6261

62+
func (t *TokenSQL) GetByType(_type TokenType) (*Token, error) {
63+
row, err := storage.Database.Query("SELECT token_id, type, value, keyword FROM token WHERE type = $1", string(_type))
64+
if err != nil {
65+
log.Error(err)
66+
return nil, errors.New("couldn't find token")
67+
}
68+
var token Token
69+
row.Next()
70+
err = row.Scan(&token.ID, &token.Type, &token.Value, &token.Keyword)
71+
if err != nil {
72+
log.Error(err)
73+
return nil, errors.New("couldn't find token")
74+
}
75+
return &token, nil
76+
}
77+
6378
func (t *Token) Delete() error {
6479
_, err := storage.Database.Query("DELETE FROM token WHERE value = $1 and keyword = $2 and type = $3", t.Value, t.Keyword, t.Type)
6580
if err != nil {

0 commit comments

Comments
 (0)