|
|
@@ -53,7 +53,7 @@ func (app *App) HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// HandleAuthCheck checks whether current session is authenticated.
|
|
|
+// HandleAuthCheck checks whether current session is authenticated and returns user ID if so.
|
|
|
func (app *App) HandleAuthCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
session, err := app.store.Get(r, app.cookieName)
|
|
|
|
|
|
@@ -61,14 +61,18 @@ func (app *App) HandleAuthCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
}
|
|
|
|
|
|
- if auth, ok := session.Values["authenticated"].(bool); !auth || !ok {
|
|
|
- app.logger.Info().Msgf(strconv.FormatBool(auth))
|
|
|
- w.WriteHeader(http.StatusOK)
|
|
|
- w.Write([]byte("false"))
|
|
|
+ userID, _ := session.Values["user_id"].(uint)
|
|
|
+
|
|
|
+ resUser := &models.UserExternal{
|
|
|
+ ID: userID,
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := json.NewEncoder(w).Encode(resUser); err != nil {
|
|
|
+ app.handleErrorFormDecoding(err, ErrUserDecode, w)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
- w.Write([]byte("true"))
|
|
|
}
|
|
|
|
|
|
// HandleLoginUser checks the request header for cookie and validates the user.
|
|
|
@@ -113,6 +117,15 @@ func (app *App) HandleLoginUser(w http.ResponseWriter, r *http.Request) {
|
|
|
app.logger.Warn().Err(err)
|
|
|
}
|
|
|
|
|
|
+ resUser := &models.UserExternal{
|
|
|
+ ID: storedUser.ID,
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := json.NewEncoder(w).Encode(resUser); err != nil {
|
|
|
+ app.handleErrorFormDecoding(err, ErrUserDecode, w)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
}
|
|
|
|