Change: Update Database auth to user method

This commit is contained in:
2021-12-12 01:57:54 +08:00
parent 1f960f8f64
commit b96daa07c6
5 changed files with 55 additions and 12 deletions

View File

@@ -143,3 +143,26 @@ func (api *API) HandleRegister(w http.ResponseWriter, r *http.Request) {
return
}
}
func (api *API) CheckAdmin(w http.ResponseWriter, r *http.Request) error {
session, _ := api.store.Get(r, api.defaultSessionName)
userId, ok := session.Values["userId"]
if !ok {
api.HandleError(w, r, ErrNotLoggedIn)
return ErrNotLoggedIn
}
user, err := api.Db.GetUserById(userId.(int64))
if err != nil {
api.HandleError(w, r, err)
return err
}
if user.Role != database.RoleAdmin {
api.HandleError(w, r, ErrNotAdmin)
return ErrNotAdmin
}
w.WriteHeader(http.StatusOK)
return nil
}