···1717CREATE TABLE users
1818(
1919 id uuid PRIMARY KEY NOT NULL UNIQUE,
2020- email TEXT NOT NULL UNIQUE
2020+ email TEXT NOT NULL UNIQUE,
2121+ password_hash BLOB NOT NULL
2122);
22232324CREATE TABLE subscriptions
+2-1
db/schema.sql
···22CREATE TABLE users
33(
44 id uuid PRIMARY KEY NOT NULL UNIQUE,
55- email TEXT NOT NULL UNIQUE
55+ email TEXT NOT NULL UNIQUE,
66+ password_hash BLOB NOT NULL
67);
78CREATE TABLE subscriptions
89(
+2-1
dev/insert_test_data.gleam
···29293030 io.println("Inserting test data")
31313232- let test_user = user.new("rss@ollie.earth")
3232+ let password = user.hash_password("testing123")
3333+ let test_user = user.new("rss@ollie.earth", password)
33343435 let assert Ok(_) = database.add_user(test_user, into: database)
3536 as "Failed to insert test user"
+9-4
src/eater/database.gleam
···2121import eater/user
2222import gleam/dynamic/decode
2323import gleam/list
2424-import gleam/option.{type Option, None, Some}
2424+import gleam/option.{Some}
2525import gleam/result
2626import parrot/dev
2727import sqlight
···114114 user user: user.User,
115115 into on: sqlight.Connection,
116116) -> Result(Nil, sqlight.Error) {
117117- let #(sql, with) = sql.add_user(user.id |> uuid.to_bit_array, user.email)
117117+ let #(sql, with) =
118118+ sql.add_user(
119119+ id: user.id |> uuid.to_bit_array,
120120+ email: user.email,
121121+ password_hash: user.password_hash,
122122+ )
118123119124 let with = list.map(with, parrot_to_sqlight)
120125···137142 let assert Ok(id) = uuid.from_bit_array(user.id)
138143 as "invalid UUID from db UUID column?!"
139144140140- user.User(id, user.email)
145145+ user.User(id, user.email, user.password_hash)
141146 })
142147 |> Ok
143148}
···161166 let assert Ok(id) = uuid.from_bit_array(user.id)
162167 as "invalid UUID from db UUID column?!"
163168164164- user.User(id, user.email)
169169+ user.User(id, user.email, user.password_hash)
165170 |> Ok
166171 }
167172 }
···1313// This software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND. [cite: 5]
1414// See the Licence for the specific language governing permissions and limitations. [cite: 6]
15151616+import gleam/crypto
1617import youid/uuid
17181819pub type User {
1919- User(id: uuid.Uuid, email: String)
2020+ User(id: uuid.Uuid, email: String, password_hash: BitArray)
2021}
21222223/// creates a new user with a uuid
2324///
2424-pub fn new(email email: String) {
2525- User(uuid.v7(), email:)
2525+pub fn new(email email: String, password_hash password_hash: BitArray) {
2626+ User(uuid.v7(), email:, password_hash:)
2727+}
2828+2929+/// hash a string password
3030+///
3131+pub fn hash_password(password: String) {
3232+ crypto.hash(crypto.Sha256, <<password:utf8>>)
2633}