go-jwtserver/README.md

95 lines
1.6 KiB
Markdown
Raw Normal View History

2020-07-16 10:51:50 +02:00
# Go-JWTServer
2020-07-16 14:36:55 +02:00
Go-JWTServer met a disposition :
- Un serveur de stockage de utilisateur sous postgres et d'authenfication Jwt
- Un middleware d'authentification Jwt
2020-07-16 10:51:50 +02:00
2020-07-16 14:36:55 +02:00
## Serveur
2020-07-16 10:51:50 +02:00
2020-07-16 14:36:55 +02:00
### Configuration
2020-07-16 10:51:50 +02:00
Editer le ficher `.env`
```
## Server
web_adress=":3001"
## Postgres
db_user="jwtserver"
db_pass="jwtserver"
db_name="jwtserver"
db_host="localhost"
## JWT
token_password="NotSoSecretJwtSecretPassword"
```
2020-07-16 14:36:55 +02:00
### Executer le serveur
Lancer le conteneur postgres
2020-07-16 14:43:26 +02:00
2020-07-16 14:48:26 +02:00
```
make up
```
2020-07-16 14:36:55 +02:00
Dans une autre console, lancer le serveur jwt
2020-07-16 14:43:26 +02:00
2020-07-16 14:48:26 +02:00
```
make run
```
2020-07-16 14:36:55 +02:00
## Middleware
Le middleware permet d'enregistrer et d'authentifier un utilisateur et
de vérifier la validité du token Jwt
##### Exemple
```
2020-07-16 14:43:26 +02:00
r := chi.NewRouter()
2020-07-16 14:36:55 +02:00
2020-07-16 14:43:26 +02:00
//add Jwt Authentification
r.Use(jwtmiddleware.JwtAuthentication)
2020-07-16 14:36:55 +02:00
2020-07-16 14:43:26 +02:00
.Route("/api/", func(r chi.Router) {
//Middleware routes
r.Post("/user/new", jwtmiddleware.CreateAccount)
r.Post("/user/login", jwtmiddleware.Authenticate)
})
2020-07-16 14:36:55 +02:00
```
2020-07-16 10:51:50 +02:00
## API
2020-07-16 14:36:55 +02:00
##### Enregistrer un utilisateur
2020-07-16 10:51:50 +02:00
```
POST {{host}}/api/user/new
content-type: application/json
{
"email": "test@test.com",
"password": "test"
}
```
2020-07-16 14:36:55 +02:00
##### Authentifier un utilisateur
2020-07-16 10:51:50 +02:00
```
POST {{host}}/api/user/login
content-type: application/json
{
"email": "test@test.com",
"password": "test"
}
```
2020-07-16 14:36:55 +02:00
##### Réponse
2020-07-16 10:51:50 +02:00
```
{
"account": {
"ID": 1,
"CreatedAt": "2020-07-15T14:08:22.288502Z",
"UpdatedAt": "2020-07-15T14:08:22.288502Z",
"DeletedAt": null,
"email": "test@test.com",
"password": "",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjF9.-bV_jRNcykDMsI-vjxKbiNBsEwqSfDspEEjBTE2nds8"
},
"message": "Logged In",
"status": true
}
```