65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
|
package handler
|
||
|
|
||
|
import (
|
||
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/auth"
|
||
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/database"
|
||
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
func AuthSignup(c *gin.Context) {
|
||
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
||
|
|
||
|
var user models.User
|
||
|
if err := c.ShouldBindJSON(&user); err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
||
|
return
|
||
|
}
|
||
|
err := databaseRepo.CreateUser(c, &user)
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// return JWT
|
||
|
tokenString, err := auth.GenerateJWT(user.Username)
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": tokenString})
|
||
|
}
|
||
|
|
||
|
func AuthSignin(c *gin.Context) {
|
||
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
||
|
|
||
|
var user models.User
|
||
|
if err := c.ShouldBindJSON(&user); err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
foundUser, err := databaseRepo.GetUserByEmail(c, user.Username)
|
||
|
if err != nil || foundUser == nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = foundUser.CheckPassword(user.Password)
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusUnauthorized, gin.H{"success": false})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// return JWT
|
||
|
tokenString, err := auth.GenerateJWT(user.Username)
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": tokenString})
|
||
|
}
|