// Update handles user's data update in the data store.
// On email address change, a verification email will be sent to the new
// address.
// On password change, all other sessions will be destroyed and email sent.
Update handles user's data update in the data store.
On email address change, a verification email will be sent to the new
address.
On password change, all other sessions will be destroyed and email sent.
// Update handles user ' s data update in the data store . // On email address change a verification email will be sent to the new // address . // On password change all other sessions will be destroyed and email sent .
func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	_, id, err := httpflow.ExtractSession(ctx)
	if err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	inp, err := h.ext.parse(r)
	if err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	usr, err := h.db.FetchUserByID(ctx, id)
	if err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	upd, err := usr.ApplyInput(inp)
	if err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	usrC := usr.ExposeCore()
	updC := upd.ExposeCore()
	var tok string
	if updC.Email {
		tok, err = usrC.InitVerification(h.token.verif)
		if err != nil {
			httpflow.RespondError(h.log, w, r, err)
			return
		}
	}
	if updC.Password {
		if err := h.session.manager.RevokeOther(ctx); err != nil {
			httpflow.RespondError(h.log, w, r, err)
			return
		}
	}
	if err = h.db.UpdateUser(ctx, usr); err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	if updC.Password {
		go h.email.SendPasswordChanged(ctx, usrC.Email, false)
	}
	if tok != "" {
		go h.email.SendEmailVerification(ctx, usrC.UnverifiedEmail.String, tok)
	}
	httpflow.Respond(h.log, w, r, nil, http.StatusNoContent)
}
func ( h * Handler ) Update ( w http . ResponseWriter , r * http . Request ) { ctx := r . Context ( ) _ , id , err := httpflow . ExtractSession ( ctx ) if err != nil { httpflow . RespondError ( h . log , w , r , err ) return } inp , err := h . ext . parse ( r ) if err != nil { httpflow . RespondError ( h . log , w , r , err ) return } usr , err := h . db . FetchUserByID ( ctx , id ) if err != nil { httpflow . RespondError ( h . log , w , r , err ) return } upd , err := usr . ApplyInput ( inp ) if err != nil { httpflow . RespondError ( h . log , w , r , err ) return } usrC := usr . ExposeCore ( ) updC := upd . ExposeCore ( ) var tok string if updC . Email { tok , err = usrC . InitVerification ( h . token . verif ) if err != nil { httpflow . RespondError ( h . log , w , r , err ) return } } if updC . Password { if err := h . session . manager . RevokeOther ( ctx ) ; err != nil { httpflow . RespondError ( h . log , w , r , err ) return } } if err = h . db . UpdateUser ( ctx , usr ) ; err != nil { httpflow . RespondError ( h . log , w , r , err ) return } if updC . Password { go h . email . SendPasswordChanged ( ctx , usrC . Email , false ) } if tok != " " { go h . email . SendEmailVerification ( ctx , usrC . UnverifiedEmail . String , tok ) } httpflow . Respond ( h . log , w , r , nil , http . StatusNoContent ) }
Update handles user's data update in the data store.
Update handles user ' s data update in the data store .