···11import gleam/bit_array
22+import gleam/bool
23import gleam/crypto
34import gleam/list
45import gleam/result
···1718 WrongPassword
1819 /// User not found in the Databse
1920 NotFound
2121+ /// User account is inactive
2222+ AccountDisabled
2023}
21242225pub const hash_algorithm = crypto.Sha512
···4649 |> result.replace_error(NotFound),
4750 )
48515252+ use <- bool.guard(when: !row.is_active, return: Error(AccountDisabled))
5353+4954 let input_bits =
5055 hash(value: password, salt: ctx.secret_key)
5156 |> bit_array.from_string
···8186 case err {
8287 Database(_) -> wisp.internal_server_error()
8388 NotFound -> wisp.not_found()
8989+9090+ AccountDisabled ->
9191+ "Account is disabled"
9292+ |> wisp.Text
9393+ |> wisp.set_body(wisp.response(401), _)
9494+8495 WrongPassword ->
8596 "Wrong password"
8697 |> wisp.Text
+9-3
server/src/server/auth/sql.gleam
···1515/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
1616///
1717pub type AuthenticateRow {
1818- AuthenticateRow(id: Uuid, password_hash: String)
1818+ AuthenticateRow(id: Uuid, password_hash: String, is_active: Bool)
1919}
20202121/// ๎นณ authenticate an user
···3030 let decoder = {
3131 use id <- decode.field(0, uuid_decoder())
3232 use password_hash <- decode.field(1, decode.string)
3333- decode.success(AuthenticateRow(id:, password_hash:))
3333+ use is_active <- decode.field(2, decode.bool)
3434+ decode.success(AuthenticateRow(id:, password_hash:, is_active:))
3435 }
35363637 "-- ๎นณ authenticate an user
3737-select u.id, u.password_hash from public.user_account as u where u.email = $1;
3838+select
3939+ u.id,
4040+ u.password_hash,
4141+ u.is_active
4242+from public.user_account as u
4343+where u.email = $1;
3844"
3945 |> pog.query
4046 |> pog.parameter(pog.text(arg_1))
+6-1
server/src/server/auth/sql/authenticate.sql
···11-- ๎นณ authenticate an user
22-select u.id, u.password_hash from public.user_account as u where u.email = $1;
22+select
33+ u.id,
44+ u.password_hash,
55+ u.is_active
66+from public.user_account as u
77+where u.email = $1;