Implement user registration functionality
This commit is contained in:
parent
31aa06c526
commit
d201bd6636
40
api/auth.go
40
api/auth.go
@ -1,13 +1,51 @@
|
||||
package api
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.tek.govt.hu/dowerx/chat/server/controller"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func isLoggedIn(c *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func register(c *gin.Context) {
|
||||
type registerTransaction struct {
|
||||
Username string `form:"username"`
|
||||
Password string `form:"password"`
|
||||
RepeatPassword string `form:"repeatPassword"`
|
||||
}
|
||||
|
||||
transaction := registerTransaction{}
|
||||
if err := c.Bind(&transaction); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
userController, err := controller.MakeUserController()
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err = userController.Register(transaction.Username, transaction.Password, transaction.RepeatPassword)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "sucessful register",
|
||||
})
|
||||
}
|
||||
|
||||
func login(c *gin.Context) {
|
||||
|
@ -28,18 +28,18 @@ type Config struct {
|
||||
var config *Config
|
||||
|
||||
func GetConfig() *Config {
|
||||
if config == nil {
|
||||
config = &Config{}
|
||||
|
||||
if len(config.Valkey.InitAddress) == 0 {
|
||||
config.Valkey.InitAddress = make([]string, 1)
|
||||
}
|
||||
|
||||
if config == nil {
|
||||
config = &Config{}
|
||||
|
||||
flag.StringVar(&config.Database.Host, "db-host", "db", "database host")
|
||||
flag.UintVar(&config.Database.Port, "db-port", 5432, "database port")
|
||||
flag.StringVar(&config.Database.User, "db-user", "admin", "database user")
|
||||
flag.StringVar(&config.Database.Password, "db-password", "admin", "database password")
|
||||
flag.StringVar(&config.Database.DBname, "db-name", "szoe", "database name")
|
||||
flag.StringVar(&config.Database.DBname, "db-name", "chat", "database name")
|
||||
|
||||
flag.StringVar(&config.Valkey.InitAddress[0], "valkey-address", "valkey:6379", "Valkey server address")
|
||||
flag.StringVar(&config.Valkey.Username, "valkey-username", "", "Valkey username")
|
||||
|
@ -29,7 +29,7 @@ func (c UserController) Register(username string, password string, repeatPasswor
|
||||
return errors.New("username too short")
|
||||
}
|
||||
if len(password) < MIN_PASSWORD_LENGTH {
|
||||
return errors.New("username too short")
|
||||
return errors.New("password too short")
|
||||
}
|
||||
|
||||
if password != repeatPassword {
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
|
||||
"git.tek.govt.hu/dowerx/chat/server/config"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var db *sqlx.DB
|
||||
|
1
go.mod
1
go.mod
@ -38,5 +38,6 @@ require (
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.10.1
|
||||
github.com/lib/pq v1.10.9
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
)
|
||||
|
1
go.sum
1
go.sum
@ -37,6 +37,7 @@ github.com/kouhin/envflag v0.0.0-20150818174321-0e9a86061649 h1:l95EUBxc0iMtMeam
|
||||
github.com/kouhin/envflag v0.0.0-20150818174321-0e9a86061649/go.mod h1:BT0PpXv8Y4EL/WUsQmYsQ2FSB9HwQXIuvY+pElZVdFg=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
|
10
main.go
10
main.go
@ -1,13 +1,15 @@
|
||||
package main
|
||||
|
||||
import "git.tek.govt.hu/dowerx/chat/server/controller"
|
||||
import (
|
||||
"git.tek.govt.hu/dowerx/chat/server/api"
|
||||
"git.tek.govt.hu/dowerx/chat/server/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
userController, err := controller.MakeUserController()
|
||||
cfg := config.GetConfig()
|
||||
|
||||
err := api.Listen(cfg.API.Address, cfg.API.Base)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
userController.Register("", "", "")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user