Skip to content

Commit e85b5f0

Browse files
メールを送信する #24
1 parent 9ea6e3e commit e85b5f0

File tree

5 files changed

+83
-5
lines changed

5 files changed

+83
-5
lines changed

.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
55
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
66

7-
DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"
7+
DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"
8+
9+
GMAIL_USER="iam.o.sin@gmail.com"
10+
GMAIL_PASS="google_app_password"

package-lock.json

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@sveltejs/adapter-auto": "next",
1818
"@sveltejs/kit": "next",
1919
"@types/bcrypt": "^5.0.0",
20+
"@types/nodemailer": "^6.4.6",
2021
"@typescript-eslint/eslint-plugin": "^5.27.0",
2122
"@typescript-eslint/parser": "^5.27.0",
2223
"eslint": "^8.16.0",
@@ -34,6 +35,7 @@
3435
"type": "module",
3536
"dependencies": {
3637
"@prisma/client": "^4.5.0",
37-
"bcrypt": "^5.1.0"
38+
"bcrypt": "^5.1.0",
39+
"nodemailer": "^6.8.0"
3840
}
3941
}

src/lib/nodemailer_manager.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as nodemailer from 'nodemailer';
2+
3+
export class NodemailerManager {
4+
private readonly _transporter: nodemailer.Transporter
5+
6+
constructor() {
7+
this._transporter = nodemailer.createTransport({
8+
service: 'gmail',
9+
port: 465,
10+
secure: true,
11+
auth: {
12+
user: process.env.GMAIL_USER,
13+
pass: process.env.GMAIL_PASS,
14+
},
15+
})
16+
}
17+
18+
async sendMail(to: string, subject: string, text: string) {
19+
const from = process.env.GMAIL_USER
20+
21+
return await this._transporter.sendMail({ from, to, subject, text })
22+
}
23+
}

src/routes/login/+page.server.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
import { CookiesManager } from '$lib/cookies_manager'
22
import { db } from '$lib/database'
3+
import { NodemailerManager } from '$lib/nodemailer_manager'
34
import type { Actions, PageServerLoad } from '.svelte-kit/types/src/routes/register/$types'
45
import { invalid, redirect } from '@sveltejs/kit'
56
import bcrypt from 'bcrypt'
67

78
export const load: PageServerLoad = async ({ locals, url }) => {
89
if (locals.user) {
9-
const redirect_url = url.searchParams.get('redirect') || '/'
10+
const redirect_url = url.searchParams.get('redirect') || ' /'
1011
throw redirect(302, redirect_url)
1112
}
1213
}
1314

15+
async function sendMail(username: string) {
16+
const nodemailerManager = new NodemailerManager()
17+
18+
try {
19+
await nodemailerManager.sendMail(
20+
'info@sinproject.net',
21+
'SvelteKit Authentication',
22+
`${username} logged in `
23+
)
24+
} catch (error) {
25+
console.error(error)
26+
}
27+
}
28+
1429
export const actions: Actions = {
1530
default: async ({ cookies, request }) => {
1631
const data = await request.formData()
@@ -27,13 +42,15 @@ export const actions: Actions = {
2742

2843
if (!password_valid) return invalid(400, { credentials: true })
2944

45+
sendMail(username)
46+
3047
const auth_token = await db.authToken.upsert({
3148
where: { user_id: user.id },
3249
update: { token: crypto.randomUUID() },
3350
create: { user_id: user.id, token: crypto.randomUUID() },
3451
})
3552

36-
new CookiesManager(cookies).setSessionId(auth_token.token);
53+
new CookiesManager(cookies).setSessionId(auth_token.token)
3754

3855
return { success: true }
3956
},

0 commit comments

Comments
 (0)