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