2 Commits

Author SHA1 Message Date
5fba4c6643 Linty fresh and pretty
Some checks failed
continuous-integration/drone/push Build is failing
2023-05-02 20:54:14 -04:00
f105ce537f Prettier! 2023-05-02 20:36:40 -04:00
5 changed files with 25 additions and 15 deletions

View File

@@ -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`;

View File

@@ -6,6 +6,7 @@ import { STRATEGIES } from '../constants/strategies';
import { TokenProps, sign, verify as verifyJwt } from '../utils/jwt'; import { TokenProps, sign, verify as verifyJwt } from '../utils/jwt';
import { encrypt, verify as verifyPassword } from '../utils/password'; import { encrypt, verify as verifyPassword } from '../utils/password';
import { generateResetToken } from '../utils/tokens'; import { generateResetToken } from '../utils/tokens';
import { getPasswordResetLink } from '../utils/links';
export type Auth = { export type Auth = {
is2FA?: boolean; is2FA?: boolean;
@@ -65,12 +66,9 @@ AuthSchema.methods = {
}, },
async getResetLink(route) { async getResetLink(route) {
const resetToken = await this.getResetToken(); const token = await this.getResetToken();
if (resetToken) { if (token) {
let resetRoute = route; const resetUrl = getPasswordResetLink(token);
resetRoute = resetRoute.replace(':user_id', this._id);
resetRoute = resetRoute.replace(':reset_token?', resetToken);
const resetUrl = `${process.env.URL}${resetRoute}`;
console.log('[sendPasswordReset] resetUrl:', resetUrl); console.log('[sendPasswordReset] resetUrl:', resetUrl);
return resetUrl; return resetUrl;
} }
@@ -109,13 +107,13 @@ AuthSchema.methods = {
}; };
AuthSchema.statics = { AuthSchema.statics = {
// authenticateAndGetRecordLocator: async function (username, password) { authenticateAndGetRecordLocator: async function (username, password) {
// const auth = await this.findByUserName(username); const auth = await this.findByUsername(username);
// if (auth && auth.authenticate(password)) { if (auth && auth.authenticate(password)) {
// return auth?.record; return auth.record;
// } }
// return false; return false;
// }, },
async findByUsername(username) { async findByUsername(username) {
return this.findOne({ username }); return this.findOne({ username });

View File

@@ -2,10 +2,11 @@ import dotenv from 'dotenv';
import app from './app'; import app from './app';
import { connection } from './database/database.connection'; import { connection } from './database/database.connection';
import { PORT as DEFAULT_PORT } from './constants/defaults';
dotenv.config(); dotenv.config();
const PORT: number = Number(process.env.PORT) || 9000; const PORT: number = Number(process.env.PORT) || DEFAULT_PORT;
connection.then( connection.then(
() => app.listen(PORT), () => app.listen(PORT),

View File

@@ -5,7 +5,7 @@ import { sign } from './jwt';
export const getAuthenticationBundle = async (username: string, password: string) => { export const getAuthenticationBundle = async (username: string, password: string) => {
const auth = await Auth.findByUsername(username).catch(); const auth = await Auth.findByUsername(username).catch();
const isAuthenticated = !!auth && (auth as AuthModel).authenticate(password); const isAuthenticated = !!auth && (auth as AuthModel).authenticate(password);
const record = isAuthenticated ? (auth as AuthPrivate).record as string : null; const record = isAuthenticated ? ((auth as AuthPrivate).record as string) : null;
const token = sign(record || undefined); const token = sign(record || undefined);
return { return {
record, record,

7
lib/utils/links.ts Normal file
View File

@@ -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}`;
};