Skip to content

Commit 00a1e58

Browse files
committed
feat: add user auth api
1 parent c5c6e71 commit 00a1e58

File tree

24 files changed

+128
-720
lines changed

24 files changed

+128
-720
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"editor.defaultFormatter": "golang.go",
3+
"editor.formatOnPaste": true,
4+
"editor.formatOnSave": true
5+
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ http://your-domain/logs
3333
- PostgreSQL Version `>= version 15.0`
3434

3535
## How To Use
36-
1. Clone the repository or **Use This Template**
36+
1. Clone the repository
3737
```bash
38-
git clone https://github.com/Caknoooo/go-gin-clean-starter.git
38+
git clone https://github.com/Lab-RPL-ITS/twitter-clone-api.git
3939
```
4040
2. Navigate to the project directory:
4141
```bash
42-
cd go-gin-clean-starter
42+
cd twitter-clone-api
4343
```
4444
3. Copy the example environment file and configure it:
4545
```bash

command/command.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import (
55
"os"
66
"strings"
77

8-
"github.com/Caknoooo/go-gin-clean-starter/constants"
9-
"github.com/Caknoooo/go-gin-clean-starter/migrations"
10-
"github.com/Caknoooo/go-gin-clean-starter/script"
8+
"github.com/Lab-RPL-ITS/twitter-clone-api/constants"
9+
"github.com/Lab-RPL-ITS/twitter-clone-api/migrations"
10+
"github.com/Lab-RPL-ITS/twitter-clone-api/script"
1111
"github.com/samber/do"
1212
"gorm.io/gorm"
1313
)
1414

1515
func Commands(injector *do.Injector) bool {
1616
db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB)
17-
17+
1818
var scriptName string
1919

2020
migrate := false

config/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/Caknoooo/go-gin-clean-starter/constants"
7+
"github.com/Lab-RPL-ITS/twitter-clone-api/constants"
88
"github.com/joho/godotenv"
99
"gorm.io/driver/postgres"
1010
"gorm.io/gorm"

controller/user_controller.go

Lines changed: 3 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package controller
33
import (
44
"net/http"
55

6-
"github.com/Caknoooo/go-gin-clean-starter/dto"
7-
"github.com/Caknoooo/go-gin-clean-starter/service"
8-
"github.com/Caknoooo/go-gin-clean-starter/utils"
6+
"github.com/Lab-RPL-ITS/twitter-clone-api/dto"
7+
"github.com/Lab-RPL-ITS/twitter-clone-api/service"
8+
"github.com/Lab-RPL-ITS/twitter-clone-api/utils"
99
"github.com/gin-gonic/gin"
1010
)
1111

@@ -14,11 +14,6 @@ type (
1414
Register(ctx *gin.Context)
1515
Login(ctx *gin.Context)
1616
Me(ctx *gin.Context)
17-
GetAllUser(ctx *gin.Context)
18-
SendVerificationEmail(ctx *gin.Context)
19-
VerifyEmail(ctx *gin.Context)
20-
Update(ctx *gin.Context)
21-
Delete(ctx *gin.Context)
2217
}
2318

2419
userController struct {
@@ -51,31 +46,6 @@ func (c *userController) Register(ctx *gin.Context) {
5146
ctx.JSON(http.StatusOK, res)
5247
}
5348

54-
func (c *userController) GetAllUser(ctx *gin.Context) {
55-
var req dto.PaginationRequest
56-
if err := ctx.ShouldBind(&req); err != nil {
57-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
58-
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
59-
return
60-
}
61-
62-
result, err := c.userService.GetAllUserWithPagination(ctx.Request.Context(), req)
63-
if err != nil {
64-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_LIST_USER, err.Error(), nil)
65-
ctx.JSON(http.StatusBadRequest, res)
66-
return
67-
}
68-
69-
resp := utils.Response{
70-
Status: true,
71-
Message: dto.MESSAGE_SUCCESS_GET_LIST_USER,
72-
Data: result.Data,
73-
Meta: result.PaginationResponse,
74-
}
75-
76-
ctx.JSON(http.StatusOK, resp)
77-
}
78-
7949
func (c *userController) Me(ctx *gin.Context) {
8050
userId := ctx.MustGet("user_id").(string)
8151

@@ -108,74 +78,3 @@ func (c *userController) Login(ctx *gin.Context) {
10878
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_LOGIN, result)
10979
ctx.JSON(http.StatusOK, res)
11080
}
111-
112-
func (c *userController) SendVerificationEmail(ctx *gin.Context) {
113-
var req dto.SendVerificationEmailRequest
114-
if err := ctx.ShouldBind(&req); err != nil {
115-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
116-
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
117-
return
118-
}
119-
120-
err := c.userService.SendVerificationEmail(ctx.Request.Context(), req)
121-
if err != nil {
122-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, err.Error(), nil)
123-
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
124-
return
125-
}
126-
127-
res := utils.BuildResponseSuccess(dto.MESSAGE_SEND_VERIFICATION_EMAIL_SUCCESS, nil)
128-
ctx.JSON(http.StatusOK, res)
129-
}
130-
131-
func (c *userController) VerifyEmail(ctx *gin.Context) {
132-
var req dto.VerifyEmailRequest
133-
if err := ctx.ShouldBind(&req); err != nil {
134-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
135-
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
136-
return
137-
}
138-
139-
result, err := c.userService.VerifyEmail(ctx.Request.Context(), req)
140-
if err != nil {
141-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_VERIFY_EMAIL, err.Error(), nil)
142-
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
143-
return
144-
}
145-
146-
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_VERIFY_EMAIL, result)
147-
ctx.JSON(http.StatusOK, res)
148-
}
149-
150-
func (c *userController) Update(ctx *gin.Context) {
151-
var req dto.UserUpdateRequest
152-
if err := ctx.ShouldBind(&req); err != nil {
153-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
154-
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
155-
return
156-
}
157-
158-
userId := ctx.MustGet("user_id").(string)
159-
result, err := c.userService.Update(ctx.Request.Context(), req, userId)
160-
if err != nil {
161-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_USER, err.Error(), nil)
162-
ctx.JSON(http.StatusBadRequest, res)
163-
return
164-
}
165-
166-
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_USER, result)
167-
ctx.JSON(http.StatusOK, res)
168-
}
169-
170-
func (c *userController) Delete(ctx *gin.Context) {
171-
userId := ctx.MustGet("user_id").(string)
172-
173-
if err := c.userService.Delete(ctx.Request.Context(), userId); err != nil {
174-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_USER, err.Error(), nil)
175-
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
176-
return
177-
}
178-
179-
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_USER, nil)
180-
ctx.JSON(http.StatusOK, res)
181-
}

dto/user_dto.go

Lines changed: 27 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,126 +3,56 @@ package dto
33
import (
44
"errors"
55
"mime/multipart"
6-
7-
"github.com/Caknoooo/go-gin-clean-starter/entity"
86
)
97

108
const (
119
// Failed
12-
MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body"
13-
MESSAGE_FAILED_REGISTER_USER = "failed create user"
14-
MESSAGE_FAILED_GET_LIST_USER = "failed get list user"
15-
MESSAGE_FAILED_GET_USER_TOKEN = "failed get user token"
16-
MESSAGE_FAILED_TOKEN_NOT_VALID = "token not valid"
17-
MESSAGE_FAILED_TOKEN_NOT_FOUND = "token not found"
18-
MESSAGE_FAILED_GET_USER = "failed get user"
19-
MESSAGE_FAILED_LOGIN = "failed login"
20-
MESSAGE_FAILED_WRONG_EMAIL_OR_PASSWORD = "wrong email or password"
21-
MESSAGE_FAILED_UPDATE_USER = "failed update user"
22-
MESSAGE_FAILED_DELETE_USER = "failed delete user"
23-
MESSAGE_FAILED_PROSES_REQUEST = "failed proses request"
24-
MESSAGE_FAILED_DENIED_ACCESS = "denied access"
25-
MESSAGE_FAILED_VERIFY_EMAIL = "failed verify email"
10+
MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body"
11+
MESSAGE_FAILED_REGISTER_USER = "failed create user"
12+
MESSAGE_FAILED_TOKEN_NOT_VALID = "token not valid"
13+
MESSAGE_FAILED_TOKEN_NOT_FOUND = "token not found"
14+
MESSAGE_FAILED_GET_USER = "failed get user"
15+
MESSAGE_FAILED_LOGIN = "failed login"
16+
MESSAGE_FAILED_PROSES_REQUEST = "failed proses request"
17+
MESSAGE_FAILED_DENIED_ACCESS = "denied access"
2618

2719
// Success
28-
MESSAGE_SUCCESS_REGISTER_USER = "success create user"
29-
MESSAGE_SUCCESS_GET_LIST_USER = "success get list user"
30-
MESSAGE_SUCCESS_GET_USER = "success get user"
31-
MESSAGE_SUCCESS_LOGIN = "success login"
32-
MESSAGE_SUCCESS_UPDATE_USER = "success update user"
33-
MESSAGE_SUCCESS_DELETE_USER = "success delete user"
34-
MESSAGE_SEND_VERIFICATION_EMAIL_SUCCESS = "success send verification email"
35-
MESSAGE_SUCCESS_VERIFY_EMAIL = "success verify email"
20+
MESSAGE_SUCCESS_REGISTER_USER = "success create user"
21+
MESSAGE_SUCCESS_GET_USER = "success get user"
22+
MESSAGE_SUCCESS_LOGIN = "success login"
3623
)
3724

3825
var (
39-
ErrCreateUser = errors.New("failed to create user")
40-
ErrGetAllUser = errors.New("failed to get all user")
41-
ErrGetUserById = errors.New("failed to get user by id")
42-
ErrGetUserByEmail = errors.New("failed to get user by email")
43-
ErrEmailAlreadyExists = errors.New("email already exist")
44-
ErrUpdateUser = errors.New("failed to update user")
45-
ErrUserNotAdmin = errors.New("user not admin")
46-
ErrUserNotFound = errors.New("user not found")
47-
ErrEmailNotFound = errors.New("email not found")
48-
ErrDeleteUser = errors.New("failed to delete user")
49-
ErrPasswordNotMatch = errors.New("password not match")
50-
ErrEmailOrPassword = errors.New("wrong email or password")
51-
ErrAccountNotVerified = errors.New("account not verified")
52-
ErrTokenInvalid = errors.New("token invalid")
53-
ErrTokenExpired = errors.New("token expired")
54-
ErrAccountAlreadyVerified = errors.New("account already verified")
26+
ErrCreateUser = errors.New("failed to create user")
27+
ErrGetUserById = errors.New("failed to get user by id")
28+
ErrUsernameAlreadyExists = errors.New("username already exist")
29+
ErrUsernameNotFound = errors.New("username not found")
30+
ErrPasswordNotMatch = errors.New("password not match")
5531
)
5632

5733
type (
5834
UserCreateRequest struct {
59-
Name string `json:"name" form:"name"`
60-
TelpNumber string `json:"telp_number" form:"telp_number"`
61-
Email string `json:"email" form:"email"`
62-
Image *multipart.FileHeader `json:"image" form:"image"`
63-
Password string `json:"password" form:"password"`
35+
Name string `json:"name" form:"name" binding:"required"`
36+
UserName string `json:"username" form:"username" binding:"required"`
37+
Bio string `json:"bio" form:"bio" binding:"required"`
38+
Image *multipart.FileHeader `json:"image" form:"image"`
39+
Password string `json:"password" form:"password" binding:"required"`
6440
}
6541

6642
UserResponse struct {
67-
ID string `json:"id"`
68-
Name string `json:"name"`
69-
Email string `json:"email"`
70-
TelpNumber string `json:"telp_number"`
71-
Role string `json:"role"`
72-
ImageUrl string `json:"image_url"`
73-
IsVerified bool `json:"is_verified"`
74-
}
75-
76-
UserPaginationResponse struct {
77-
Data []UserResponse `json:"data"`
78-
PaginationResponse
79-
}
80-
81-
GetAllUserRepositoryResponse struct {
82-
Users []entity.User `json:"users"`
83-
PaginationResponse
84-
}
85-
86-
UserUpdateRequest struct {
87-
Name string `json:"name" form:"name"`
88-
TelpNumber string `json:"telp_number" form:"telp_number"`
89-
Email string `json:"email" form:"email"`
90-
}
91-
92-
UserUpdateResponse struct {
93-
ID string `json:"id"`
94-
Name string `json:"name"`
95-
TelpNumber string `json:"telp_number"`
96-
Role string `json:"role"`
97-
Email string `json:"email"`
98-
IsVerified bool `json:"is_verified"`
99-
}
100-
101-
SendVerificationEmailRequest struct {
102-
Email string `json:"email" form:"email" binding:"required"`
103-
}
104-
105-
VerifyEmailRequest struct {
106-
Token string `json:"token" form:"token" binding:"required"`
107-
}
108-
109-
VerifyEmailResponse struct {
110-
Email string `json:"email"`
111-
IsVerified bool `json:"is_verified"`
43+
ID string `json:"id"`
44+
Name string `json:"name"`
45+
UserName string `json:"username"`
46+
Bio string `json:"bio"`
47+
ImageUrl *string `json:"image_url"`
11248
}
11349

11450
UserLoginRequest struct {
115-
Email string `json:"email" form:"email" binding:"required"`
51+
UserName string `json:"username" form:"username" binding:"required"`
11652
Password string `json:"password" form:"password" binding:"required"`
11753
}
11854

11955
UserLoginResponse struct {
12056
Token string `json:"token"`
121-
Role string `json:"role"`
122-
}
123-
124-
UpdateStatusIsVerifiedRequest struct {
125-
UserId string `json:"user_id" form:"user_id" binding:"required"`
126-
IsVerified bool `json:"is_verified" form:"is_verified"`
12757
}
12858
)

entity/user_entity.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package entity
22

33
import (
4-
"github.com/Caknoooo/go-gin-clean-starter/helpers"
4+
"github.com/Lab-RPL-ITS/twitter-clone-api/helpers"
55
"github.com/google/uuid"
66
"gorm.io/gorm"
77
)
88

99
type User struct {
10-
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
11-
Name string `json:"name"`
12-
TelpNumber string `json:"telp_number"`
13-
Email string `json:"email"`
14-
Password string `json:"password"`
15-
Role string `json:"role"`
16-
ImageUrl string `json:"image_url"`
17-
IsVerified bool `json:"is_verified"`
10+
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
11+
Name string `gorm:"not null" json:"name"`
12+
Username string `gorm:"not null" gorm:"unique" json:"username"`
13+
Bio string `gorm:"not null" json:"bio"`
14+
Password string `gorm:"not null" json:"password"`
15+
ImageUrl *string `json:"image_url"`
1816

1917
Timestamp
2018
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/Caknoooo/go-gin-clean-starter
1+
module github.com/Lab-RPL-ITS/twitter-clone-api
22

33
go 1.23.0
44

main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"log"
55
"os"
66

7-
"github.com/Caknoooo/go-gin-clean-starter/command"
8-
"github.com/Caknoooo/go-gin-clean-starter/middleware"
9-
"github.com/Caknoooo/go-gin-clean-starter/provider"
10-
"github.com/Caknoooo/go-gin-clean-starter/routes"
7+
"github.com/Lab-RPL-ITS/twitter-clone-api/command"
8+
"github.com/Lab-RPL-ITS/twitter-clone-api/middleware"
9+
"github.com/Lab-RPL-ITS/twitter-clone-api/provider"
10+
"github.com/Lab-RPL-ITS/twitter-clone-api/routes"
1111
"github.com/samber/do"
1212

1313
"github.com/common-nighthawk/go-figure"
@@ -42,7 +42,7 @@ func run(server *gin.Engine) {
4242
serve = ":" + port
4343
}
4444

45-
myFigure := figure.NewColorFigure("Caknoo", "", "green", true)
45+
myFigure := figure.NewColorFigure("Twitter Clone API", "", "green", true)
4646
myFigure.Print()
4747

4848
if err := server.Run(serve); err != nil {

0 commit comments

Comments
 (0)