예제 #1
0
func (a *Auth) VerifyAdmin(r *http.Request) bool {
	token := JwtToken{}

	userToken, err := token.ParseToken(r)

	if err != nil {
		return false
	}

	//get user
	userController := controller.User{}
	userId := userToken.Claims["userId"].(float64)
	user, err := userController.GetUser(r, int64(userId))

	if err != nil {
		return false
	}

	//verify admin status
	if user.Role != 2 {
		return false
	}

	//verified
	return true
}
예제 #2
0
func UserGetHandler(w http.ResponseWriter, r *http.Request) {
	userController := controller.User{}

	//verify user
	jwtToken := auth.JwtToken{}
	userToken, err := jwtToken.ParseToken(r)

	if err != nil {
		http.Error(w, "Invalid user", 400)
	} else {
		userId := userToken.Claims["userId"].(float64)
		user, err := userController.GetUser(r, int64(userId))

		if err != nil {
			http.Error(w, "Invalid user", 400)
		} else {
			jsonRes, _ := json.Marshal(user)
			fmt.Fprint(w, string(jsonRes))
		}
	}
}