Skip to content

Commit

Permalink
feat: add reset password email template
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgleam committed Oct 2, 2024
1 parent 5c97f4a commit bd8d1d1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
43 changes: 43 additions & 0 deletions src/app/models/email.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,46 @@ pub fn send_verify_user(

Ok(data)
}

pub fn send_forgot_password(
email_api_key: String,
to: String,
reset_password_link: String,
) -> Result(ApiData, AppError) {
// Create an email to send

let body =
[templates.forgot_password_confirmation(reset_password_link)]
|> curry2(layout)("Forgot Password Confirmation")
|> element.to_string

let email =
zeptomail.Email(
from: Addressee("Planktonsoft", "[email protected]"),
to: [Addressee(to, to)],
reply_to: [],
cc: [],
bcc: [],
body: zeptomail.HtmlBody(body),
subject: "Crappy Board: Forgot Password Confirmation",
)

// Prepare an API request that sends the email
let request = zeptomail.email_request(email, email_api_key)

// Send the API request using `gleam_httpc`
let assert Ok(response) = httpc.send(request)

// Parse the API response to verify success
use data <- result.then(
zeptomail.decode_email_response(response)
|> result.map_error(fn(error) {
io.debug(error)
case error {
_ -> error.ApiError(error)
}
}),
)

Ok(data)
}
4 changes: 2 additions & 2 deletions src/app/routes/user_routes.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import app/helpers/constant
import app/helpers/uuid
import app/models/board.{create_board}
import app/models/board_user.{create_board_user, list_board_user}
import app/models/email.{send_verify_user}
import app/models/email.{send_forgot_password, send_verify_user}
import app/models/user.{create_user, get_user_by_email, signin_user}
import app/pages
import app/pages/layout.{layout}
Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn post_forgot_password(req: Request, ctx: Context) {

let reset_password_link = ctx.base_url <> "/reset-password?token=" <> token

send_verify_user(ctx.email_api_key, user_email, reset_password_link)
send_forgot_password(ctx.email_api_key, user_email, reset_password_link)
}

[pages.submit_forgot_password()]
Expand Down
5 changes: 5 additions & 0 deletions src/app/templates.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import app/templates/forgot_password_confirmation
import app/templates/signup_confirmation

pub fn signup_confirmation(confirmation_link: String) {
signup_confirmation.root(confirmation_link)
}

pub fn forgot_password_confirmation(reset_password_link: String) {
forgot_password_confirmation.root(reset_password_link)
}
17 changes: 17 additions & 0 deletions src/app/templates/forgot_password_confirmation.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import lustre/attribute.{attribute}
import lustre/element.{type Element}
import lustre/element/html

pub fn root(reset_password_link: String) -> Element(t) {
html.div([attribute.class("email-container")], [
html.h1([], [element.text("Forgot Password Confirmation")]),
html.p([], [element.text("Please click the button below.")]),
html.a([attribute.href(reset_password_link)], [
element.text("Forgot Password"),
]),
html.p([], [
element.text("If you didn’t forgot password, ignore this email."),
]),
html.footer([], [element.text("© 2024 Planktonsoft. All rights reserved.")]),
])
}

0 comments on commit bd8d1d1

Please sign in to comment.