Esempio n. 1
0
func (a *AuthRoute) Login(w http.ResponseWriter, req *http.Request) {
	var authForm map[string]string

	err := RequestToJsonObject(req, &authForm)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	email := authForm["email"]
	pass := authForm["password"]

	userId, err := a.userStore.Login(email, pass)
	if err != nil {
		http.Error(w, "Username or Password Invalid", http.StatusUnauthorized)
		return
	}

	token, err := jwt.GenerateJWTToken(userId, a.options)
	if err != nil {
		http.Error(w, "Error while Signing Token :S", http.StatusInternalServerError)
		return
	}

	jtoken, err := json.Marshal(map[string]string{"token": token})
	if err != nil {
		http.Error(w, "Error marshalling the token to json", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(jtoken)
}
Esempio n. 2
0
func (a *AuthRoute) RefreshToken(w http.ResponseWriter, req *http.Request) {
	userId, _, err := a.authenticate(w, req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusUnauthorized)
		return
	}

	token, err := jwt.GenerateJWTToken(userId, a.options)
	if err != nil {
		http.Error(w, "Error while Signing Token :S", http.StatusInternalServerError)
		return
	}

	jtoken, err := json.Marshal(map[string]string{"token": token})
	if err != nil {
		http.Error(w, "Error marshalling the token to json", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(jtoken)
}