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
|
package controllers
import (
"lain/config"
"lain/repository"
"lain/session"
"lain/types"
"lain/utils/crypto"
"lain/utils/email"
"lain/utils/meta"
"lain/utils/shortcuts"
"github.com/gofiber/fiber/v2"
)
func LoginPage(context *fiber.Ctx) error {
meta.SetPageTitle(context, "Login")
return shortcuts.Render(context, "auth/login", fiber.Map{
"AllowedDomains": config.Server.AllowedDomains,
})
}
func Login(context *fiber.Ctx) error {
var formData types.LoginForm
if err := context.BodyParser(&formData); err != nil {
return BadRequest(context, err)
}
imapClient, err := email.ConnectIMAP(formData.Email, formData.Password)
if err != nil {
return shortcuts.RedirectWithFlash(context, "auth.login", fiber.Map{
"Error": "Invalid email or password.",
})
}
imapClient.Close()
encryptedPassword, err := crypto.Encrypt(formData.Password)
if err != nil {
return InternalServerError(context, err)
}
formData.Password = encryptedPassword
preferences, err := repository.GetPreferences(formData)
if err != nil {
return InternalServerError(context, err)
}
if err = session.CreateSession(context, preferences.Email); err != nil {
return InternalServerError(context, err)
}
return shortcuts.Redirect(context, "mail.inbox")
}
func Logout(context *fiber.Ctx) error {
if err := session.DestroySession(context); err != nil {
return InternalServerError(context, err)
}
return shortcuts.Redirect(context, "auth.login")
}
|