Skip to content
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

Nodemailer Email Implementation #984

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import { ChangeEvent, PureComponent, useEffect, useState } from 'react';

import actionTypesStore from '$actions/ActionTypesStore';
import { loadSchedule, saveSchedule } from '$actions/AppStoreActions';
import trpc from '$lib/api/trpc'; // email sending
import AppStore from '$stores/AppStore';
import { useThemeStore } from '$stores/SettingsStore';


interface LoadSaveButtonBaseProps {
action: typeof saveSchedule;
actionName: 'Save' | 'Load';
Expand Down Expand Up @@ -49,6 +51,10 @@ function SaveLoadIcon(props: SaveLoadIconProps) {
);
}

const sendEmail = async () => {
await trpc.users.sendUserEmail.query();
};

class LoadSaveButtonBase extends PureComponent<LoadSaveButtonBaseProps, LoadSaveButtonBaseState> {
state: LoadSaveButtonBaseState = {
isOpen: false,
Expand Down Expand Up @@ -153,6 +159,9 @@ class LoadSaveButtonBase extends PureComponent<LoadSaveButtonBaseProps, LoadSave
<Button onClick={() => this.handleClose(false)} color={this.props.colorType}>
{this.props.actionName}
</Button>
<Button onClick={sendEmail} color={this.props.colorType}>
Send Email
</Button>
</DialogActions>
</Dialog>
</>
Expand Down
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"express": "^4.18.2",
"mongodb": "^5.0.1",
"mongoose": "^7.1.0j",
"nodemailer": "^6.9.13",
"superjson": "^1.12.3",
"websoc-api": "^3.0.0"
},
Expand Down
4 changes: 4 additions & 0 deletions apps/backend/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const Environment = type({
AWS_REGION: 'string',
MAPBOX_ACCESS_TOKEN: 'string',
'PR_NUM?': 'number',
GOOGLE_EMAIL: 'string',
GOOGLE_ID: 'string',
GOOGLE_SECRET: 'string',
GOOGLE_REFRESH_TOKEN: 'string',
});

const env = Environment.assert({ ...process.env });
Expand Down
27 changes: 25 additions & 2 deletions apps/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ const corsOptions: CorsOptions = {
const MAPBOX_API_URL = 'https://api.mapbox.com';

const PORT = 3000;
import { createTransport } from 'nodemailer';
const email = createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: env.GOOGLE_EMAIL,
clientId: env.GOOGLE_ID,
clientSecret: env.GOOGLE_SECRET,
refreshToken: env.GOOGLE_REFRESH_TOKEN,
},
})

export async function start(corsEnabled = false) {
await connectToMongoDB();
// await connectToMongoDB();

const app = express();
app.use(cors(corsEnabled ? corsOptions : undefined));
app.use(cors({ origin: true }));
app.use(express.json());

app.use('/mapbox/directions/*', async (req, res) => {
Expand All @@ -42,6 +55,16 @@ export async function start(corsEnabled = false) {
// res.type('image/png')
// res.send(result)
});

app.get('/email', async () => {
console.log("SENDING EMAIL")
const res = await email.sendMail({
to: '[email protected]', // presumably input.userEmail or something
subject: 'Reset Password',
text: `Reset your password`,
})
console.log('rez', res);
})

app.use(
'/trpc',
Expand Down
25 changes: 25 additions & 0 deletions apps/backend/src/routers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@ import { UserSchema } from '@packages/antalmanac-types';
import { router, procedure } from '../trpc';
import { ddbClient, VISIBILITY } from '../db/ddb';
import { TRPCError } from '@trpc/server';
import env from '../env';
import { createTransport } from 'nodemailer';

const userInputSchema = type([{ userId: 'string' }, '|', { googleId: 'string' }]);

const email = createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: env.GOOGLE_EMAIL,
clientId: env.GOOGLE_ID,
clientSecret: env.GOOGLE_SECRET,
refreshToken: env.GOOGLE_REFRESH_TOKEN,
},
})

const viewInputSchema = type({
/**
* ID of the user who's requesting to view another user's schedule.
Expand Down Expand Up @@ -88,6 +103,16 @@ const usersRouter = router({
viewUserData: procedure.input(viewInputSchema.assert).query(async ({ input }) => {
return await ddbClient.viewUserData(input.requesterId, input.requesteeId);
}),

sendUserEmail: procedure.query(async () => {
console.log("SENDING EMAIL")
const res = await email.sendMail({
to: '[email protected]', // presumably input.userEmail or something
subject: 'Reset Password',
text: `Reset your password`,
})
console.log('rez', res);
}),
});

export default usersRouter;
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading