Skip to content

Commit

Permalink
add user updates
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacharrisholt committed Sep 3, 2024
1 parent a6ea431 commit c6af58d
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 38 deletions.
36 changes: 34 additions & 2 deletions src/pevensie/auth.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argus
import gleam/dict
import gleam/dynamic.{type Decoder}
import gleam/option.{None, Some}
import gleam/option.{type Option, None, Some}
import gleam/result
import pevensie/drivers.{
type AuthDriver, type Connected, type Disabled, type Disconnected,
Expand All @@ -10,7 +10,9 @@ import pevensie/drivers.{
import pevensie/internal/auth
import pevensie/internal/pevensie.{type Pevensie}
import pevensie/internal/user.{
type User as InternalUser, type UserInsert as UserInsertInternal, UserInsert,
type User as InternalUser, type UserInsert as UserInsertInternal,
type UserUpdate as UserUpdateInternal, Set, UserInsert, UserUpdate,
default_user_update,
}

pub type User(user_metadata) =
Expand All @@ -19,6 +21,9 @@ pub type User(user_metadata) =
pub type UserInsert(user_metadata) =
UserInsertInternal(user_metadata)

pub type UserUpdate(user_metadata) =
UserUpdateInternal(user_metadata)

pub type AuthConfig(driver, user_metadata, connected) =
auth.AuthConfig(driver, user_metadata, connected)

Expand Down Expand Up @@ -135,3 +140,30 @@ pub fn create_user_with_email(
user_metadata_encoder,
)
}

pub fn set_user_role(
pevensie: Pevensie(
user_metadata,
auth_driver,
Connected,
cache_driver,
cache_status,
),
id: String,
role: Option(String),
) -> Result(User(user_metadata), Nil) {
let assert auth.AuthConfig(
driver:,
user_metadata_decoder:,
user_metadata_encoder:,
) = pevensie.auth_config

driver.update_user(
driver.driver,
"id",
id,
UserUpdate(..default_user_update(), role: Set(role)),
user_metadata_decoder,
user_metadata_encoder,
)
}
40 changes: 39 additions & 1 deletion src/pevensie/drivers.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleam/dynamic.{type Decoder}
import gleam/json
import gleam/option.{type Option}
import pevensie/internal/user.{type User, type UserInsert}
import pevensie/internal/user.{type User, type UserInsert, type UserUpdate}

pub type Connected

Expand All @@ -12,16 +12,45 @@ pub type Disabled
pub type Encoder(a) =
fn(a) -> json.Json

/// A function that connects the auth driver. This may
/// set up any connections or perform any other setup
/// required to make the driver ready to use.
type ConnectFunction(auth_driver) =
fn(auth_driver) -> Result(auth_driver, Nil)

/// A function that disconnects the auth driver. This may
/// tear down any connections or perform any other cleanup
/// required once the driver is no longer needed.
type DisconnectFunction(auth_driver) =
fn(auth_driver) -> Result(auth_driver, Nil)

/// A function that retrieves a user by the given field and value.
/// The first string argument is the field to search by, and the
/// second string argument is the value to search for.
type GetUserFunction(auth_driver, user_metadata) =
fn(auth_driver, String, String, Decoder(user_metadata)) ->
Result(User(user_metadata), Nil)

/// A function that updates a user by the given field and value.
/// The first string argument is the field to update, the second
/// string argument is the value to update it to, and the third
/// argument is the user update to apply.
type UpdateUserFunction(auth_driver, user_metadata) =
fn(
auth_driver,
String,
String,
UserUpdate(user_metadata),
Decoder(user_metadata),
Encoder(user_metadata),
) ->
Result(User(user_metadata), Nil)

/// A function that inserts a user into the database.
/// The first argument is the user to insert, the second
/// argument is the decoder to use to decode the user metadata,
/// and the third argument is the encoder to use to encode the
/// user metadata.
type InsertUserFunction(auth_driver, user_metadata) =
fn(
auth_driver,
Expand All @@ -31,13 +60,22 @@ type InsertUserFunction(auth_driver, user_metadata) =
) ->
Result(User(user_metadata), Nil)

/// A function that deletes a user from the database.
/// The first string argument is the field to delete by, and the
/// second string argument is the value to delete it by.
type DeleteUserFunction(auth_driver, user_metadata) =
fn(auth_driver, String, String, Decoder(user_metadata)) ->
Result(User(user_metadata), Nil)

pub type AuthDriver(driver, user_metadata) {
AuthDriver(
driver: driver,
connect: ConnectFunction(driver),
disconnect: DisconnectFunction(driver),
get_user: GetUserFunction(driver, user_metadata),
insert_user: InsertUserFunction(driver, user_metadata),
update_user: UpdateUserFunction(driver, user_metadata),
delete_user: DeleteUserFunction(driver, user_metadata),
)
}

Expand Down
Loading

0 comments on commit c6af58d

Please sign in to comment.