We can add and list groups now let's goooo
This commit is contained in:
parent
bdd03f96f7
commit
fc074d83c3
3 changed files with 34 additions and 14 deletions
|
@ -20,6 +20,7 @@ func main() {
|
|||
app.GET("/auth/logout", api.AuthLogoutRedirect)
|
||||
// Create & Update
|
||||
app.POST("/post/user/update", api.CreateOrUpdateUser)
|
||||
app.POST("/post/group", api.CreateDatabaseGroup)
|
||||
// Read
|
||||
app.GET("/get/user/info", api.GetUserInfo)
|
||||
app.GET("/get/user/authorized", api.GetIsUserAuthorized)
|
||||
|
|
|
@ -103,15 +103,16 @@ func CreateOrUpdateUser(context *gin.Context) {
|
|||
}
|
||||
if databasecommands.GetDatabaseUserExists(GlobalDatabase, currentDiscordUser.Id) {
|
||||
dbOAuthToken := databasecommands.GetDatabaseUserToken(GlobalDatabase, currentDiscordUser.Id)
|
||||
if dbOAuthToken == "" {
|
||||
context.SetCookie("discord-oauthtoken", string(oauthTokenJSON), 0, "", GlobalConfig.API.Domain, false, false)
|
||||
err := databasecommands.UpdateDatabaseUser(GlobalDatabase, updatedDatabaseUser)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
context.AbortWithStatus(http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
if databasecommands.GetDatabaseUserLoggedIn(GlobalDatabase, dbOAuthToken) {
|
||||
context.SetCookie("discord-oauthtoken", dbOAuthToken, 0, "", GlobalConfig.API.Domain, false, false)
|
||||
updatedDatabaseUser.LoginToken = string(oauthTokenJSON)
|
||||
} else {
|
||||
context.SetCookie("discord-oauthtoken", string(oauthTokenJSON), 0, "", GlobalConfig.API.Domain, false, false)
|
||||
}
|
||||
err := databasecommands.UpdateDatabaseUser(GlobalDatabase, updatedDatabaseUser)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
context.AbortWithStatus(http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
err := databasecommands.CreateDatabaseUser(GlobalDatabase, updatedDatabaseUser)
|
||||
|
@ -123,7 +124,22 @@ func CreateOrUpdateUser(context *gin.Context) {
|
|||
}
|
||||
|
||||
func CreateDatabaseGroup(context *gin.Context) {
|
||||
|
||||
GetIsUserAuthorized(context)
|
||||
isAuthorized := context.GetBool("is-authorized")
|
||||
log.Println(isAuthorized)
|
||||
if isAuthorized {
|
||||
name := context.Query("name")
|
||||
if name != "" {
|
||||
databasecommands.CreateDatabaseGroup(GlobalDatabase, databasemodels.Group{
|
||||
Name: name,
|
||||
})
|
||||
context.Status(http.StatusOK)
|
||||
} else {
|
||||
context.AbortWithStatus(http.StatusBadRequest)
|
||||
}
|
||||
} else {
|
||||
context.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
// Read Endpoints (get/)
|
||||
|
@ -159,6 +175,7 @@ func GetUserInfo(context *gin.Context) {
|
|||
|
||||
func GetIsUserAuthorized(context *gin.Context) {
|
||||
oauthTokenJSON, err := context.Cookie("discord-oauthtoken")
|
||||
log.Println(oauthTokenJSON)
|
||||
if err == nil {
|
||||
var oauthToken *oauth2.Token
|
||||
err := json.Unmarshal([]byte(oauthTokenJSON), &oauthToken)
|
||||
|
@ -168,18 +185,17 @@ func GetIsUserAuthorized(context *gin.Context) {
|
|||
context.JSON(http.StatusOK, gin.H{
|
||||
"message": true,
|
||||
})
|
||||
} else {
|
||||
context.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": true,
|
||||
})
|
||||
context.Set("is-authorized", true)
|
||||
log.Println("yessss")
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
context.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": false,
|
||||
})
|
||||
context.Set("is-authorized", false)
|
||||
}
|
||||
|
||||
func GetDatabaseGroup(context *gin.Context) {
|
||||
|
|
|
@ -16,6 +16,7 @@ func InitializeDatabase() *gorm.DB {
|
|||
log.Fatal("Failed to connect to database.")
|
||||
}
|
||||
db.AutoMigrate(&databasemodels.User{})
|
||||
db.AutoMigrate(&databasemodels.Group{})
|
||||
return db
|
||||
}
|
||||
|
||||
|
@ -45,6 +46,7 @@ func GetDatabaseUserLoggedIn(db *gorm.DB, oauthTokenJSON string) bool {
|
|||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return false
|
||||
} else {
|
||||
log.Println(queryUser)
|
||||
return queryUser.LoggedIn
|
||||
}
|
||||
}
|
||||
|
@ -71,6 +73,7 @@ func LogoutDatabaseUser(db *gorm.DB, oauthToken string) {
|
|||
|
||||
func CreateDatabaseGroup(db *gorm.DB, group databasemodels.Group) error {
|
||||
result := db.Create(&group)
|
||||
log.Println(group)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue