Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix notification channels confirmation link

Blue Mousec5f4c86b227e
diff --git a/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts b/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts
index 9d3a7d6f5..883ece5fc 100644
--- a/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts
+++ b/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts
@@ -207,47 +207,39 @@ export class NotificationChannelController {
@Param('token') token: string,
@Res() res: Response,
) {
- const channel = await this.channelService.findByVerificationToken(token)
- if (channel) {
- await this.channelService.markVerified(channel.id)
- }
+ const verified = await this.verifyChannel(token)
const clientUrl =
this.configService.get<string>('CLIENT_URL') || 'https://swetrix.com'
res.redirect(
HttpStatus.FOUND,
- `${clientUrl}/notification-channels?verified=${channel ? '1' : '0'}`,
+ `${clientUrl}/dashboard?notificationChannelVerified=${verified ? '1' : '0'}`,
)
}
+ @Public()
+ @Post('/verify/:token')
+ async completeVerificationFromClient(@Param('token') token: string) {
+ return { success: await this.verifyChannel(token) }
+ }
+
@Public()
@Get('/unsubscribe/:token')
async unsubscribeEmail(@Param('token') token: string, @Res() res: Response) {
- let channelId: string | null
- try {
- channelId = this.emailDispatcher.verifyUnsubscribeToken(token)
- } catch (reason) {
- if (reason instanceof Error) {
- throw new HttpException(
- {
- statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
- message: reason.message,
- },
- HttpStatus.INTERNAL_SERVER_ERROR,
- )
- }
- throw reason
- }
- if (channelId) {
- await this.channelService.setEmailUnsubscribed(channelId, true)
- }
+ const unsubscribed = await this.unsubscribeChannel(token)
const clientUrl =
this.configService.get<string>('CLIENT_URL') || 'https://swetrix.com'
res.redirect(
HttpStatus.FOUND,
- `${clientUrl}/notification-channels/unsubscribed?ok=${channelId ? '1' : '0'}`,
+ `${clientUrl}/dashboard?notificationChannelUnsubscribed=${unsubscribed ? '1' : '0'}`,
)
}
+ @Public()
+ @Post('/unsubscribe/:token')
+ async unsubscribeEmailFromClient(@Param('token') token: string) {
+ return { success: await this.unsubscribeChannel(token) }
+ }
+
@ApiBearerAuth()
@Post('/webpush/subscribe')
@Auth()
@@ -297,6 +289,37 @@ export class NotificationChannelController {
)
}
+ private async verifyChannel(token: string) {
+ const channel = await this.channelService.findByVerificationToken(token)
+ if (!channel) return false
+
+ await this.channelService.markVerified(channel.id)
+ return true
+ }
+
+ private async unsubscribeChannel(token: string) {
+ let channelId: string | null
+ try {
+ channelId = this.emailDispatcher.verifyUnsubscribeToken(token)
+ } catch (reason) {
+ if (reason instanceof Error) {
+ throw new HttpException(
+ {
+ statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
+ message: reason.message,
+ },
+ HttpStatus.INTERNAL_SERVER_ERROR,
+ )
+ }
+ throw reason
+ }
+
+ if (!channelId) return false
+
+ await this.channelService.setEmailUnsubscribed(channelId, true)
+ return true
+ }
+
private serialise(channel: NotificationChannel) {
return {
id: channel.id,
diff --git a/web/app/routes/api.notification-channel.unsubscribe.$token.tsx b/web/app/routes/api.notification-channel.unsubscribe.$token.tsx
new file mode 100644
index 000000000..a50221a44
--- /dev/null
+++ b/web/app/routes/api.notification-channel.unsubscribe.$token.tsx
@@ -0,0 +1,51 @@
+import type { LoaderFunctionArgs, MetaFunction } from 'react-router'
+import { redirect } from 'react-router'
+import type { SitemapFunction } from 'remix-sitemap'
+
+import { serverFetch } from '~/api/api.server'
+import StatusPage from '~/ui/StatusPage'
+import routes from '~/utils/routes'
+import { getTitle } from '~/utils/seo'
+
+export const sitemap: SitemapFunction = () => ({
+ exclude: true,
+})
+
+export const meta: MetaFunction = () => [
+ ...getTitle('Unsubscribe from notifications'),
+ { name: 'robots', content: 'noindex' },
+]
+
+export async function loader({
+ request,
+ params,
+}: LoaderFunctionArgs): Promise<Response | null> {
+ const { token } = params
+
+ if (!token) return null
+
+ const result = await serverFetch<{ success: boolean }>(
+ request,
+ `notification-channel/unsubscribe/${encodeURIComponent(token)}`,
+ { method: 'POST', skipAuth: true },
+ )
+
+ if (!result.error && result.data?.success === true) {
+ return redirect('/notification-channel-status?result=unsubscribed', 302)
+ }
+
+ return null
+}
+
+export default function NotificationChannelUnsubscribeRoute() {
+ return (
+ <StatusPage
+ type='error'
+ title='This unsubscribe link is invalid'
+ actions={[
+ { label: 'Sign In', to: routes.signin, primary: true },
+ { label: 'Support', to: routes.contact },
+ ]}
+ />
+ )
+}
diff --git a/web/app/routes/api.notification-channel.verify.$token.tsx b/web/app/routes/api.notification-channel.verify.$token.tsx
new file mode 100644
index 000000000..2551af644
--- /dev/null
+++ b/web/app/routes/api.notification-channel.verify.$token.tsx
@@ -0,0 +1,51 @@
+import type { LoaderFunctionArgs, MetaFunction } from 'react-router'
+import { redirect } from 'react-router'
+import type { SitemapFunction } from 'remix-sitemap'
+
+import { serverFetch } from '~/api/api.server'
+import StatusPage from '~/ui/StatusPage'
+import routes from '~/utils/routes'
+import { getTitle } from '~/utils/seo'
+
+export const sitemap: SitemapFunction = () => ({
+ exclude: true,
+})
+
+export const meta: MetaFunction = () => [
+ ...getTitle('Confirm notification channel'),
+ { name: 'robots', content: 'noindex' },
+]
+
+export async function loader({
+ request,
+ params,
+}: LoaderFunctionArgs): Promise<Response | null> {
+ const { token } = params
+
+ if (!token) return null
+
+ const result = await serverFetch<{ success: boolean }>(
+ request,
+ `notification-channel/verify/${encodeURIComponent(token)}`,
+ { method: 'POST', skipAuth: true },
+ )
+
+ if (!result.error && result.data?.success === true) {
+ return redirect('/notification-channel-status?result=confirmed', 302)
+ }
+
+ return null
+}
+
+export default function NotificationChannelVerificationRoute() {
+ return (
+ <StatusPage
+ type='error'
+ title='This confirmation link is invalid or has expired'
+ actions={[
+ { label: 'Dashboard', to: routes.dashboard, primary: true },
+ { label: 'Support', to: routes.contact },
+ ]}
+ />
+ )
+}
diff --git a/web/app/routes/notification-channel-status.tsx b/web/app/routes/notification-channel-status.tsx
new file mode 100644
index 000000000..ee7fb147b
--- /dev/null
+++ b/web/app/routes/notification-channel-status.tsx
@@ -0,0 +1,71 @@
+import type { LoaderFunctionArgs, MetaFunction } from 'react-router'
+import { useLoaderData } from 'react-router'
+import type { SitemapFunction } from 'remix-sitemap'
+
+import StatusPage from '~/ui/StatusPage'
+import routes from '~/utils/routes'
+import { getTitle } from '~/utils/seo'
+
+export const sitemap: SitemapFunction = () => ({
+ exclude: true,
+})
+
+export const meta: MetaFunction = () => [
+ ...getTitle('Notification channel'),
+ { name: 'robots', content: 'noindex' },
+]
+
+interface NotificationChannelStatusLoaderData {
+ result: 'confirmed' | 'unsubscribed' | null
+}
+
+export function loader({
+ request,
+}: LoaderFunctionArgs): NotificationChannelStatusLoaderData {
+ const result = new URL(request.url).searchParams.get('result')
+
+ return {
+ result: result === 'confirmed' || result === 'unsubscribed' ? result : null,
+ }
+}
+
+export default function NotificationChannelStatusRoute() {
+ const { result } = useLoaderData<NotificationChannelStatusLoaderData>()
+
+ if (result === 'confirmed') {
+ return (
+ <StatusPage
+ type='success'
+ title='Notification channel confirmed'
+ actions={[
+ {
+ label: 'Continue to dashboard',
+ to: routes.dashboard,
+ primary: true,
+ },
+ ]}
+ />
+ )
+ }
+
+ if (result === 'unsubscribed') {
+ return (
+ <StatusPage
+ type='success'
+ title='Successfully unsubscribed from this notification channel'
+ actions={[{ label: 'Sign In', to: routes.signin, primary: true }]}
+ />
+ )
+ }
+
+ return (
+ <StatusPage
+ type='error'
+ title='Invalid notification channel status'
+ actions={[
+ { label: 'Dashboard', to: routes.dashboard, primary: true },
+ { label: 'Support', to: routes.contact },
+ ]}
+ />
+ )
+}