Skip to content

Commit 271987a

Browse files
committed
Added jwt plugin usage to example
1 parent 6538b83 commit 271987a

File tree

8 files changed

+65
-1
lines changed

8 files changed

+65
-1
lines changed

example/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@ psql -U postgres
2727
create database dbservice_example;
2828
\c dbservice_example
2929
30+
CREATE EXTENSION pgcrypto;
31+
3032
create table products(
3133
id serial,
3234
name text not null,
3335
price integer not null,
3436
status text
3537
);
38+
39+
create table users(
40+
id serial,
41+
name text not null,
42+
email text not null,
43+
password text not null
44+
);
3645
```
3746
Try different requests
3847
----------------------

example/routes

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ post /products, name: 'create_product'
55
put /products/:id, name: 'update_product'
66

77
post /login, name: 'login' | jwt
8+
post /sign_up, name: 'sign_up'
9+
get /current_user, name: 'current_user'

example/schemas/login.schema

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"title": "User login",
3+
"description": "User login",
4+
"type": "object",
5+
"properties": {
6+
"email": {
7+
"description": "User email",
8+
"type": "string",
9+
"minLength": 1
10+
},
11+
"password": {
12+
"description": "User password",
13+
"type": "string",
14+
"minLength": 1
15+
}
16+
},
17+
"required": ["email", "password"]
18+
}

example/schemas/sign_up.schema

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"title": "User signup",
3+
"description": "User signup",
4+
"type": "object",
5+
"properties": {
6+
"email": {
7+
"description": "User email",
8+
"type": "string",
9+
"minLength": 1
10+
},
11+
"name": {
12+
"description": "User name",
13+
"type": "string",
14+
"minLength": 1
15+
},
16+
"password": {
17+
"description": "User password",
18+
"type": "string",
19+
"minLength": 1
20+
}
21+
},
22+
"required": ["email", "name", "password"]
23+
}

example/sql/current_user.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{ if .jwt.user_id }}
2+
select id, name, email from users where id={{.jwt.user_id | quote}}
3+
{{ else }}
4+
select false as logged_in
5+
{{ end }}

example/sql/login.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
select json_object('{user_id, 443}') as __jwt, true as success
1+
select id, name, email, json_build_object('user_id', id) as __jwt from users where email = lower({{.params.email | quote }}) AND password = crypt({{.params.password | quote }}, password)

example/sql/sign_up.sql

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INSERT INTO users (name, email, password) VALUES
2+
({{.params.name | quote}}, {{.params.email | quote}}, crypt({{.params.password | quote}}, gen_salt('bf', 10)));

main.go

+5
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ func goThroughPipelines(api *Api,
219219
}
220220
data = response.Data
221221
}
222+
dataJson, err := json.Marshal(data)
223+
if err != nil {
224+
return err
225+
}
226+
fmt.Fprint(w, string(dataJson))
222227
return nil
223228
}
224229

0 commit comments

Comments
 (0)