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 }
func AdminLoginHandler(w http.ResponseWriter, r *http.Request) { type JwtToken struct { Token string `json:"token"` Status bool `json:"status"` } userController := controller.User{} user, err := userController.Login(w, r) var jsonJwt *JwtToken //ensure role is admin (2) if err != nil || user.Role == 1 { //incorrect login data jsonJwt = &JwtToken{ Token: " ", Status: false, } } else { //issue jwt token jwtToken := auth.JwtToken{} jwt := jwtToken.GenerateToken(user.Id, user.Role) jsonJwt = &JwtToken{ Token: jwt, Status: true, } } token, _ := json.Marshal(jsonJwt) fmt.Fprint(w, string(token)) }
func UserCreateHandler(w http.ResponseWriter, r *http.Request) { userController := controller.User{} _, err := userController.CreateUser(w, r) if err != nil { log.Println(err) } }
func UserGetEmailHandler(w http.ResponseWriter, r *http.Request) { userController := controller.User{} userStatus, err := userController.CheckEmail(w, r) if err != nil { //handle err } fmt.Fprint(w, userStatus) }
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)) } } }