-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signin.go
162 lines (138 loc) · 3.94 KB
/
signin.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package handlers
import (
"net/http"
"time"
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/actions"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/web"
webutil "github.com/getfider/fider/app/pkg/web/util"
"github.com/getfider/fider/app/tasks"
)
// SignInPage renders the sign in page
func SignInPage() web.HandlerFunc {
return func(c web.Context) error {
if c.Tenant().IsPrivate || c.Tenant().Status == models.TenantLocked {
return c.Page(web.Props{
Title: "Sign in",
ChunkName: "SignIn.page",
})
}
return c.Redirect(c.BaseURL())
}
}
// NotInvitedPage renders the not invited page
func NotInvitedPage() web.HandlerFunc {
return func(c web.Context) error {
return c.Render(http.StatusForbidden, "not-invited.html", web.Props{
Title: "Not Invited",
Description: "We couldn't find your account for your email address.",
})
}
}
// SignInByEmail sends a new email with verification key
func SignInByEmail() web.HandlerFunc {
return func(c web.Context) error {
input := new(actions.SignInByEmail)
if result := c.BindTo(input); !result.Ok {
return c.HandleValidation(result)
}
err := c.Services().Tenants.SaveVerificationKey(input.Model.VerificationKey, 30*time.Minute, input.Model)
if err != nil {
return c.Failure(err)
}
c.Enqueue(tasks.SendSignInEmail(input.Model))
return c.Ok(web.Map{})
}
}
// VerifySignInKey checks if verify key is correct and sign in user
func VerifySignInKey(kind models.EmailVerificationKind) web.HandlerFunc {
return func(c web.Context) error {
result, err := validateKey(kind, c)
if result == nil {
return err
}
var user *models.User
if kind == models.EmailVerificationKindSignUp && c.Tenant().Status == models.TenantPending {
if err = c.Services().Tenants.Activate(c.Tenant().ID); err != nil {
return c.Failure(err)
}
user = &models.User{
Name: result.Name,
Email: result.Email,
Tenant: c.Tenant(),
Role: models.RoleAdministrator,
}
if err = c.Services().Users.Register(user); err != nil {
return c.Failure(err)
}
} else if kind == models.EmailVerificationKindSignIn {
user, err = c.Services().Users.GetByEmail(result.Email)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
if c.Tenant().IsPrivate {
return NotInvitedPage()(c)
}
return Index()(c)
}
return c.Failure(err)
}
} else if kind == models.EmailVerificationKindUserInvitation {
user, err = c.Services().Users.GetByEmail(result.Email)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
if c.Tenant().IsPrivate {
return SignInPage()(c)
}
return Index()(c)
}
return c.Failure(err)
}
} else {
return c.NotFound()
}
err = c.Services().Tenants.SetKeyAsVerified(result.Key)
if err != nil {
return c.Failure(err)
}
webutil.AddAuthUserCookie(c, user)
return c.Redirect(c.BaseURL())
}
}
// CompleteSignInProfile handles the action to update user profile
func CompleteSignInProfile() web.HandlerFunc {
return func(c web.Context) error {
input := new(actions.CompleteProfile)
if result := c.BindTo(input); !result.Ok {
return c.HandleValidation(result)
}
_, err := c.Services().Users.GetByEmail(input.Model.Email)
if errors.Cause(err) != app.ErrNotFound {
return c.Ok(web.Map{})
}
user := &models.User{
Name: input.Model.Name,
Email: input.Model.Email,
Tenant: c.Tenant(),
Role: models.RoleVisitor,
}
err = c.Services().Users.Register(user)
if err != nil {
return c.Failure(err)
}
err = c.Services().Tenants.SetKeyAsVerified(input.Model.Key)
if err != nil {
return c.Failure(err)
}
webutil.AddAuthUserCookie(c, user)
return c.Ok(web.Map{})
}
}
// SignOut remove auth cookies
func SignOut() web.HandlerFunc {
return func(c web.Context) error {
c.RemoveCookie(web.CookieAuthName)
return c.Redirect(c.QueryParam("redirect"))
}
}