/
account.go
108 lines (97 loc) · 3.55 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
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
package controller
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/ybkuroki/go-webapp-sample/container"
"github.com/ybkuroki/go-webapp-sample/model"
"github.com/ybkuroki/go-webapp-sample/model/dto"
"github.com/ybkuroki/go-webapp-sample/service"
)
// AccountController is a controller for managing user account.
type AccountController interface {
GetLoginStatus(c echo.Context) error
GetLoginAccount(c echo.Context) error
Login(c echo.Context) error
Logout(c echo.Context) error
}
type accountController struct {
context container.Container
service service.AccountService
dummyAccount *model.Account
}
// NewAccountController is constructor.
func NewAccountController(container container.Container) AccountController {
return &accountController{
context: container,
service: service.NewAccountService(container),
dummyAccount: model.NewAccountWithPlainPassword("test", "test", 1),
}
}
// GetLoginStatus returns the status of login.
// @Summary Get the login status.
// @Description Get the login status of current logged-in user.
// @Tags Auth
// @Accept json
// @Produce json
// @Success 200 {boolean} bool "The current user have already logged-in. Returns true."
// @Failure 401 {boolean} bool "The current user haven't logged-in yet. Returns false."
// @Router /auth/loginStatus [get]
func (controller *accountController) GetLoginStatus(c echo.Context) error {
return c.JSON(http.StatusOK, true)
}
// GetLoginAccount returns the account data of logged in user.
// @Summary Get the account data of logged-in user.
// @Description Get the account data of logged-in user.
// @Tags Auth
// @Accept json
// @Produce json
// @Success 200 {object} model.Account "Success to fetch the account data. If the security function is disable, it returns the dummy data."
// @Failure 401 {boolean} bool "The current user haven't logged-in yet. Returns false."
// @Router /auth/loginAccount [get]
func (controller *accountController) GetLoginAccount(c echo.Context) error {
if !controller.context.GetConfig().Extension.SecurityEnabled {
return c.JSON(http.StatusOK, controller.dummyAccount)
}
return c.JSON(http.StatusOK, controller.context.GetSession().GetAccount())
}
// Login is the method to login using username and password by http post.
// @Summary Login using username and password.
// @Description Login using username and password.
// @Tags Auth
// @Accept json
// @Produce json
// @Param data body dto.LoginDto true "User name and Password for logged-in."
// @Success 200 {object} model.Account "Success to the authentication."
// @Failure 401 {boolean} bool "Failed to the authentication."
// @Router /auth/login [post]
func (controller *accountController) Login(c echo.Context) error {
dto := dto.NewLoginDto()
if err := c.Bind(dto); err != nil {
return c.JSON(http.StatusBadRequest, dto)
}
sess := controller.context.GetSession()
if account := sess.GetAccount(); account != nil {
return c.JSON(http.StatusOK, account)
}
authenticate, a := controller.service.AuthenticateByUsernameAndPassword(dto.UserName, dto.Password)
if authenticate {
_ = sess.SetAccount(a)
_ = sess.Save()
return c.JSON(http.StatusOK, a)
}
return c.NoContent(http.StatusUnauthorized)
}
// Logout is the method to logout by http post.
// @Summary Logout.
// @Description Logout.
// @Tags Auth
// @Accept json
// @Produce json
// @Success 200
// @Router /auth/logout [post]
func (controller *accountController) Logout(c echo.Context) error {
sess := controller.context.GetSession()
_ = sess.SetAccount(nil)
_ = sess.Delete()
return c.NoContent(http.StatusOK)
}