Rename UserController to AuthController
This commit is contained in:
parent
eb24084e24
commit
3104cc8f7a
12
api/auth.go
12
api/auth.go
@ -38,7 +38,7 @@ func register(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
userController, err := controller.MakeUserController()
|
||||
AuthController, err := controller.MakeAuthController()
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
@ -47,7 +47,7 @@ func register(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
err = userController.Register(transaction.Username, transaction.Password, transaction.RepeatPassword)
|
||||
err = AuthController.Register(transaction.Username, transaction.Password, transaction.RepeatPassword)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
@ -74,7 +74,7 @@ func login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
userController, err := controller.MakeUserController()
|
||||
AuthController, err := controller.MakeAuthController()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": err.Error(),
|
||||
@ -82,7 +82,7 @@ func login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
token, ok, err := userController.Login(transaction.Username, transaction.Password)
|
||||
token, ok, err := AuthController.Login(transaction.Username, transaction.Password)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": err.Error(),
|
||||
@ -105,7 +105,7 @@ func login(c *gin.Context) {
|
||||
}
|
||||
|
||||
func logout(c *gin.Context) {
|
||||
userController, err := controller.MakeUserController()
|
||||
AuthController, err := controller.MakeAuthController()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": err.Error(),
|
||||
@ -114,7 +114,7 @@ func logout(c *gin.Context) {
|
||||
}
|
||||
|
||||
token, _ := c.Get(SESSION_COOKIE) // must exist after isLoggedIn
|
||||
err = userController.Logout(token.(string))
|
||||
err = AuthController.Logout(token.(string))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": err.Error(),
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type UserController struct {
|
||||
type AuthController struct {
|
||||
userDAO dao.IUserDAO
|
||||
sessionDAO dao.ISessionDAO
|
||||
}
|
||||
@ -22,7 +22,7 @@ const (
|
||||
TOKEN_LENGTH int = 32
|
||||
)
|
||||
|
||||
func (c *UserController) init() error {
|
||||
func (c *AuthController) init() error {
|
||||
userDAO, err := dao.MakeUserDAO()
|
||||
c.userDAO = userDAO
|
||||
if err != nil {
|
||||
@ -38,7 +38,7 @@ func (c *UserController) init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c UserController) Register(username string, password string, repeatPassword string) error {
|
||||
func (c AuthController) Register(username string, password string, repeatPassword string) error {
|
||||
if len(username) < MIN_USERNAME_LENGTH {
|
||||
return errors.New("username too short")
|
||||
}
|
||||
@ -72,7 +72,7 @@ func generateToken(length int) (string, error) {
|
||||
return base64.URLEncoding.EncodeToString(b), nil
|
||||
}
|
||||
|
||||
func (c UserController) Login(username string, password string) (string, bool, error) {
|
||||
func (c AuthController) Login(username string, password string) (string, bool, error) {
|
||||
user, err := c.userDAO.Read(model.User{Username: username})
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
@ -95,6 +95,6 @@ func (c UserController) Login(username string, password string) (string, bool, e
|
||||
return token, true, nil
|
||||
}
|
||||
|
||||
func (c UserController) Logout(token string) error {
|
||||
func (c AuthController) Logout(token string) error {
|
||||
return c.sessionDAO.Delete(token)
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package controller
|
||||
|
||||
func MakeUserController() (UserController, error) {
|
||||
userController := &UserController{}
|
||||
err := userController.init()
|
||||
return *userController, err
|
||||
func MakeAuthController() (AuthController, error) {
|
||||
AuthController := &AuthController{}
|
||||
err := AuthController.init()
|
||||
return *AuthController, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user