tangled
alpha
login
or
join now
kacaii.dev
/
senac-brigade-server
0
fork
atom
wip: currently rewriting the project as a full stack application
tangled.org/kacaii.dev/sigo
gleam
0
fork
atom
overview
issues
1
pulls
pipelines
:boom: require json for login
kacaii.dev
2 months ago
dc256e25
6846a002
+37
-33
4 changed files
expand all
collapse all
unified
split
Taskfile.yml
src
app
domain
user
login.gleam
test
app_test.gleam
user_test.gleam
+7
Taskfile.yml
···
1
version: "3"
2
3
includes:
0
4
docker: ./taskfiles/DockerTasks.yml
5
gleam: ./taskfiles/GleamTasks.yml
6
dev: ./taskfiles/DevTasks.yml
···
33
desc: Rebuild the database empty
34
cmds:
35
- task: db:rebuild-empty
0
0
0
0
0
0
36
37
run-container:
38
desc: Run the container
···
1
version: "3"
2
3
includes:
4
+
db: ./taskfiles/PostgreTasks.yml
5
docker: ./taskfiles/DockerTasks.yml
6
gleam: ./taskfiles/GleamTasks.yml
7
dev: ./taskfiles/DevTasks.yml
···
34
desc: Rebuild the database empty
35
cmds:
36
- task: db:rebuild-empty
37
+
38
+
rebuild-with-seed:
39
+
desc: Rebuild the database with a default admin
40
+
cmds:
41
+
- task: rebuild-empty
42
+
- task: dev:seed
43
44
run-container:
45
desc: Run the container
+10
-27
src/app/domain/user/login.gleam
···
8
import app/web
9
import app/web/context.{type Context}
10
import argus
11
-
import formal/form
12
import gleam/float
13
import gleam/http
14
import gleam/http/response
···
52
/// ```
53
pub fn handle_request(request req: wisp.Request, ctx ctx: Context) {
54
use <- wisp.require_method(req, http.Post)
55
-
56
-
use form_data <- wisp.require_form(req)
57
-
let form_result =
58
-
login_form()
59
-
|> form.add_values(form_data.values)
60
-
|> form.run
61
62
-
case form_result {
63
-
Error(_) -> wisp.unprocessable_content()
64
-
Ok(data) -> {
65
-
let cookie = user.uuid_cookie_name
66
-
handle_login(req, ctx, data, cookie)
67
-
}
68
}
69
}
70
···
93
wisp.set_cookie(response:, request: req, name:, value:, security:, max_age:)
94
}
95
96
-
/// A form that decodes the `LogIn` value.
97
-
fn login_form() -> form.Form(RequestBody) {
98
-
form.new({
99
-
use registration <- form.field("matricula", {
100
-
form.parse_string |> form.check_not_empty()
101
-
})
102
-
103
-
use password <- form.field("senha", {
104
-
form.parse_string |> form.check_not_empty()
105
-
})
106
-
107
-
RequestBody(registration:, password:)
108
-
|> form.success
109
-
})
110
}
111
112
fn handle_error(err: LoginError) -> response.Response(wisp.Body) {
···
8
import app/web
9
import app/web/context.{type Context}
10
import argus
11
+
import gleam/dynamic/decode
12
import gleam/float
13
import gleam/http
14
import gleam/http/response
···
52
/// ```
53
pub fn handle_request(request req: wisp.Request, ctx ctx: Context) {
54
use <- wisp.require_method(req, http.Post)
55
+
use body <- wisp.require_json(req)
0
0
0
0
0
56
57
+
case decode.run(body, body_decoder()) {
58
+
Error(err) -> web.handle_decode_error(err)
59
+
Ok(data) -> handle_login(req, ctx, data, user.uuid_cookie_name)
0
0
0
60
}
61
}
62
···
85
wisp.set_cookie(response:, request: req, name:, value:, security:, max_age:)
86
}
87
88
+
fn body_decoder() -> decode.Decoder(RequestBody) {
89
+
use registration <- decode.field("matricula", decode.string)
90
+
use password <- decode.field("senha", decode.string)
91
+
RequestBody(registration:, password:)
92
+
|> decode.success
0
0
0
0
0
0
0
0
0
93
}
94
95
fn handle_error(err: LoginError) -> response.Response(wisp.Body) {
+7
-1
test/app_test.gleam
···
4
import envoy
5
import gleam/erlang/process
6
import gleam/http
0
7
import gleeunit
8
import global_value
9
import pog
···
45
46
let login_req =
47
simulate.browser_request(http.Post, "/user/login")
48
-
|> simulate.form_body([#("matricula", "000"), #("senha", "aluno")])
0
0
0
0
0
49
50
let login_resp = http_router.handle_request(login_req, ctx)
51
···
4
import envoy
5
import gleam/erlang/process
6
import gleam/http
7
+
import gleam/json
8
import gleeunit
9
import global_value
10
import pog
···
46
47
let login_req =
48
simulate.browser_request(http.Post, "/user/login")
49
+
|> simulate.json_body(
50
+
json.object([
51
+
#("matricula", json.string("000")),
52
+
#("senha", json.string("aluno")),
53
+
]),
54
+
)
55
56
let login_resp = http_router.handle_request(login_req, ctx)
57
+13
-5
test/user_test.gleam
···
21
22
let req =
23
simulate.browser_request(http.Post, "/user/login")
24
-
|> simulate.form_body([#("matricula", "000"), #("senha", "aluno")])
0
0
0
0
0
25
26
let resp = http_router.handle_request(req, ctx)
27
assert resp.status == 200 as "Status should be 200"
···
230
let path = "/user/profile/update"
231
let login_req =
232
simulate.browser_request(http.Post, login_path)
233
-
|> simulate.form_body([
234
-
#("matricula", dummy_registration),
235
-
#("senha", dummy_password),
236
-
])
0
0
0
237
let login_resp = http_router.handle_request(login_req, ctx)
238
239
// UPDATING DUMMY ------------------------------------------------------------
···
21
22
let req =
23
simulate.browser_request(http.Post, "/user/login")
24
+
|> simulate.json_body(
25
+
json.object([
26
+
#("matricula", json.string("000")),
27
+
#("senha", json.string("aluno")),
28
+
]),
29
+
)
30
31
let resp = http_router.handle_request(req, ctx)
32
assert resp.status == 200 as "Status should be 200"
···
235
let path = "/user/profile/update"
236
let login_req =
237
simulate.browser_request(http.Post, login_path)
238
+
|> simulate.json_body(
239
+
json.object([
240
+
#("matricula", json.string(dummy_registration)),
241
+
#("senha", json.string(dummy_password)),
242
+
]),
243
+
)
244
+
245
let login_resp = http_router.handle_request(login_req, ctx)
246
247
// UPDATING DUMMY ------------------------------------------------------------