Skip to content

Commit

Permalink
refactor: move migrations to private folder
Browse files Browse the repository at this point in the history
  • Loading branch information
soulsam480 committed Nov 2, 2024
1 parent 7bcc90d commit ce0130d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
File renamed without changes.
12 changes: 4 additions & 8 deletions src/app/controllers/sessions.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ fn handle_login(req: Request, ctx: config.Context) -> Response {
// read it when looking for user in auth hook
wisp.ok()
|> wisp.json_body(user_serializer.run(user))
|> wisp.set_cookie(
req,
auth.cookie_name,
user.email,
wisp.Signed,
auth.cookie_max_age,
)
|> auth.set_cookie(req, user)
}

False -> {
Expand Down Expand Up @@ -103,7 +97,9 @@ fn handle_register(req: Request, ctx: config.Context) {
}

Ok(new_user) -> {
wisp.ok() |> wisp.json_body(user_serializer.run(new_user))
wisp.ok()
|> wisp.json_body(user_serializer.run(new_user))
|> auth.set_cookie(req, new_user)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/app/db/migrator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import app/db/connection
import app/lib/logger
import feather
import feather/migrate as migrator
import wisp

pub fn migrate_to_latest() {
logger.info("Fetching migrations...")

let assert Ok(migrations) = migrator.get_migrations("src/app/db/migrations")
let assert Ok(priv_dir) = wisp.priv_directory("okane")

let assert Ok(migrations) = migrator.get_migrations(priv_dir <> "/migrations")

logger.info("Acquiring connection...")

Expand Down
41 changes: 31 additions & 10 deletions src/app/hooks/auth.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ pub const cookie_max_age = 604_800

pub const cookie_name = "__session"

pub fn get_cookie(
req: wisp.Request,
with: fn(String) -> wisp.Response,
) -> wisp.Response {
let cookie_res = wisp.get_cookie(req, cookie_name, wisp.Signed)

case cookie_res {
Ok(c) -> with(c)
Error(_) -> {
response_helpers.unauthorized()
|> wisp.json_body(base_serializer.serialize_error(
"Invalid token or token not found",
))
}
}
}

pub fn set_cookie(res: wisp.Response, req: wisp.Request, user: user.User) {
wisp.set_cookie(
res,
req,
cookie_name,
user.email,
wisp.Signed,
cookie_max_age,
)
}

/// session/auth hook
/// 1. check if cookie is present
/// 2. find user if there and put it inside context
Expand All @@ -18,22 +46,15 @@ pub fn hook(
ctx: config.Context,
handle: fn(config.Context) -> wisp.Response,
) -> wisp.Response {
wisp.get_cookie(req, cookie_name, wisp.Signed)
use user_email <- get_cookie(req)

user.find_by_email(user_email, ctx.db)
|> result.map_error(fn(_) {
response_helpers.unauthorized()
|> wisp.json_body(base_serializer.serialize_error(
"Invalid token or token not found",
))
})
|> result.try(fn(user_email) {
user.find_by_email(user_email, ctx.db)
|> result.map_error(fn(_) {
response_helpers.unauthorized()
|> wisp.json_body(base_serializer.serialize_error(
"Invalid token or token not found",
))
})
})
|> result.map(fn(user) { config.set_user(ctx, user) |> handle })
|> result.unwrap_error(wisp.internal_server_error())
}

0 comments on commit ce0130d

Please sign in to comment.