-
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.
feat(MariaDB ORM): Simple MariaDB with authentification
- Loading branch information
Showing
14 changed files
with
147 additions
and
38 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,16 @@ | ||
package models | ||
|
||
import "gorm.io/gorm" | ||
|
||
type Subscribed struct { | ||
gorm.Model | ||
UserID uint `gorm:"not null" json:"user_id"` | ||
Provider string `gorm:"not null" json:"provider"` | ||
ProviderID string `gorm:"not null" json:"provider_id"` | ||
AccessToken string `gorm:"not null" json:"access_token"` | ||
} | ||
|
||
type Users struct { | ||
User User `gorm:"embedded"` | ||
Subscribed []Subscribed `gorm:"foreignKey:UserID"` | ||
} |
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,12 @@ | ||
package models | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
type User struct { | ||
gorm.Model | ||
Email string `gorm:"unique;not null" json:"email" binding:"required"` | ||
Password string `gorm:"not null" json:"password" binding:"required"` | ||
Token string `gorm:"not null" json:"token"` | ||
} |
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,25 @@ | ||
package db | ||
|
||
import ( | ||
"AREA/internal/config" | ||
"fmt" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"log" | ||
"strconv" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func InitDB() { | ||
|
||
user := config.AppConfig.DB.User | ||
password := config.AppConfig.DB.Password | ||
dsn := user + ":" + password + "@tcp(" + config.AppConfig.DB.Host + ":" + strconv.Itoa(config.AppConfig.DB.Port) + ")/" + config.AppConfig.DB.Name + "?parseTime=true" | ||
err := error(nil) | ||
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) | ||
if err != nil { | ||
log.Fatalf("failed to initialize database, got error %v", err) | ||
} | ||
fmt.Println("Database connection established") | ||
} |
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,21 @@ | ||
package utils | ||
|
||
import ( | ||
"AREA/internal/config" | ||
"github.com/dgrijalva/jwt-go" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func NewToken(c *gin.Context, email string) string { | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
"email": email, | ||
"exp": time.Now().Add(time.Hour * 1).Unix(), | ||
}) | ||
tokenString, err := token.SignedString([]byte(config.AppConfig.SecretKey)) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate token"}) | ||
} | ||
return tokenString | ||
} |
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