From 5fba4c6643495c8fc387bf591493c5adb5c15a78 Mon Sep 17 00:00:00 2001 From: mifi Date: Tue, 2 May 2023 20:54:14 -0400 Subject: [PATCH] Linty fresh and pretty --- lib/constants/defaults.ts | 4 ++++ lib/schema/auth.ts | 24 +++++++++++------------- lib/server.ts | 3 ++- lib/utils/links.ts | 7 +++++++ 4 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 lib/constants/defaults.ts create mode 100644 lib/utils/links.ts diff --git a/lib/constants/defaults.ts b/lib/constants/defaults.ts new file mode 100644 index 0000000..c7a3691 --- /dev/null +++ b/lib/constants/defaults.ts @@ -0,0 +1,4 @@ +export const PORT = 9000; +export const API_PATH = '/api'; +export const AUTH_ROUTE = '/auth'; +export const RESET_ROUTE = `${AUTH_ROUTE}/reset`; diff --git a/lib/schema/auth.ts b/lib/schema/auth.ts index 0bd47a0..96ea824 100644 --- a/lib/schema/auth.ts +++ b/lib/schema/auth.ts @@ -6,6 +6,7 @@ import { STRATEGIES } from '../constants/strategies'; import { TokenProps, sign, verify as verifyJwt } from '../utils/jwt'; import { encrypt, verify as verifyPassword } from '../utils/password'; import { generateResetToken } from '../utils/tokens'; +import { getPasswordResetLink } from '../utils/links'; export type Auth = { is2FA?: boolean; @@ -65,12 +66,9 @@ AuthSchema.methods = { }, async getResetLink(route) { - const resetToken = await this.getResetToken(); - if (resetToken) { - let resetRoute = route; - resetRoute = resetRoute.replace(':user_id', this._id); - resetRoute = resetRoute.replace(':reset_token?', resetToken); - const resetUrl = `${process.env.URL}${resetRoute}`; + const token = await this.getResetToken(); + if (token) { + const resetUrl = getPasswordResetLink(token); console.log('[sendPasswordReset] resetUrl:', resetUrl); return resetUrl; } @@ -109,13 +107,13 @@ AuthSchema.methods = { }; AuthSchema.statics = { - // authenticateAndGetRecordLocator: async function (username, password) { - // const auth = await this.findByUserName(username); - // if (auth && auth.authenticate(password)) { - // return auth?.record; - // } - // return false; - // }, + authenticateAndGetRecordLocator: async function (username, password) { + const auth = await this.findByUsername(username); + if (auth && auth.authenticate(password)) { + return auth.record; + } + return false; + }, async findByUsername(username) { return this.findOne({ username }); diff --git a/lib/server.ts b/lib/server.ts index d0bb5f3..2c57423 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -2,10 +2,11 @@ import dotenv from 'dotenv'; import app from './app'; import { connection } from './database/database.connection'; +import { PORT as DEFAULT_PORT } from './constants/defaults'; dotenv.config(); -const PORT: number = Number(process.env.PORT) || 9000; +const PORT: number = Number(process.env.PORT) || DEFAULT_PORT; connection.then( () => app.listen(PORT), diff --git a/lib/utils/links.ts b/lib/utils/links.ts new file mode 100644 index 0000000..e1875e5 --- /dev/null +++ b/lib/utils/links.ts @@ -0,0 +1,7 @@ +import { API_PATH, PORT, RESET_ROUTE } from '../constants/defaults'; + +export const getPasswordResetLink = (token: string) => { + const hostname = process.env.HOST_NAME || `localhost:${process.env.PORT || PORT}`; + const path = `${process.env.API_PATH || API_PATH}${process.env.RESET_ROUTE || RESET_ROUTE}`; + return `https://${hostname}${path}?t=${token}`; +};