wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam

:boom: require json for login

+37 -33
+7
Taskfile.yml
··· 1 1 version: "3" 2 2 3 3 includes: 4 + db: ./taskfiles/PostgreTasks.yml 4 5 docker: ./taskfiles/DockerTasks.yml 5 6 gleam: ./taskfiles/GleamTasks.yml 6 7 dev: ./taskfiles/DevTasks.yml ··· 33 34 desc:  Rebuild the database empty 34 35 cmds: 35 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 36 43 37 44 run-container: 38 45 desc:  Run the container
+10 -27
src/app/domain/user/login.gleam
··· 8 8 import app/web 9 9 import app/web/context.{type Context} 10 10 import argus 11 - import formal/form 11 + import gleam/dynamic/decode 12 12 import gleam/float 13 13 import gleam/http 14 14 import gleam/http/response ··· 52 52 /// ``` 53 53 pub fn handle_request(request req: wisp.Request, ctx ctx: Context) { 54 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 55 + use body <- wisp.require_json(req) 61 56 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 - } 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) 68 60 } 69 61 } 70 62 ··· 93 85 wisp.set_cookie(response:, request: req, name:, value:, security:, max_age:) 94 86 } 95 87 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 - }) 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 110 93 } 111 94 112 95 fn handle_error(err: LoginError) -> response.Response(wisp.Body) {
+7 -1
test/app_test.gleam
··· 4 4 import envoy 5 5 import gleam/erlang/process 6 6 import gleam/http 7 + import gleam/json 7 8 import gleeunit 8 9 import global_value 9 10 import pog ··· 45 46 46 47 let login_req = 47 48 simulate.browser_request(http.Post, "/user/login") 48 - |> simulate.form_body([#("matricula", "000"), #("senha", "aluno")]) 49 + |> simulate.json_body( 50 + json.object([ 51 + #("matricula", json.string("000")), 52 + #("senha", json.string("aluno")), 53 + ]), 54 + ) 49 55 50 56 let login_resp = http_router.handle_request(login_req, ctx) 51 57
+13 -5
test/user_test.gleam
··· 21 21 22 22 let req = 23 23 simulate.browser_request(http.Post, "/user/login") 24 - |> simulate.form_body([#("matricula", "000"), #("senha", "aluno")]) 24 + |> simulate.json_body( 25 + json.object([ 26 + #("matricula", json.string("000")), 27 + #("senha", json.string("aluno")), 28 + ]), 29 + ) 25 30 26 31 let resp = http_router.handle_request(req, ctx) 27 32 assert resp.status == 200 as "Status should be 200" ··· 230 235 let path = "/user/profile/update" 231 236 let login_req = 232 237 simulate.browser_request(http.Post, login_path) 233 - |> simulate.form_body([ 234 - #("matricula", dummy_registration), 235 - #("senha", dummy_password), 236 - ]) 238 + |> simulate.json_body( 239 + json.object([ 240 + #("matricula", json.string(dummy_registration)), 241 + #("senha", json.string(dummy_password)), 242 + ]), 243 + ) 244 + 237 245 let login_resp = http_router.handle_request(login_req, ctx) 238 246 239 247 // UPDATING DUMMY ------------------------------------------------------------