/
account.go
59 lines (51 loc) · 1.65 KB
/
account.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
package controller
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/ybkuroki/go-webapp-project-template/config"
"github.com/ybkuroki/go-webapp-project-template/model"
"github.com/ybkuroki/go-webapp-project-template/service"
"github.com/ybkuroki/go-webapp-project-template/session"
)
var dummyAccount = model.NewAccountWithPlainPassword("test", "test", model.NewAuthority("Admin"))
// GetLoginStatus returns the status of login.
func GetLoginStatus() echo.HandlerFunc {
return func(c echo.Context) error {
return c.JSON(http.StatusOK, true)
}
}
// GetLoginAccount returns the account data of logged in user.
func GetLoginAccount() echo.HandlerFunc {
return func(c echo.Context) error {
if !config.GetConfig().Extension.SecurityEnabled {
return c.JSON(http.StatusOK, dummyAccount)
}
return c.JSON(http.StatusOK, session.GetAccount(c))
}
}
// PostLogin is the method to login using username and password by http post.
func PostLogin() echo.HandlerFunc {
return func(c echo.Context) error {
username := c.FormValue("username")
password := c.FormValue("password")
account := session.GetAccount(c)
if account == nil {
authenticate, a := service.AuthenticateByUsernameAndPassword(username, password)
if authenticate {
_ = session.SetAccount(c, a)
_ = session.Save(c)
return c.JSON(http.StatusOK, a)
}
return c.NoContent(http.StatusUnauthorized)
}
return c.JSON(http.StatusOK, account)
}
}
// PostLogout is the method to logout by http post.
func PostLogout() echo.HandlerFunc {
return func(c echo.Context) error {
_ = session.SetAccount(c, nil)
_ = session.Delete(c)
return c.NoContent(http.StatusOK)
}
}