Skip to content

Commit b7d7d84

Browse files
committed
Update README.md
1 parent c6f56f8 commit b7d7d84

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

README.md

+38-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Main layer: Entity -> Repository -> Service -> Controller
88
- [x] Support ORM database
99
- [x] Support database migration
1010
- [x] Support fake data
11+
- [x] Support JWT
1112

1213
## Main Packages
1314
- [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 {
3334
SocialId string `gorm:"type:varchar(255);unique;default:null"`
3435
Email string `gorm:"type:varchar(255);unique;not null"`
3536
Password string `gorm:"type:varchar(255);default:null"`
36-
EmailVerifiedAt time.Time
37+
EmailVerifiedAt *time.Time
3738
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"`
3940
Pic string `gorm:"type:varchar(255);not null;default:/assets/static/user.png"`
4041
Location string `gorm:"type:varchar(255);default:Indonesia"`
4142
Desc string `gorm:"type:varchar(255);default:null"`
@@ -81,7 +82,7 @@ func Seeder(db *gorm.DB) {
8182
## Route
8283
Like laravel, you can add your route in `route/api.go` or `route/web.go`.
8384

84-
For more docs follow this `https://docs.gofiber.io/guide/routing`
85+
For more follow this docs `https://docs.gofiber.io/guide/routing`
8586
```go
8687
route.Post("/create", userController.Create)
8788
route.Get("/show/:id", userController.Show)
@@ -113,16 +114,44 @@ func (controller *UserController) Route(route fiber.Router) {
113114
```
114115

115116
## 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.
120121

121-
You can create your own route and register the middleware:
122+
You can create your own middleware and use it:
122123
```go
124+
// Use middleware on route under /users path
123125
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)
124153
```
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`
126155

127156
## Access Database from Controller
128157
You can access the database object from fiber context in the controller directly. But this is not recommended.

0 commit comments

Comments
 (0)