added login and test admin page
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"ccsecrets/models"
|
||||
"ccsecrets/utils/token"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -17,6 +18,11 @@ type RegisterInput struct {
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
type LoginInput struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
func Register(c *gin.Context) {
|
||||
var input RegisterInput
|
||||
|
||||
@@ -36,7 +42,7 @@ func Register(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"Error hashing password": err.Error()})
|
||||
return
|
||||
} else {
|
||||
fmt.Printf("Hashed password value is '%s'\n", string(hashedPassword))
|
||||
fmt.Printf("Register generated hashed password value '%s' from '%s'\n", string(hashedPassword), input.Password)
|
||||
}
|
||||
u.Password = string(hashedPassword)
|
||||
|
||||
@@ -52,3 +58,49 @@ func Register(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "registration success"})
|
||||
}
|
||||
|
||||
func Login(c *gin.Context) {
|
||||
|
||||
var input LoginInput
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
u := models.User{}
|
||||
|
||||
u.UserName = input.Username
|
||||
u.Password = input.Password
|
||||
|
||||
fmt.Printf("Login checking username '%s' and password '%s'\n", u.UserName, u.Password)
|
||||
|
||||
token, err := models.LoginCheck(u.UserName, u.Password)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "username or password is incorrect."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"token": token})
|
||||
|
||||
}
|
||||
|
||||
func CurrentUser(c *gin.Context) {
|
||||
|
||||
user_id, err := token.ExtractTokenID(c)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
u, err := models.GetUserByID(user_id)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "success", "data": u})
|
||||
}
|
||||
|
Reference in New Issue
Block a user