-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧹 chore: fiber locals reorg (#905)
- Loading branch information
1 parent
d2b8078
commit a9661bf
Showing
9 changed files
with
80 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package locals | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/GenerateNU/sac/backend/auth" | ||
"github.com/GenerateNU/sac/backend/utilities" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func CustomClaims(c *fiber.Ctx) (*auth.CustomClaims, error) { | ||
rawClaims := c.Locals(claimsKey) | ||
if rawClaims == nil { | ||
return nil, utilities.Forbidden() | ||
} | ||
|
||
claims, ok := rawClaims.(*auth.CustomClaims) | ||
if !ok { | ||
return nil, fmt.Errorf("claims are not of type auth.CustomClaims. got: %T", rawClaims) | ||
} | ||
|
||
return claims, nil | ||
} | ||
|
||
func SetCustomClaims(c *fiber.Ctx, claims *auth.CustomClaims) { | ||
c.Locals(claimsKey, claims) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package locals | ||
|
||
type localsKey byte | ||
|
||
const ( | ||
claimsKey localsKey = 0 | ||
userIDKey localsKey = 1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package locals | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/GenerateNU/sac/backend/utilities" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/google/uuid" | ||
) | ||
|
||
func UserID(c *fiber.Ctx) (*uuid.UUID, error) { | ||
userID := c.Locals(userIDKey) | ||
if userID == nil { | ||
return nil, utilities.Forbidden() | ||
} | ||
|
||
id, ok := userID.(*uuid.UUID) | ||
if !ok { | ||
return nil, fmt.Errorf("userID is not of type uuid.UUID. got: %T", userID) | ||
} | ||
|
||
return id, nil | ||
} | ||
|
||
func SetUserID(c *fiber.Ctx, id *uuid.UUID) { | ||
c.Locals(userIDKey, id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters