-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.go
241 lines (198 loc) · 6.03 KB
/
login.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package frontend
import (
"crypto/sha512"
"encoding/json"
"fmt"
"strings"
"go.savla.dev/littr/configs"
"go.savla.dev/littr/pkg/models"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type LoginPage struct {
app.Compo
userLogged bool
}
type loginContent struct {
app.Compo
nickname string
passphrase string
toastShow bool
toastText string
loginButtonDisabled bool
}
func (p *LoginPage) OnMount(ctx app.Context) {
if ctx.Page().URL().Path == "/logout" {
// destroy auth manually without API
//ctx.LocalStorage().Set("userLogged", false)
//ctx.LocalStorage().Set("userName", "")
//ctx.LocalStorage().Set("flowRecords", nil)
ctx.SetState("user", "")
ctx.SetState("authGranted", false)
p.userLogged = false
ctx.Navigate("/login")
}
}
func (p *LoginPage) OnNav(ctx app.Context) {
ctx.Page().SetTitle("login / littr")
}
func (p *LoginPage) Render() app.UI {
return app.Div().Body(
&header{},
&footer{},
&loginContent{},
)
}
func (c *loginContent) onClickRegister(ctx app.Context, e app.Event) {
ctx.Navigate("/register")
return
}
func (c *loginContent) onClickReset(ctx app.Context, e app.Event) {
ctx.Navigate("/reset")
return
}
func (c *loginContent) onClick(ctx app.Context, e app.Event) {
response := struct {
Message string `json:"message"`
AuthGranted bool `json:"auth_granted"`
//FlowRecords []string `json:"flow_records"`
Users map[string]models.User `json:"users"`
}{}
toastText := ""
// fix this!
c.loginButtonDisabled = true
ctx.Async(func() {
// trim the padding spaces on the extremities
// https://www.tutorialspoint.com/how-to-trim-a-string-in-golang
nickname := strings.TrimSpace(c.nickname)
passphrase := strings.TrimSpace(c.passphrase)
if nickname == "" || passphrase == "" {
toastText = "all fields need to be filled"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
}
passHash := sha512.Sum512([]byte(passphrase + app.Getenv("APP_PEPPER")))
respRaw, ok := litterAPI("POST", "/api/v1/auth", &models.User{
Nickname: nickname,
Passphrase: string(passHash[:]),
PassphraseHex: fmt.Sprintf("%x", passHash),
}, nickname, 0)
if !ok {
toastText = "backend error: API call failed"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
}
if respRaw == nil {
toastText = "backend error: blank response from API"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
}
if err := json.Unmarshal(*respRaw, &response); err != nil {
toastText = "backend error: cannot unmarshal response: " + err.Error()
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
}
if !response.AuthGranted {
toastText = "wrong credentials on input"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
}
user, err := json.Marshal(response.Users[nickname])
if err != nil {
toastText = "frontend error: user marshal failed"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
}
// save enrypted user data to their Local browser storage
ctx.LocalStorage().Set("user", user)
ctx.LocalStorage().Set("authGranted", true)
if response.AuthGranted {
ctx.Navigate("/flow")
}
})
}
func (c *loginContent) dismissToast(ctx app.Context, e app.Event) {
c.toastText = ""
c.toastShow = false
c.loginButtonDisabled = false
}
func (c *loginContent) Render() app.UI {
return app.Main().Class("responsive").Body(
app.Div().Class("row").Body(
app.Div().Class("max padding").Body(
app.H5().Text("littr login"),
),
),
/*app.P().Body(
app.P().Text("littr, bc even litter can be lit"),
),*/
app.Div().Class("space"),
// snackbar
app.A().OnClick(c.dismissToast).Body(
app.If(c.toastText != "",
app.Div().Class("snackbar red10 white-text top active").Body(
app.I().Text("error"),
app.Span().Text(c.toastText),
),
),
),
// login credentials fields
app.Div().Class("field border label deep-orange-text").Body(
app.Input().Type("text").Required(true).TabIndex(1).OnChange(c.ValueTo(&c.nickname)).MaxLength(configs.NicknameLengthMax).Class("active").Attr("autocomplete", "nickname"),
app.Label().Text("nickname").Class("active deep-orange-text"),
),
app.Div().Class("field border label deep-orange-text").Body(
app.Input().Type("password").Required(true).TabIndex(2).OnChange(c.ValueTo(&c.passphrase)).MaxLength(50).Class("active").Attr("autocomplete", "current-password"),
app.Label().Text("passphrase").Class("active deep-orange-text"),
),
app.Article().Class("row surface-container-highest").Body(
app.I().Text("lightbulb").Class("amber-text"),
app.P().Class("max").Body(
//app.Span().Class("deep-orange-text").Text(" "),
app.Span().Text("log-in for 30 days"),
),
),
app.Div().Class("medium-space"),
// login button
app.Div().Class("row").Body(
app.Button().Class("max deep-orange7 white-text bold").Style("border-radius", "8px").TabIndex(3).OnClick(c.onClick).Disabled(c.loginButtonDisabled).Body(
app.Text("login"),
),
),
app.Div().Class("space"),
// reset button
app.Div().Class("row max").Body(
app.Button().Class("max deep-orange7 white-text bold").Style("border-radius", "8px").TabIndex(4).OnClick(c.onClickReset).Disabled(c.loginButtonDisabled).Body(
app.Text("recover forgotten passphrase"),
),
// register button
app.If(configs.REGISTERATION_ENABLED,
app.Button().Class("max deep-orange7 white-text bold").Style("border-radius", "8px").TabIndex(5).OnClick(c.onClickRegister).Disabled(c.loginButtonDisabled).Body(
app.Text("register"),
),
).Else(
app.Button().Class("max deep-orange7 white-text bold").Style("border-radius", "8px").TabIndex(5).OnClick(nil).Disabled(true).Body(
app.Text("register"),
),
),
),
app.Div().Class("space"),
)
}