github.com/volatiletech/authboss@v2.4.1+incompatible/response.go (about)

     1  package authboss
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  const (
    11  	// FormValueRedirect should be honored by HTTPRedirector implementations
    12  	// as the value from the URL that overrides the typical redirect when
    13  	// FollowRedirParam is set to true.
    14  	FormValueRedirect = "redir"
    15  )
    16  
    17  // HTTPResponder knows how to respond to an HTTP request
    18  // Must consider:
    19  // - Flash messages
    20  // - XSRF handling (template data)
    21  // - Assembling template data from various sources
    22  //
    23  // Authboss controller methods (like the one called in response to
    24  // POST /auth/login) will call this method to write a response to the user.
    25  type HTTPResponder interface {
    26  	Respond(w http.ResponseWriter, r *http.Request, code int, templateName string, data HTMLData) error
    27  }
    28  
    29  // HTTPRedirector redirects http requests to a different url (must handle
    30  // both json and html) When an authboss controller wants to redirect a user to
    31  // a different path, it will use this interface.
    32  type HTTPRedirector interface {
    33  	Redirect(w http.ResponseWriter, r *http.Request, ro RedirectOptions) error
    34  }
    35  
    36  // RedirectOptions packages up all the pieces a module needs to write out a
    37  // response.
    38  type RedirectOptions struct {
    39  	// Success & Failure are used to set Flash messages / JSON messages
    40  	// if set. They should be mutually exclusive.
    41  	Success string
    42  	Failure string
    43  
    44  	// Code is used when it's an API request instead of 200.
    45  	Code int
    46  
    47  	// When a request should redirect a user somewhere on completion, these
    48  	// should be set. RedirectURL tells it where to go. And optionally set
    49  	// FollowRedirParam to override the RedirectURL if the form parameter
    50  	// defined by FormValueRedirect is passed in the request.
    51  	//
    52  	// Redirecting works differently whether it's an API request or not.
    53  	// If it's an API request, then it will leave the URL in a "redirect"
    54  	// parameter.
    55  	RedirectPath     string
    56  	FollowRedirParam bool
    57  }
    58  
    59  // EmailResponseOptions controls how e-mails are rendered and sent
    60  type EmailResponseOptions struct {
    61  	Data         HTMLData
    62  	HTMLTemplate string
    63  	TextTemplate string
    64  }
    65  
    66  // Email renders the e-mail templates for the given email and
    67  // sends it using the mailer.
    68  func (a *Authboss) Email(ctx context.Context, email Email, ro EmailResponseOptions) error {
    69  	ctxData := ctx.Value(CTXKeyData)
    70  	if ctxData != nil {
    71  		if ro.Data == nil {
    72  			ro.Data = HTMLData{}
    73  		}
    74  		ro.Data.Merge(ctxData.(HTMLData))
    75  	}
    76  	if len(ro.HTMLTemplate) != 0 {
    77  		htmlBody, _, err := a.Core.MailRenderer.Render(ctx, ro.HTMLTemplate, ro.Data)
    78  		if err != nil {
    79  			return errors.Wrap(err, "failed to render e-mail html body")
    80  		}
    81  		email.HTMLBody = string(htmlBody)
    82  	}
    83  
    84  	if len(ro.TextTemplate) != 0 {
    85  		textBody, _, err := a.Core.MailRenderer.Render(ctx, ro.TextTemplate, ro.Data)
    86  		if err != nil {
    87  			return errors.Wrap(err, "failed to render e-mail text body")
    88  		}
    89  		email.TextBody = string(textBody)
    90  	}
    91  
    92  	return a.Core.Mailer.Send(ctx, email)
    93  }