From caf698ed2fcc231eb2d83c2b903a0e7f7645ea5d Mon Sep 17 00:00:00 2001 From: Rijk van Zanten Date: Fri, 25 Mar 2022 13:21:59 -0400 Subject: [PATCH] Use Url util to construct urls everywhere (#12394) Fixes #12345 --- api/src/services/activity.ts | 5 ++++- api/src/services/shares.ts | 3 ++- api/src/services/users.ts | 4 +++- api/src/utils/url.ts | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/api/src/services/activity.ts b/api/src/services/activity.ts index 9c70a609cc..5048db5396 100644 --- a/api/src/services/activity.ts +++ b/api/src/services/activity.ts @@ -12,6 +12,7 @@ import { userName } from '../utils/user-name'; import { uniq } from 'lodash'; import env from '../env'; import validateUUID from 'uuid-validate'; +import { Url } from '../utils/url'; export class ActivityService extends ItemsService { notificationsService: NotificationsService; @@ -83,7 +84,9 @@ ${userName(sender)} has mentioned you in a comment: ${comment} -Click here to view. +Click here to view. `; await this.notificationsService.createOne({ diff --git a/api/src/services/shares.ts b/api/src/services/shares.ts index 1a47686532..6da28a1a73 100644 --- a/api/src/services/shares.ts +++ b/api/src/services/shares.ts @@ -19,6 +19,7 @@ import { UsersService } from './users'; import { MailService } from './mail'; import { userName } from '../utils/user-name'; import { md } from '../utils/md'; +import { Url } from '../utils/url'; export class SharesService extends ItemsService { authorizationService: AuthorizationService; @@ -137,7 +138,7 @@ Hello! ${userName(userInfo)} has invited you to view an item in ${share.collection}. -[Open](${env.PUBLIC_URL}/admin/shared/${payload.share}) +[Open](${new Url(env.PUBLIC_URL).addPath('admin', 'shared', payload.share).toString()}) `; for (const email of payload.emails) { diff --git a/api/src/services/users.ts b/api/src/services/users.ts index d05554bc19..12671a59ab 100644 --- a/api/src/services/users.ts +++ b/api/src/services/users.ts @@ -324,7 +324,9 @@ export class UsersService extends ItemsService { const payload = { email, scope: 'password-reset', hash: getSimpleHash('' + user.password) }; const token = jwt.sign(payload, env.SECRET as string, { expiresIn: '1d', issuer: 'directus' }); - const acceptURL = url ? `${url}?token=${token}` : `${env.PUBLIC_URL}/admin/reset-password?token=${token}`; + const acceptURL = url + ? new Url(url).setQuery('token', token).toString() + : new Url(env.PUBLIC_URL).addPath('admin', 'reset-password').setQuery('token', token); const subjectLine = subject ? subject : 'Password Reset Request'; await mailService.send({ diff --git a/api/src/utils/url.ts b/api/src/utils/url.ts index 42c68d267d..0727fc43c4 100644 --- a/api/src/utils/url.ts +++ b/api/src/utils/url.ts @@ -38,7 +38,7 @@ export class Url { return this.protocol === null && this.host === null; } - public addPath(...paths: string[]): Url { + public addPath(...paths: (string | number)[]): Url { const pathToAdd = paths.flatMap((p) => p.split('/')).filter((p) => p !== ''); for (const pathSegment of pathToAdd) {