Skip to content

Commit

Permalink
chore(back): removed last references to logged
Browse files Browse the repository at this point in the history
  • Loading branch information
yyewolf committed Aug 11, 2023
1 parent 0ea614b commit da61c71
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 63 deletions.
37 changes: 11 additions & 26 deletions backend/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,13 @@ var redirectCache = cache.New(5*time.Minute, 10*time.Minute)
// (GET /account/qr)
func (s *Server) GetAccountQR(c echo.Context) error {
// Get account from cookie
logged := c.Get("userLogged").(bool)
loggedOnBoard := c.Get("onBoardLogged").(bool)
if !logged && !loggedOnBoard {
return ErrorNotAuthenticated(c)
}

var accountID string
var account *models.Account

if logged {
accountID = c.Get("userAccountID").(string)
account = c.Get("userAccount").(*models.Account)
}

if loggedOnBoard {
accountID = c.Get("onBoardAccountID").(string)
account = c.Get("onBoardAccount").(*models.Account)
account, err := MustGetUserOrOnBoard(c)
if err != nil {
return nil
}

var params autogen.GetAccountQRJSONBody
err := c.Bind(&params)
err = c.Bind(&params)
if err != nil {
return Error400(c)
}
Expand All @@ -59,13 +45,13 @@ func (s *Server) GetAccountQR(c echo.Context) error {
return ErrorAccNotFound(c)
}

b64, found := qrCache.Get(accountID)
b64, found := qrCache.Get(account.Id.String())
if !found {
// Generate QR code nonce
nonce := uuid.NewString()

// Cache nonce
qrCache.Set(nonce, accountID, cache.DefaultExpiration)
qrCache.Set(nonce, account.Id.String(), cache.DefaultExpiration)

conf := config.GetConfig()
url := fmt.Sprintf("%s/auth/google/begin/%s", conf.ApiConfig.BasePath, nonce)
Expand All @@ -80,9 +66,9 @@ func (s *Server) GetAccountQR(c echo.Context) error {
return Error500(c)
}
b64 = base64.StdEncoding.EncodeToString(png)
qrCache.Set(accountID, b64, cache.DefaultExpiration)
qrCache.Set(account.Id.String(), b64, cache.DefaultExpiration)

logrus.Debugf("QR code generated for account %s: %s", accountID, url)
logrus.Debugf("QR code generated for account %s: %s", account.Id.String(), url)
}

// Convert to base64
Expand All @@ -97,10 +83,9 @@ func (s *Server) GetAccountQR(c echo.Context) error {

// (GET /account/qr)
func (s *Server) GetAccountQRWebsocket(c echo.Context) error {
logged := c.Get("userLogged").(bool)
loggedOnBoard := c.Get("onBoardLogged").(bool)
if !logged && !loggedOnBoard {
return ErrorNotAuthenticated(c)
_, err := MustGetUserOrOnBoard(c)
if err != nil {
return nil
}

return Upgrade(c)
Expand Down
18 changes: 9 additions & 9 deletions backend/api/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
// (GET /categories)
func (s *Server) GetCategories(c echo.Context) error {
// Get account from cookie
logged := c.Get("userLogged").(bool)
if !logged {
return ErrorNotAuthenticated(c)
_, err := MustGetUser(c)
if err != nil {
return nil
}

data, err := s.DBackend.GetAllCategories(c.Request().Context())
Expand Down Expand Up @@ -122,9 +122,9 @@ func (s *Server) MarkDeleteCategory(c echo.Context, categoryId autogen.UUID) err
// (GET /categories/{category_id})
func (s *Server) GetCategory(c echo.Context, categoryId autogen.UUID) error {
// Get account from cookie
logged := c.Get("userLogged").(bool)
if !logged {
return ErrorNotAuthenticated(c)
_, err := MustGetUser(c)
if err != nil {
return nil
}

category, err := s.DBackend.GetCategory(c.Request().Context(), categoryId.String())
Expand Down Expand Up @@ -198,9 +198,9 @@ func (s *Server) PatchCategory(c echo.Context, categoryId autogen.UUID) error {

// (GET /categories/{category_id}/picture)
func (s *Server) GetCategoryPicture(c echo.Context, categoryId autogen.UUID) error {
logged := c.Get("userLogged").(bool)
if !logged {
return ErrorNotAuthenticated(c)
_, err := MustGetUser(c)
if err != nil {
return nil
}

data, err := storage.GetFile("categories/" + categoryId.String())
Expand Down
6 changes: 3 additions & 3 deletions backend/api/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ func (s *Server) PatchItem(c echo.Context, categoryId autogen.UUID, itemId autog
// (GET /categories/{category_id}/items/{item_id}/picture)
func (s *Server) GetItemPicture(c echo.Context, categoryId autogen.UUID, itemId autogen.UUID) error {
// Get account from cookie
logged := c.Get("userLogged").(bool)
if !logged {
return ErrorNotAuthenticated(c)
_, err := MustGetUser(c)
if err != nil {
return nil
}

data, err := storage.GetFile("items/" + itemId.String())
Expand Down
6 changes: 3 additions & 3 deletions backend/api/refills.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func (s *Server) GetRefills(c echo.Context, params autogen.GetRefillsParams) err
// (GET /account/refills)
func (s *Server) GetSelfRefills(c echo.Context, params autogen.GetSelfRefillsParams) error {
// Get account from cookie
logged := c.Get("userLogged").(bool)
if !logged {
return ErrorNotAuthenticated(c)
_, err := MustGetUser(c)
if err != nil {
return nil
}

accountID := c.Get("userAccountID").(string)
Expand Down
14 changes: 7 additions & 7 deletions backend/api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
// (POST /account/transactions)
func (s *Server) PostTransactions(c echo.Context) error {
// Get account from cookie
logged := c.Get("userLogged").(bool)
if !logged {
return ErrorNotAuthenticated(c)
_, err := MustGetUser(c)
if err != nil {
return nil
}

accountID := c.Get("userAccountID").(string)
Expand All @@ -34,7 +34,7 @@ func (s *Server) PostTransactions(c echo.Context) error {
var fetchedItems = make(map[string]*models.Item)

// Check that pin matches
err := c.Bind(&potentialTransaction)
err = c.Bind(&potentialTransaction)
if err != nil {
logrus.Error(err)
return Error400(c)
Expand Down Expand Up @@ -201,9 +201,9 @@ func (s *Server) GetAccountTransactions(c echo.Context, accountId autogen.UUID,
// (GET /account/transactions)
func (s *Server) GetCurrentAccountTransactions(c echo.Context, params autogen.GetCurrentAccountTransactionsParams) error {
// Get account from cookie
logged := c.Get("userLogged").(bool)
if !logged {
return ErrorNotAuthenticated(c)
_, err := MustGetUser(c)
if err != nil {
return nil
}

accountID := c.Get("userAccountID").(string)
Expand Down
21 changes: 21 additions & 0 deletions backend/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ func (s *Server) RemoveOnBoardCookie(c echo.Context) {
sess.Save(c.Request(), c.Response())
}

func MustGetUserOrOnBoard(c echo.Context) (*models.Account, error) {
logged := c.Get("userLogged").(bool)
loggedOnBoard := c.Get("onBoardLogged").(bool)
if !logged && !loggedOnBoard {
ErrorNotAuthenticated(c)
return nil, errors.New("not authenticated")
}

var account *models.Account

if logged {
account = c.Get("userAccount").(*models.Account)
}

if loggedOnBoard {
account = c.Get("onBoardAccount").(*models.Account)
}

return account, nil
}

func MustGetUser(c echo.Context) (*models.Account, error) {
logged := c.Get("userLogged").(bool)
if !logged {
Expand Down
19 changes: 4 additions & 15 deletions backend/api/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,17 @@ var upgrader = websocket.Upgrader{
}

func Upgrade(c echo.Context) error {
logged := c.Get("userLogged").(bool)
loggedOnBoard := c.Get("onBoardLogged").(bool)
if !logged && !loggedOnBoard {
return ErrorNotAuthenticated(c)
}

var accountID string

if logged {
accountID = c.Get("userAccountID").(string)
}

if loggedOnBoard {
accountID = c.Get("onBoardAccountID").(string)
account, err := MustGetUserOrOnBoard(c)
if err != nil {
return nil
}

conn, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
if err != nil {
return Error500(c)
}

room := GetWSRoom(accountID)
room := GetWSRoom(account.Id.String())
room.Add(conn)

for {
Expand Down

0 comments on commit da61c71

Please sign in to comment.