onasty/internal/transport/http/apiv1/auth.go (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com refactor: dont break code consitency (#23)..., 1 year ago
ss2316544@gmail.com refactor: dont break code consitency (#23)..., 1 year ago
| 1 | package apiv1 |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/gin-gonic/gin" |
| 8 | "github.com/olexsmir/onasty/internal/dtos" |
| 9 | "github.com/olexsmir/onasty/internal/models" |
| 10 | ) |
| 11 | |
| 12 | type signUpRequest struct { |
| 13 | Username string `json:"username"` |
| 14 | Email string `json:"email"` |
| 15 | Password string `json:"password"` |
| 16 | } |
| 17 | |
| 18 | func (a *APIV1) signUpHandler(c *gin.Context) { |
| 19 | var req signUpRequest |
| 20 | if err := c.ShouldBindJSON(&req); err != nil { |
| 21 | newError(c, http.StatusBadRequest, "invalid request") |
| 22 | return |
| 23 | } |
| 24 | |
| 25 | user := models.User{ //nolint:exhaustruct |
| 26 | Username: req.Username, |
| 27 | Email: req.Email, |
| 28 | Password: req.Password, |
| 29 | CreatedAt: time.Now(), |
| 30 | LastLoginAt: time.Now(), |
| 31 | } |
| 32 | if err := user.Validate(); err != nil { |
| 33 | // TODO: find a way to return all errors at once |
| 34 | newErrorStatus(c, http.StatusBadRequest, err.Error()) |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | if _, err := a.usersrv.SignUp(c.Request.Context(), dtos.CreateUserDTO{ |
| 39 | Username: user.Username, |
| 40 | Email: user.Email, |
| 41 | Password: user.Password, |
| 42 | CreatedAt: user.CreatedAt, |
| 43 | LastLoginAt: user.LastLoginAt, |
| 44 | }); err != nil { |
| 45 | errorResponse(c, err) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | c.Status(http.StatusCreated) |
| 50 | } |
| 51 | |
| 52 | type signInRequest struct { |
| 53 | Email string `json:"email"` |
| 54 | Password string `json:"password"` |
| 55 | } |
| 56 | |
| 57 | type signInResponse struct { |
| 58 | AccessToken string `json:"access_token"` |
| 59 | RefreshToken string `json:"refresh_token"` |
| 60 | } |
| 61 | |
| 62 | func (a *APIV1) signInHandler(c *gin.Context) { |
| 63 | var req signInRequest |
| 64 | if err := c.ShouldBindJSON(&req); err != nil { |
| 65 | newError(c, http.StatusBadRequest, "invalid request") |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | toks, err := a.usersrv.SignIn(c.Request.Context(), dtos.SignInDTO{ |
| 70 | Email: req.Email, |
| 71 | Password: req.Password, |
| 72 | }) |
| 73 | if err != nil { |
| 74 | errorResponse(c, err) |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | c.JSON(http.StatusOK, signInResponse{ |
| 79 | AccessToken: toks.Access, |
| 80 | RefreshToken: toks.Refresh, |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | type refreshTokenRequest struct { |
| 85 | RefreshToken string `json:"refresh_token"` |
| 86 | } |
| 87 | |
| 88 | func (a *APIV1) refreshTokensHandler(c *gin.Context) { |
| 89 | var req refreshTokenRequest |
| 90 | if err := c.ShouldBindJSON(&req); err != nil { |
| 91 | newError(c, http.StatusBadRequest, "invalid request") |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | toks, err := a.usersrv.RefreshTokens(c.Request.Context(), req.RefreshToken) |
| 96 | if err != nil { |
| 97 | errorResponse(c, err) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | c.JSON(http.StatusOK, signInResponse{ |
| 102 | AccessToken: toks.Access, |
| 103 | RefreshToken: toks.Refresh, |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | func (a *APIV1) verifyHandler(c *gin.Context) { |
| 108 | if err := a.usersrv.Verify(c.Request.Context(), c.Param("token")); err != nil { |
| 109 | errorResponse(c, err) |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | c.String(http.StatusOK, "email verified") |
| 114 | } |
| 115 | |
| 116 | func (a *APIV1) resendVerificationEmailHandler(c *gin.Context) { |
| 117 | var req signInRequest |
| 118 | if err := c.ShouldBindJSON(&req); err != nil { |
| 119 | newError(c, http.StatusBadRequest, "invalid request") |
| 120 | return |
| 121 | } |
| 122 | |
| 123 | if err := a.usersrv.ResendVerificationEmail(c.Request.Context(), dtos.SignInDTO{ |
| 124 | Email: req.Email, |
| 125 | Password: req.Password, |
| 126 | }); err != nil { |
| 127 | errorResponse(c, err) |
| 128 | return |
| 129 | } |
| 130 | |
| 131 | c.Status(http.StatusOK) |
| 132 | } |
| 133 | |
| 134 | func (a *APIV1) logOutHandler(c *gin.Context) { |
| 135 | if err := a.usersrv.Logout(c.Request.Context(), a.getUserID(c)); err != nil { |
| 136 | errorResponse(c, err) |
| 137 | return |
| 138 | } |
| 139 | |
| 140 | c.Status(http.StatusNoContent) |
| 141 | } |
| 142 | |
| 143 | type changePasswordRequest struct { |
| 144 | CurrentPassword string `json:"current_password"` |
| 145 | NewPassword string `json:"new_password"` |
| 146 | } |
| 147 | |
| 148 | func (a *APIV1) changePasswordHandler(c *gin.Context) { |
| 149 | var req changePasswordRequest |
| 150 | if err := c.ShouldBindJSON(&req); err != nil { |
| 151 | newError(c, http.StatusBadRequest, "invalid request") |
| 152 | return |
| 153 | } |
| 154 | |
| 155 | if err := a.usersrv.ChangePassword( |
| 156 | c.Request.Context(), |
| 157 | a.getUserID(c), |
| 158 | dtos.ResetUserPasswordDTO{ |
| 159 | CurrentPassword: req.CurrentPassword, |
| 160 | NewPassword: req.NewPassword, |
| 161 | }); err != nil { |
| 162 | errorResponse(c, err) |
| 163 | return |
| 164 | } |
| 165 | |
| 166 | c.Status(http.StatusOK) |
| 167 | } |