-
-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
501 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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,97 @@ | ||
import nodeMailer from "nodemailer"; | ||
import { prisma } from "../../../prisma"; | ||
|
||
export async function forgotPassword( | ||
email: string, | ||
code: string, | ||
link: string | ||
) { | ||
try { | ||
let mail; | ||
|
||
const emails = await prisma.email.findMany(); | ||
|
||
const resetlink = `${link}/auth/reset-password?code=${code}`; | ||
|
||
if (emails.length > 0) { | ||
if (process.env.ENVIRONMENT === "development") { | ||
let testAccount = await nodeMailer.createTestAccount(); | ||
mail = nodeMailer.createTransport({ | ||
port: 1025, | ||
secure: false, // true for 465, false for other ports | ||
auth: { | ||
user: testAccount.user, // generated ethereal user | ||
pass: testAccount.pass, // generated ethereal password | ||
}, | ||
}); | ||
} else { | ||
const email = emails[0]; | ||
mail = nodeMailer.createTransport({ | ||
// @ts-ignore | ||
host: email.host, | ||
port: email.port, | ||
secure: email.secure, // true for 465, false for other ports | ||
auth: { | ||
user: email.user, // generated ethereal user | ||
pass: email.pass, // generated ethereal password | ||
}, | ||
}); | ||
} | ||
|
||
console.log("Sending email to: ", email); | ||
|
||
let info = await mail.sendMail({ | ||
from: '"No reply 👻" [email protected]', // sender address | ||
to: email, // list of receivers | ||
subject: `Password Reset Request`, // Subject line | ||
text: `Password Reset Code: ${code}, follow this link to reset your password ${link}`, // plain text body | ||
html: ` | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
<html lang="en"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html charset=UTF-8" /> | ||
</head> | ||
<div id="" style="display:none;overflow:hidden;line-height:1px;opacity:0;max-height:0;max-width:0">Ticket Created<div></div> | ||
</div> | ||
<body style="background-color:#ffffff;margin:0 auto;font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif"> | ||
<table align="center" role="presentation" cellSpacing="0" cellPadding="0" border="0" width="100%" style="max-width:600px;margin:0 auto"> | ||
<tr style="width:100%"> | ||
<td> | ||
<table style="margin-top:8px" align="center" border="0" cellPadding="0" cellSpacing="0" role="presentation" width="100%"> | ||
</table> | ||
<h1 style="color:#1d1c1d;font-size:16px;font-weight:700;margin:10px 0;padding:0;line-height:42px">Password Reset</h1> | ||
<p style="font-size:20px;line-height:28px;margin:4px 0"> | ||
<p>Password code: ${code}</p> | ||
<a href=${resetlink}>Reset Here</a> | ||
<p style="font-size:14px;margin:16px 0;color:#000"> | ||
Kind regards, | ||
<table align="center" border="0" cellPadding="0" cellSpacing="0" role="presentation" width="100%"> | ||
<tbody> | ||
<tr> | ||
<td> | ||
<a target="_blank" style="color:#b7b7b7;text-decoration:underline" href="https://slackhq.com" rel="noopener noreferrer">Our blog</a> | <a target="_blank" style="color:#b7b7b7;text-decoration:underline" href="https://slack.com/legal" rel="noopener noreferrer">Documentation</a> | <a target="_blank" style="color:#b7b7b7;text-decoration:underline" href="https://slack.com/help" rel="noopener noreferrer">Discord</a> </a> | ||
<p style="font-size:12px;line-height:15px;margin:16px 0;color:#b7b7b7;text-align:left">This was an automated message sent by peppermint.sh -> An open source helpdesk solution</p> | ||
<p style="font-size:12px;line-height:15px;margin:16px 0;color:#b7b7b7;text-align:left;margin-bottom:50px">©2022 Peppermint Ticket Management, a Peppermint Labs product.<br />All rights reserved.</p> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</td> | ||
</tr> | ||
</table> | ||
</body> | ||
</html> | ||
`, | ||
}); | ||
|
||
console.log("Message sent: %s", info.messageId); | ||
|
||
// Preview only available when sending through an Ethereal account | ||
console.log("Preview URL: %s", nodeMailer.getTestMessageUrl(info)); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
apps/api/src/prisma/migrations/20231203224035_code/migration.sql
This file contains 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,11 @@ | ||
-- CreateTable | ||
CREATE TABLE "PasswordResetToken" ( | ||
"id" TEXT NOT NULL, | ||
"code" TEXT NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "PasswordResetToken_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "PasswordResetToken_code_key" ON "PasswordResetToken"("code"); |
This file contains 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 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,113 @@ | ||
import { notifications } from "@mantine/notifications"; | ||
import Link from "next/link"; | ||
import { useRouter } from "next/router"; | ||
import { useState } from "react"; | ||
|
||
export default function Login({}) { | ||
const router = useRouter(); | ||
|
||
const [email, setEmail] = useState(""); | ||
const [view, setView] = useState("request"); | ||
|
||
async function postData() { | ||
await fetch( | ||
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/auth/password-reset`, | ||
{ | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ email, link: window.location.origin }), | ||
} | ||
) | ||
.then((res) => res.json()) | ||
.then((res) => { | ||
if (res.success) { | ||
notifications.show({ | ||
title: "Success", | ||
message: "A password reset email is on its way.", | ||
color: "green", | ||
autoClose: 5000, | ||
}); | ||
router.push("/auth/login"); | ||
} else { | ||
notifications.show({ | ||
title: "Error", | ||
message: | ||
"There was an error with this request, please try again. If this issue persists, please contact support via the discord.", | ||
color: "red", | ||
autoClose: 5000, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
return ( | ||
<div className="min-h-screen bg-gray-50 flex flex-col justify-center py-12 sm:px-6 lg:px-8"> | ||
<div className="sm:mx-auto sm:w-full sm:max-w-md"> | ||
<a target="_blank" href="https://peppermint.sh/"> | ||
<img | ||
className="mx-auto h-36 w-auto" | ||
src="/login.svg" | ||
alt="peppermint.sh logo" | ||
/> | ||
</a> | ||
<h2 className="mt-6 text-center text-3xl font-extrabold text-gray-900"> | ||
Request Password Reset | ||
</h2> | ||
</div> | ||
|
||
<div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md"> | ||
<div className="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10"> | ||
<div className="space-y-4"> | ||
<div> | ||
<label | ||
htmlFor="email" | ||
className="block text-sm font-medium text-gray-700" | ||
> | ||
Email address | ||
</label> | ||
<div className="mt-1"> | ||
<input | ||
id="email" | ||
name="email" | ||
type="email" | ||
autoComplete="email" | ||
required | ||
onChange={(e) => setEmail(e.target.value)} | ||
className="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-green-500 focus:border-green-500 sm:text-sm" | ||
/> | ||
</div> | ||
</div> | ||
|
||
<div className="flex items-center justify-between"> | ||
<div className="text-sm"> | ||
<Link | ||
href="/auth/login" | ||
className="font-medium text-indigo-600 hover:text-indigo-500" | ||
> | ||
Remember your password? | ||
</Link> | ||
</div> | ||
</div> | ||
|
||
<div> | ||
<button | ||
type="submit" | ||
onClick={postData} | ||
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500" | ||
> | ||
Submit Request | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div className="mt-8 text-center flex flex-col space-y-2"> | ||
<span className="font-bold">Built with 💚 by Peppermint Labs</span> | ||
<a href="https://docs.peppermint.sh/" target="_blank"> | ||
Documentation | ||
</a> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains 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
Oops, something went wrong.