@@ -8,6 +8,7 @@ Main layer: Entity -> Repository -> Service -> Controller
8
8
- [x] Support ORM database
9
9
- [x] Support database migration
10
10
- [x] Support fake data
11
+ - [x] Support JWT
11
12
12
13
## Main Packages
13
14
- [x] Gorm: The fantastic ORM library for Golang, aims to be developer friendly. ` github.com/go-gorm/gorm `
@@ -33,9 +34,9 @@ type User struct {
33
34
SocialId string ` gorm:"type:varchar(255);unique;default:null"`
34
35
Email string ` gorm:"type:varchar(255);unique;not null"`
35
36
Password string ` gorm:"type:varchar(255);default:null"`
36
- EmailVerifiedAt time.Time
37
+ EmailVerifiedAt * time.Time
37
38
Nick string ` gorm:"type:varchar(50);unique;not null"`
38
- Name string ` gorm:"type:varchar(255);not null"`
39
+ Name string ` gorm:"type:varchar(255);index:,class:FULLTEXT; not null"`
39
40
Pic string ` gorm:"type:varchar(255);not null;default:/assets/static/user.png"`
40
41
Location string ` gorm:"type:varchar(255);default:Indonesia"`
41
42
Desc string ` gorm:"type:varchar(255);default:null"`
@@ -81,7 +82,7 @@ func Seeder(db *gorm.DB) {
81
82
## Route
82
83
Like laravel, you can add your route in ` route/api.go ` or ` route/web.go ` .
83
84
84
- For more docs follow this ` https://docs.gofiber.io/guide/routing `
85
+ For more follow this docs ` https://docs.gofiber.io/guide/routing `
85
86
``` go
86
87
route.Post (" /create" , userController.Create )
87
88
route.Get (" /show/:id" , userController.Show )
@@ -113,16 +114,44 @@ func (controller *UserController) Route(route fiber.Router) {
113
114
```
114
115
115
116
## Middleware
116
- There are 3 default middleware :
117
- - [x] APPMiddleware: affect all routes including api and web.
118
- - [x] APIMiddleware: affect api route only.
119
- - [x] WebMiddleware: affect web route only.
117
+ There are 3 default middlewares :
118
+ - [x] APPMiddleware: Used by all routes including api and web.
119
+ - [x] APIMiddleware: Used by api route only.
120
+ - [x] WebMiddleware: Used by web route only.
120
121
121
- You can create your own route and register the middleware :
122
+ You can create your own middleware and use it :
122
123
``` go
124
+ // Use middleware on route under /users path
123
125
route.Use (" /users" , middleware.Authenticate )
126
+
127
+ // Use middleware on specific route
128
+ route.Post (" /update/:id" , middleware.Authenticate , controller.Update )
129
+ ```
130
+ For more follow this docs ` https://docs.gofiber.io/guide/routing#middleware `
131
+
132
+ ## JWT
133
+ There is helper ` jwt.go ` that you can use to create or parse the token:
134
+ ``` go
135
+ // Create token
136
+ type LoginUserResponse struct {
137
+ Id uint ` json:"id"`
138
+ Email string ` json:"email"`
139
+ Name string ` json:"name"`
140
+ jwt.StandardClaims
141
+ }
142
+ data := model.LoginUserResponse {
143
+ Id : user.ID ,
144
+ Email : user.Email ,
145
+ Name : user.Name ,
146
+ }
147
+ token := helper.MakeECDSAToken (&data, jwt.SigningMethodES256 )
148
+
149
+ // Parsing the token
150
+ token := helper.ParseECDSAToken (request.Token , jwt.SigningMethodES256 )
151
+ claims := token.Claims .(jwt.MapClaims )
152
+ email := claims[" email" ].(string )
124
153
```
125
- For more docs follow this ` https://docs.gofiber.io/guide/routing#middleware `
154
+ For more follow this docs ` https://github.com/golang-jwt/jwt `
126
155
127
156
## Access Database from Controller
128
157
You can access the database object from fiber context in the controller directly. But this is not recommended.
0 commit comments