onasty/internal/transport/http/apiv1/auth.go(view raw)
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
package apiv1
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/olexsmir/onasty/internal/dtos"
"github.com/olexsmir/onasty/internal/models"
)
type signUpRequest struct {
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
}
func (a *APIV1) signUpHandler(c *gin.Context) {
var req signUpRequest
if err := c.ShouldBindJSON(&req); err != nil {
newError(c, http.StatusBadRequest, "invalid request")
return
}
user := models.User{ //nolint:exhaustruct
Username: req.Username,
Email: req.Email,
Password: req.Password,
CreatedAt: time.Now(),
LastLoginAt: time.Now(),
}
if err := user.Validate(); err != nil {
// TODO: find a way to return all errors at once
newErrorStatus(c, http.StatusBadRequest, err.Error())
return
}
if _, err := a.usersrv.SignUp(c.Request.Context(), dtos.CreateUserDTO{
Username: user.Username,
Email: user.Email,
Password: user.Password,
CreatedAt: user.CreatedAt,
LastLoginAt: user.LastLoginAt,
}); err != nil {
errorResponse(c, err)
return
}
c.Status(http.StatusCreated)
}
type signInRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
type signInResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
func (a *APIV1) signInHandler(c *gin.Context) {
var req signInRequest
if err := c.ShouldBindJSON(&req); err != nil {
newError(c, http.StatusBadRequest, "invalid request")
return
}
toks, err := a.usersrv.SignIn(c.Request.Context(), dtos.SignInDTO{
Email: req.Email,
Password: req.Password,
})
if err != nil {
errorResponse(c, err)
return
}
c.JSON(http.StatusOK, signInResponse{
AccessToken: toks.Access,
RefreshToken: toks.Refresh,
})
}
type refreshTokenRequest struct {
RefreshToken string `json:"refresh_token"`
}
func (a *APIV1) refreshTokensHandler(c *gin.Context) {
var req refreshTokenRequest
if err := c.ShouldBindJSON(&req); err != nil {
newError(c, http.StatusBadRequest, "invalid request")
return
}
toks, err := a.usersrv.RefreshTokens(c.Request.Context(), req.RefreshToken)
if err != nil {
errorResponse(c, err)
return
}
c.JSON(http.StatusOK, signInResponse{
AccessToken: toks.Access,
RefreshToken: toks.Refresh,
})
}
func (a *APIV1) verifyHandler(c *gin.Context) {
if err := a.usersrv.Verify(c.Request.Context(), c.Param("token")); err != nil {
errorResponse(c, err)
return
}
c.String(http.StatusOK, "email verified")
}
func (a *APIV1) resendVerificationEmailHandler(c *gin.Context) {
var req signInRequest
if err := c.ShouldBindJSON(&req); err != nil {
newError(c, http.StatusBadRequest, "invalid request")
return
}
if err := a.usersrv.ResendVerificationEmail(c.Request.Context(), dtos.SignInDTO{
Email: req.Email,
Password: req.Password,
}); err != nil {
errorResponse(c, err)
return
}
c.Status(http.StatusOK)
}
func (a *APIV1) logOutHandler(c *gin.Context) {
if err := a.usersrv.Logout(c.Request.Context(), a.getUserID(c)); err != nil {
errorResponse(c, err)
return
}
c.Status(http.StatusNoContent)
}
type changePasswordRequest struct {
CurrentPassword string `json:"current_password"`
NewPassword string `json:"new_password"`
}
func (a *APIV1) changePasswordHandler(c *gin.Context) {
var req changePasswordRequest
if err := c.ShouldBindJSON(&req); err != nil {
newError(c, http.StatusBadRequest, "invalid request")
return
}
if err := a.usersrv.ChangePassword(
c.Request.Context(),
dtos.ResetUserPasswordDTO{
UserID: a.getUserID(c),
CurrentPassword: req.CurrentPassword,
NewPassword: req.NewPassword,
}); err != nil {
errorResponse(c, err)
return
}
c.Status(http.StatusOK)
}
|