Added func DeleteSession
parent
fdfb5520a6
commit
079565460c
|
@ -1,7 +1,6 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.umbach.dev/app-idea/rest-api/modules/database"
|
"git.umbach.dev/app-idea/rest-api/modules/database"
|
||||||
|
@ -16,7 +15,7 @@ func isSessionIdValid(sessionId string) bool {
|
||||||
deleteExpiredSessions(database.DB)
|
deleteExpiredSessions(database.DB)
|
||||||
|
|
||||||
var res string
|
var res string
|
||||||
var db = database.DB
|
db := database.DB
|
||||||
|
|
||||||
db.Raw("SELECT session_id FROM sessions WHERE session_id = ?", sessionId).Scan(&res)
|
db.Raw("SELECT session_id FROM sessions WHERE session_id = ?", sessionId).Scan(&res)
|
||||||
|
|
||||||
|
@ -27,12 +26,32 @@ func isSessionIdValid(sessionId string) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteSession(db *sql.DB, sessionId string) {
|
func DeleteSession(c *fiber.Ctx) error {
|
||||||
_, err := db.Exec("DELETE FROM sessions WHERE session_id = ?", sessionId)
|
// swagger:operation POST /sessions/{id} Sessions sessions
|
||||||
|
// ---
|
||||||
|
// summary: Delete a user sessions
|
||||||
|
// responses:
|
||||||
|
// '200':
|
||||||
|
// description: User informations
|
||||||
|
// '400':
|
||||||
|
// description: Session id invalid
|
||||||
|
|
||||||
|
db := database.DB
|
||||||
|
|
||||||
|
userId, err := getUserIdBySessionId(c.Cookies("session_id"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnln("err deleting session:", err)
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
res := db.Where("user_id = ? AND session_id = ?", userId, c.Cookies("session_id")).Delete(&structs.Session{})
|
||||||
|
|
||||||
|
// session isn't in list
|
||||||
|
if res.RowsAffected == 0 {
|
||||||
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.SendStatus(fiber.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteExpiredSessions(db *gorm.DB) {
|
func deleteExpiredSessions(db *gorm.DB) {
|
||||||
|
|
Loading…
Reference in New Issue