// InitRecovery initializes recovery token for the user associated with the
// provided email address and sends to the same address.
// On successful execution, a recovery email will be sent to the email
// provided.
InitRecovery initializes recovery token for the user associated with the
provided email address and sends to the same address.
On successful execution, a recovery email will be sent to the email
provided.
// InitRecovery initializes recovery token for the user associated with the // provided email address and sends to the same address . // On successful execution a recovery email will be sent to the email // provided .
func (h *Handler) InitRecovery(w http.ResponseWriter, r *http.Request) {
	var cInp CoreInput
	if err := httpflow.DecodeJSON(r, &cInp); err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	if err := CheckEmail(cInp.Email); err != nil {
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	respErr := func(err error) {
		if httpflow.ErrorCode(err) < 500 {
			httpflow.Respond(h.log, w, r, nil, http.StatusAccepted)
			return
		}
		httpflow.RespondError(h.log, w, r, err)
	}
	ctx := r.Context()
	usr, err := h.db.FetchUserByEmail(ctx, cInp.Email)
	if err != nil {
		respErr(err)
		return
	}
	usrC := usr.ExposeCore()
	tok, err := usrC.InitRecovery(h.token.recov)
	if err != nil {
		if errors.Is(err, ErrTooManyTokens) {
			secs := int(time.Until(usrC.Recovery.NextAt.Time).Seconds())
			w.Header().Add("Retry-After", strconv.Itoa(secs))
		}
		httpflow.RespondError(h.log, w, r, err)
		return
	}
	if err = h.db.UpdateUser(ctx, usr); err != nil {
		respErr(err)
		return
	}
	go h.email.SendAccountRecovery(ctx, usrC.Email, tok)
	httpflow.Respond(h.log, w, r, nil, http.StatusAccepted)
}
func ( h * Handler ) InitRecovery ( w http . ResponseWriter , r * http . Request ) { var cInp CoreInput if err := httpflow . DecodeJSON ( r , & cInp ) ; err != nil { httpflow . RespondError ( h . log , w , r , err ) return } if err := CheckEmail ( cInp . Email ) ; err != nil { httpflow . RespondError ( h . log , w , r , err ) return } respErr := func ( err error ) { if httpflow . ErrorCode ( err ) < 500 { httpflow . Respond ( h . log , w , r , nil , http . StatusAccepted ) return } httpflow . RespondError ( h . log , w , r , err ) } ctx := r . Context ( ) usr , err := h . db . FetchUserByEmail ( ctx , cInp . Email ) if err != nil { respErr ( err ) return } usrC := usr . ExposeCore ( ) tok , err := usrC . InitRecovery ( h . token . recov ) if err != nil { if errors . Is ( err , ErrTooManyTokens ) { secs := int ( time . Until ( usrC . Recovery . NextAt . Time ) . Seconds ( ) ) w . Header ( ) . Add ( " " , strconv . Itoa ( secs ) ) } httpflow . RespondError ( h . log , w , r , err ) return } if err = h . db . UpdateUser ( ctx , usr ) ; err != nil { respErr ( err ) return } go h . email . SendAccountRecovery ( ctx , usrC . Email , tok ) httpflow . Respond ( h . log , w , r , nil , http . StatusAccepted ) }
InitRecovery initializes recovery token for the user associated with the
provided email address and sends to the same address.
InitRecovery initializes recovery token for the user associated with the provided email address and sends to the same address .
// if email is not found, etc. we don't want the user or attacker // to know this.