Skip to content

Commit

Permalink
Working on registration
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreRoba committed Jan 6, 2025
1 parent 5c5bbe8 commit 3eaf365
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
4 changes: 2 additions & 2 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ packages = [
{ name = "gleam_crypto", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "8AE56026B3E05EBB1F076778478A762E9EB62B31AEEB4285755452F397029D22" },
{ name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" },
{ name = "gleam_http", version = "3.7.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8A70D2F70BB7CFEB5DF048A2183FFBA91AF6D4CF5798504841744A16999E33D2" },
{ name = "gleam_json", version = "2.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "093214EB186A88D301795A94F0A8128C2E24CF1423997ED31A6C6CC67FC3E1A1" },
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
{ name = "gleam_otp", version = "0.16.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "FA0EB761339749B4E82D63016C6A18C4E6662DA05BAB6F1346F9AF2E679E301A" },
{ name = "gleam_regexp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "A3655FDD288571E90EE9C4009B719FEF59FA16AFCDF3952A76A125AF23CF1592" },
{ name = "gleam_stdlib", version = "0.51.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "14AFA8D3DDD7045203D422715DBB822D1725992A31DF35A08D97389014B74B68" },
{ name = "gleam_stdlib", version = "0.52.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "50703862DF26453B277688FFCDBE9DD4AC45B3BD9742C0B370DB62BC1629A07D" },
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "glisten", version = "7.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "028C0882EAC7ABEDEFBE92CE4D1FEDADE95FA81B1B1AB099C4F91C133BEF2C42" },
Expand Down
7 changes: 7 additions & 0 deletions src/realworld/auth/token.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub opaque type Token {
Token(val: String)
}

pub fn authenticate(email: String, password: String) -> Result(Token, String) {
todo
}
9 changes: 9 additions & 0 deletions src/realworld/models/user.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub type User {
User(
email: String,
token: String,
username: String,
bio: String,
image: String,
)
}
8 changes: 6 additions & 2 deletions src/realworld/router.gleam
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import realworld/auth/router.{user_handler}
import gleam/http
import realworld/users/web.{get_user, login, register, update_user}
import wisp.{type Request, type Response}

pub fn handle_request(req: Request) -> Response {
case wisp.path_segments(req) {
["users", ..] | ["user", ..] -> user_handler(req)
["user"] if req.method == http.Get -> get_user(req)
["user"] if req.method == http.Put -> update_user(req)
["users"] if req.method == http.Post -> register(req)
["users", "login"] if req.method == http.Post -> login(req)
_ -> wisp.not_found()
}
}
38 changes: 7 additions & 31 deletions src/realworld/auth/router.gleam → src/realworld/users/web.gleam
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import gleam/dynamic/decode
import gleam/http
import gleam/json
import gleam/result
import wisp.{type Request, type Response}

pub fn user_handler(req: Request) -> Response {
case wisp.path_segments(req) {
["users"] -> {
use <- wisp.require_method(req, http.Post)
register(req)
}
["users", "login"] -> {
use <- wisp.require_method(req, http.Post)
login(req)
}
["user"] -> user(req)
_ -> wisp.not_found()
}
}

fn register(req: Request) -> Response {
pub fn register(req: Request) -> Response {
use json <- wisp.require_json(req)
let result = {
use data <- result.try(decode.run(json, registration_decoder()))
Expand All @@ -44,15 +28,15 @@ fn register(req: Request) -> Response {
}
}

pub type RegistrationRequestJson {
type RegistrationRequestJson {
RegistrationRequestJson(user: UserRegistrationRequestJson)
}

pub type UserRegistrationRequestJson {
type UserRegistrationRequestJson {
UserRegistrationRequestJson(email: String, password: String, username: String)
}

pub fn registration_decoder() {
fn registration_decoder() {
let user_registration_decoder = {
use email <- decode.field("email", decode.string)
use password <- decode.field("password", decode.string)
Expand All @@ -63,22 +47,14 @@ pub fn registration_decoder() {
decode.success(RegistrationRequestJson(user:))
}

fn login(_req: Request) -> Response {
pub fn login(_req: Request) -> Response {
todo
}

fn user(req: Request) -> Response {
case req.method {
http.Get -> get_user(req)
http.Put -> put_user(req)
_ -> wisp.method_not_allowed([http.Get, http.Post])
}
}

fn get_user(_req: Request) -> Response {
pub fn get_user(_req: Request) -> Response {
todo
}

fn put_user(_req: Request) -> Response {
pub fn update_user(_req: Request) -> Response {
todo
}

0 comments on commit 3eaf365

Please # to comment.