-
Notifications
You must be signed in to change notification settings - Fork 0
Added contact form support in backend #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MeleMannen
wants to merge
6
commits into
main
Choose a base branch
from
144-create-a-new-send-email-route-that-sends-an-email-from-noreplyvektorprogrammetno-to-an-email-address
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a0be815
Added a mail-service and a email endpoint to send an email from the w…
MeleMannen 1bef2b6
Created a mail-service and a contact router, to be able to send mails…
MeleMannen a71410c
Added extra contact form proctection.
MeleMannen d5e3255
Made nodemailer use https.
MeleMannen 30dc227
Removed hardcoded email.
MeleMannen 4b6c524
Fixed lint and biome ci issues.
MeleMannen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { clientError } from "@/src/error/http-errors"; | ||
| import { sendEmail } from "@/src/services/mail-service"; | ||
| import { Router, json } from "express"; | ||
| import { z } from "zod"; | ||
|
|
||
| export const contactRouter = Router(); | ||
| contactRouter.use(json()); | ||
|
|
||
| /** | ||
| * @openapi | ||
| * /contact: | ||
| * post: | ||
| * tags: [contact] | ||
| * summary: Send a contact email | ||
| * description: Send an email to contact, using the configured mail service. | ||
| * requestBody: | ||
| * required: true | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * type: object | ||
| * required: | ||
| * - receivingEmail | ||
| * - replyTo | ||
| * - about | ||
| * properties: | ||
| * receivingEmail: | ||
| * type: string | ||
| * format: email | ||
| * description: Recipient email address | ||
| * replyTo: | ||
| * type: string | ||
| * format: email | ||
| * nullable: true | ||
| * description: Optional reply-to address | ||
| * about: | ||
| * type: string | ||
| * description: Email subject | ||
| * text: | ||
| * type: string | ||
| * nullable: true | ||
| * description: Plain text email body | ||
| * responses: | ||
| * 200: | ||
| * description: Email sent successfully | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * type: string | ||
| * example: Successfully sent the email! | ||
| * 400: | ||
| * description: Invalid request format | ||
| */ | ||
| contactRouter.post("/", async (req, res, next) => { | ||
| const contactSchema = z.object({ | ||
| receivingEmail: z.string().email(), | ||
| replyTo: z.string().email(), | ||
| about: z.string().min(1), | ||
| text: z.string().optional(), | ||
| }); | ||
|
|
||
| const result = contactSchema.safeParse(req.body); | ||
|
|
||
| if (!result.success) { | ||
| return next(clientError(400, "Invalid request format", result.error)); | ||
| } | ||
|
|
||
| const { receivingEmail, replyTo, about, text } = result.data; | ||
|
|
||
| const validEmailAddresses = [ | ||
| "hovedstyret@vektorprogrammet.no", | ||
| "styret.ntnu@vektorprogrammet.no", | ||
| "uib@vektorprogrammet.no", | ||
| "nmbu@vektorprogrammet.no", | ||
| ]; | ||
|
|
||
| if (!validEmailAddresses.includes(receivingEmail)) { | ||
| return next(clientError(418, "Nice try!")); | ||
| } | ||
|
|
||
| try { | ||
| await sendEmail(receivingEmail, replyTo, about, text); | ||
|
|
||
| res.json("Successfully sent the email!"); | ||
| } catch (error) { | ||
| console.error("CONTACT EMAIL ERROR:", error); | ||
| next(error); | ||
| } | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import nodemailer from "nodemailer"; | ||
|
|
||
| const transporter = nodemailer.createTransport({ | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| secure: true, | ||
| // biome-ignore lint/style/useNamingConvention: Nodemailer requires the `requireTLS` option name. | ||
| requireTLS: true, | ||
| service: "gmail", | ||
| auth: { | ||
| type: "OAuth2", | ||
| user: process.env.GOOGLE_FROM_EMAIL, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vurder å bruke allerede eksisterende environment variabel parsere her |
||
| clientId: process.env.GOOGLE_CLIENT_ID, | ||
| clientSecret: process.env.GOOGLE_CLIENT_SECRET, | ||
| refreshToken: process.env.GOOGLE_REFRESH_TOKEN, | ||
| }, | ||
| }); | ||
|
|
||
| export async function sendEmail( | ||
| to: string, | ||
| replyTo: string, | ||
| subject: string, | ||
| text?: string, | ||
| html?: string, | ||
| ) { | ||
| await transporter.sendMail({ | ||
| from: `"Vektorprogrammet" <${process.env.GOOGLE_FROM_EMAIL}>`, | ||
| to, | ||
| replyTo, | ||
| subject, | ||
| text, | ||
| html, | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kan ikke ha magic konstanter, vurder å legg det til i en global variabel, konfig fil eller databasen