-
Notifications
You must be signed in to change notification settings - Fork 0
[users] add module #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| -- +goose Up | ||
| -- +goose StatementBegin | ||
| CREATE TABLE IF NOT EXISTS `users` ( | ||
| `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, | ||
| `email` VARCHAR(255) NOT NULL, | ||
|
capcom6 marked this conversation as resolved.
|
||
| `password_hash` VARCHAR(255) NOT NULL, | ||
| `role` ENUM('admin', 'user') NOT NULL DEFAULT 'user', | ||
| `status` ENUM('pending', 'active', 'blocked') NOT NULL DEFAULT 'pending', | ||
| `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
| PRIMARY KEY (`id`), | ||
| UNIQUE KEY `idx_users_email` (`email`) | ||
| ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci; | ||
| -- +goose StatementEnd | ||
| --- | ||
|
capcom6 marked this conversation as resolved.
|
||
| -- +goose Down | ||
| -- +goose StatementBegin | ||
| DROP TABLE IF EXISTS `users`; | ||
| -- +goose StatementEnd | ||
This file contains hidden or 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,32 @@ | ||
| package jwt | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
| ) | ||
|
|
||
| const ( | ||
| minSecretLength = 32 | ||
| ) | ||
|
|
||
| type Config struct { | ||
| Secret string | ||
| AccessTTL time.Duration | ||
| Issuer string | ||
| } | ||
|
|
||
| func (c Config) Validate() error { | ||
| if c.Secret == "" { | ||
| return fmt.Errorf("%w: secret is required", ErrInvalidConfig) | ||
| } | ||
|
|
||
| if len(c.Secret) < minSecretLength { | ||
| return fmt.Errorf("%w: secret must be at least %d bytes", ErrInvalidConfig, minSecretLength) | ||
| } | ||
|
|
||
| if c.AccessTTL <= 0 { | ||
| return fmt.Errorf("%w: access ttl must be positive", ErrInvalidConfig) | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or 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 jwt provides JWT token generation and validation for the application. | ||
| // | ||
| // The module covers: | ||
| // - token generation with HS256 algorithm, | ||
| // - token validation and claims extraction, | ||
| // - 24-hour token expiry, | ||
| // - integration with the users domain. | ||
| package jwt |
This file contains hidden or 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,15 @@ | ||
| package jwt | ||
|
|
||
| import ( | ||
| "github.com/bit-issues/backend/internal/users" | ||
| "github.com/golang-jwt/jwt/v5" | ||
| ) | ||
|
|
||
| // Claims represents the JWT token claims. | ||
| type Claims struct { | ||
| jwt.RegisteredClaims | ||
|
|
||
| UserID int64 `json:"user_id"` | ||
| Role users.Role `json:"role"` | ||
| Status users.Status `json:"status"` | ||
| } |
This file contains hidden or 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,9 @@ | ||
| package jwt | ||
|
|
||
| import "errors" | ||
|
|
||
| var ( | ||
| ErrInvalidConfig = errors.New("invalid config") | ||
| ErrInvalidToken = errors.New("invalid token") | ||
| ErrExpiredToken = errors.New("token has expired") | ||
| ) |
This file contains hidden or 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,18 @@ | ||
| package jwt | ||
|
|
||
| import ( | ||
| "github.com/go-core-fx/logger" | ||
| "go.uber.org/fx" | ||
| ) | ||
|
|
||
| // Module creates and returns an FX module for the jwt package. | ||
| // | ||
| // The module provides: | ||
| // - JWTService for token generation and validation | ||
| func Module() fx.Option { | ||
| return fx.Module( | ||
| "jwt", | ||
| logger.WithNamedLogger("jwt"), | ||
| fx.Provide(NewService), | ||
| ) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.