Added a log out feature.
This commit is contained in:
parent
dc55b6f19a
commit
d20eff4306
4 changed files with 54 additions and 13 deletions
|
@ -114,6 +114,17 @@ func loginRedirect(context *gin.Context) {
|
|||
context.Redirect(302, oauthConfig.AuthCodeURL(context.GetString("state")))
|
||||
}
|
||||
|
||||
func logoutRedirect(context *gin.Context) {
|
||||
oauthTokenJSON, err := context.Cookie("discord-oauthtoken")
|
||||
if err == nil {
|
||||
dbcommands.LogoutDatabaseUser(db, oauthTokenJSON)
|
||||
context.SetCookie("discord-oauthtoken", "", -1, "", config.API.Domain, false, true)
|
||||
} else {
|
||||
log.Println(err)
|
||||
}
|
||||
context.Redirect(http.StatusTemporaryRedirect, "/")
|
||||
}
|
||||
|
||||
func authCallback(context *gin.Context) {
|
||||
oauthState := randomstring.CookieFriendlyString(32)
|
||||
context.Set("state", oauthState)
|
||||
|
@ -127,6 +138,8 @@ func authCallback(context *gin.Context) {
|
|||
}
|
||||
oauthTokenJSON, _ := json.Marshal(oauthToken)
|
||||
context.SetCookie("discord-oauthtoken", string(oauthTokenJSON), 0, "", config.API.Domain, false, false)
|
||||
user := getDiscordUser(context, oauthToken)
|
||||
createOrUpdateUser(context, oauthToken, user)
|
||||
context.Redirect(http.StatusTemporaryRedirect, "/dashboard")
|
||||
}
|
||||
|
||||
|
@ -155,8 +168,7 @@ func getDiscordUser(context *gin.Context, oauthToken *oauth2.Token) discordUser
|
|||
return user
|
||||
}
|
||||
|
||||
func createOrUpdateUser(context *gin.Context, oauthToken *oauth2.Token) {
|
||||
user := getDiscordUser(context, oauthToken)
|
||||
func createOrUpdateUser(context *gin.Context, oauthToken *oauth2.Token, user discordUser) {
|
||||
oauthTokenJSON, err := json.Marshal(oauthToken)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
@ -168,16 +180,18 @@ func createOrUpdateUser(context *gin.Context, oauthToken *oauth2.Token) {
|
|||
Avatar: user.Avatar,
|
||||
AvatarDecoration: user.Avatar_Decoration_Data.Asset,
|
||||
LoginToken: string(oauthTokenJSON),
|
||||
LoggedIn: true,
|
||||
}
|
||||
if dbcommands.DatabaseUserExists(db, user.Id) {
|
||||
dbOAuthToken := dbcommands.GetDatabaseUserToken(db, user.Id)
|
||||
if dbOAuthToken == "" {
|
||||
context.SetCookie("discord-oauthtoken", string(oauthTokenJSON), 0, "", config.API.Domain, false, false)
|
||||
err := dbcommands.UpdateDatabaseUser(db, dbUser)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else {
|
||||
context.SetCookie("discord-oauthtoken", dbOAuthToken, 0, "", config.API.Domain, false, false)
|
||||
dbUser.LoginToken = dbOAuthToken
|
||||
}
|
||||
err := dbcommands.UpdateDatabaseUser(db, dbUser)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else {
|
||||
err := dbcommands.CreateDatabaseUser(db, dbUser)
|
||||
|
@ -194,9 +208,12 @@ func dashboardDisplay(context *gin.Context) {
|
|||
err := json.Unmarshal([]byte(oauthTokenJSON), &oauthToken)
|
||||
if err == nil {
|
||||
if oauthToken.Valid() {
|
||||
createOrUpdateUser(context, oauthToken)
|
||||
user := getDiscordUser(context, oauthToken)
|
||||
context.HTML(http.StatusOK, "dashboard.html", user)
|
||||
if dbcommands.DatabaseUserLoggedIn(db, user.Id) {
|
||||
context.HTML(http.StatusOK, "dashboard.html", user)
|
||||
} else {
|
||||
context.Redirect(http.StatusTemporaryRedirect, "/logout")
|
||||
}
|
||||
return
|
||||
}
|
||||
} else {
|
||||
|
@ -218,6 +235,7 @@ func main() {
|
|||
app.GET("/", loginDisplay)
|
||||
app.GET("/login", loginRedirect)
|
||||
app.GET("/auth/callback", authCallback)
|
||||
app.GET("/logout", logoutRedirect)
|
||||
app.GET("/dashboard", dashboardDisplay)
|
||||
app.Run(":31337")
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ type User struct {
|
|||
Avatar string
|
||||
AvatarDecoration string
|
||||
LoginToken string
|
||||
LoggedIn bool
|
||||
}
|
||||
|
||||
func InitializeDatabase() *gorm.DB {
|
||||
|
@ -47,6 +48,16 @@ func DatabaseUserExists(db *gorm.DB, id string) bool {
|
|||
}
|
||||
}
|
||||
|
||||
func DatabaseUserLoggedIn(db *gorm.DB, id string) bool {
|
||||
var queryUser User
|
||||
result := db.Where("id = ?", id).Take(&queryUser)
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return false
|
||||
} else {
|
||||
return queryUser.LoggedIn
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateDatabaseUser(db *gorm.DB, user User) error {
|
||||
result := db.Save(&user)
|
||||
if result.Error != nil {
|
||||
|
@ -62,3 +73,8 @@ func CreateDatabaseUser(db *gorm.DB, user User) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func LogoutDatabaseUser(db *gorm.DB, oauthToken string) {
|
||||
db.Model(&User{}).Where("login_token = ?", oauthToken).Update("logged_in", false)
|
||||
db.Model(&User{}).Where("login_token = ?", oauthToken).Update("login_token", "")
|
||||
}
|
||||
|
|
|
@ -6,8 +6,7 @@ require (
|
|||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||
golang.org/x/oauth2 v0.29.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
gorm.io/driver/sqlite v1.5.7 // indirect
|
||||
gorm.io/gorm v1.25.12 // indirect
|
||||
gorm.io/driver/sqlite v1.5.7 // direct
|
||||
gorm.io/gorm v1.25.12 // direct
|
||||
)
|
||||
|
|
|
@ -16,7 +16,8 @@
|
|||
class="container max-w-3xl p-4 mx-auto space-y-2 font-mono text-white bg-primary min-w-xs md:space-y-4">
|
||||
<div class="p-2 bg-secondary md:p-4 ring-2 ring-secondary/80">
|
||||
<div class="bg-primary ring-2 ring-primary/80">
|
||||
<div class="flex flex-col w-full h-full p-2 space-x-4 rounded-sm space-2 drop-shadow-md drop-shadow-accent">
|
||||
<div
|
||||
class="flex flex-col w-full h-full p-2 space-x-4 rounded-sm space-2 drop-shadow-md drop-shadow-accent">
|
||||
<h1 class="text-md md:text-xl">> USER AUTHENTICATED</h1>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -24,9 +25,16 @@
|
|||
<div class="p-2 bg-secondary md:p-4 ring-2 ring-secondary/80">
|
||||
{{ template "userinfo.html" . }}
|
||||
</div>
|
||||
<a class="flex w-full p-2 font-mono text-lg md:p-4 md:text-2xl bg-secondary ring-2 ring-secondary/80 h-fit0 hover:bg-secondary/50"
|
||||
href="logout">
|
||||
<div class="w-full h-full text-center bg-primary">
|
||||
<p class="drop-shadow-accent drop-shadow-md">Logout</p>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in a new issue