aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-08-09 21:36:03 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-08-09 21:37:03 +0200
commit464e470441287572cfda8d95484f781236b9db35 (patch)
tree87177837cb6ee6ee000f0d39fa5ba7ee6bb2943e /lib
downloadmy_apps-464e470441287572cfda8d95484f781236b9db35.tar.gz
my_apps-464e470441287572cfda8d95484f781236b9db35.tar.bz2
my_apps-464e470441287572cfda8d95484f781236b9db35.zip
init commit
Diffstat (limited to 'lib')
-rw-r--r--lib/dbConnect.js45
-rw-r--r--lib/fetchJson.js23
-rw-r--r--lib/sendMail.js26
-rw-r--r--lib/useUser.js33
-rw-r--r--lib/withSession.js14
5 files changed, 141 insertions, 0 deletions
diff --git a/lib/dbConnect.js b/lib/dbConnect.js
new file mode 100644
index 0000000..92f34bd
--- /dev/null
+++ b/lib/dbConnect.js
@@ -0,0 +1,45 @@
+import mongoose from 'mongoose'
+
+const MONGODB_URI = process.env.MYAPPS_MONGODB_URI
+
+if (!MONGODB_URI) {
+ throw new Error(
+ 'Please define the MONGODB_URI environment variable inside .env.local'
+ )
+}
+
+/**
+ * Global is used here to maintain a cached connection across hot reloads
+ * in development. This prevents connections growing exponentially
+ * during API Route usage.
+ */
+let cached = global.mongoose
+
+if (!cached) {
+ cached = global.mongoose = {conn: null, promise: null}
+}
+
+async function dbConnect() {
+ if (cached.conn) {
+ return cached.conn
+ }
+
+ if (!cached.promise) {
+ const opts = {
+ useNewUrlParser: true,
+ useUnifiedTopology: true,
+ bufferCommands: false,
+ bufferMaxEntries: 0,
+ useFindAndModify: false,
+ useCreateIndex: true,
+ }
+
+ cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
+ return mongoose
+ })
+ }
+ cached.conn = await cached.promise
+ return cached.conn
+}
+
+export default dbConnect
diff --git a/lib/fetchJson.js b/lib/fetchJson.js
new file mode 100644
index 0000000..5db80b5
--- /dev/null
+++ b/lib/fetchJson.js
@@ -0,0 +1,23 @@
+export default async function fetcher(...args) {
+ try {
+ const response = await fetch(...args)
+
+ // if the server replies, there's always some data in json
+ // if there's a network error, it will throw at the previous line
+ const data = await response.json()
+
+ if (response.ok) {
+ return data
+ }
+
+ const error = new Error(response.statusText)
+ error.response = response
+ error.data = data
+ throw error
+ } catch (error) {
+ if (!error.data) {
+ error.data = {message: error.message}
+ }
+ throw error
+ }
+}
diff --git a/lib/sendMail.js b/lib/sendMail.js
new file mode 100644
index 0000000..f50eaf1
--- /dev/null
+++ b/lib/sendMail.js
@@ -0,0 +1,26 @@
+import nodemailer from 'nodemailer'
+
+const sendMail = (to, subject, text, html) => {
+
+ const transporter = nodemailer.createTransport({
+ port: 465,
+ host: process.env.MYAPPS_MAIL_SMTP_SERVER,
+ auth: {
+ user: process.env.MYAPPS_MAIL_ADDRESS,
+ pass: process.env.MYAPPS_MAIL_PASSWORD,
+ },
+ secure: true,
+ })
+
+ const mailData = {
+ from: `"Notes App" <${process.env.MYAPPS_MAIL_ADDRESS}>`,
+ to,
+ subject,
+ text,
+ html,
+ }
+
+ return transporter.sendMail(mailData)
+}
+
+export default sendMail
diff --git a/lib/useUser.js b/lib/useUser.js
new file mode 100644
index 0000000..16833aa
--- /dev/null
+++ b/lib/useUser.js
@@ -0,0 +1,33 @@
+import { useEffect } from 'react'
+import Router from 'next/router'
+import useSWR from 'swr'
+
+export default function useUser({
+ redirectToLogin = false,
+ redirectToVerify = false,
+ redirectToApps = false,
+} = {}) {
+ const { data: user, mutate: mutateUser } = useSWR('/api/user')
+
+ useEffect(() => {
+ if (!user || (!redirectToLogin && !redirectToVerify && !redirectToApps)) return
+
+ if (redirectToLogin && !user?.isLoggedIn) {
+ Router.push('/login')
+ }
+
+ if (redirectToVerify && user?.isLoggedIn && !user?.isVerified) {
+ Router.push('/verify')
+ }
+
+ if (
+ (redirectToApps && user?.isLoggedIn && user?.isVerified)
+ ) {
+ Router.push('/')
+ }
+
+ return
+ }, [user, redirectToLogin, redirectToVerify, redirectToApps])
+
+ return {user, mutateUser}
+}
diff --git a/lib/withSession.js b/lib/withSession.js
new file mode 100644
index 0000000..0361db3
--- /dev/null
+++ b/lib/withSession.js
@@ -0,0 +1,14 @@
+// this file is a wrapper with defaults to be used in both API routes and `getServerSideProps` functions
+import {withIronSession} from 'next-iron-session'
+
+export default function withSession(handler) {
+ return withIronSession(handler, {
+ password: process.env.MYAPPS_SECRET_COOKIE_PASSWORD,
+ cookieName: 'myapps_session',
+ cookieOptions: {
+ // the next line allows to use the session in non-https environments like
+ // Next.js dev mode (http://localhost:3000)
+ secure: process.env.NODE_ENV === 'production' ? true : false,
+ },
+ })
+}