Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

hotfix: magiclink dynamic email redirection url (#67)

Balu Babu22aa8ee334ee
diff --git a/.env.example b/.env.example
index aa98c1c03..902414178 100644
--- a/.env.example
+++ b/.env.example
@@ -47,6 +47,7 @@ RATE_LIMIT_MAX=100 # Max requests per IP
# Base URLs
VITE_BASE_URL=http://localhost:3000
VITE_SHORTCODE_BASE_URL=http://localhost:3000
+VITE_ADMIN_URL=http://localhost:3100
# Backend URLs
VITE_BACKEND_GQL_URL=http://localhost:3170/graphql
diff --git a/packages/hoppscotch-backend/src/auth/auth.controller.ts b/packages/hoppscotch-backend/src/auth/auth.controller.ts
index 0309eabd2..dba497161 100644
--- a/packages/hoppscotch-backend/src/auth/auth.controller.ts
+++ b/packages/hoppscotch-backend/src/auth/auth.controller.ts
@@ -3,6 +3,7 @@ import {
Controller,
Get,
Post,
+ Query,
Req,
Request,
Res,
@@ -34,9 +35,13 @@ export class AuthController {
** Route to initiate magic-link auth for a users email
*/
@Post('signin')
- async signInMagicLink(@Body() authData: SignInMagicDto) {
+ async signInMagicLink(
+ @Body() authData: SignInMagicDto,
+ @Query('origin') origin: string,
+ ) {
const deviceIdToken = await this.authService.signInMagicLink(
authData.email,
+ origin,
);
if (E.isLeft(deviceIdToken)) throwHTTPErr(deviceIdToken.left);
return deviceIdToken.right;
diff --git a/packages/hoppscotch-backend/src/auth/auth.service.spec.ts b/packages/hoppscotch-backend/src/auth/auth.service.spec.ts
index 97b1121ae..f7f3b7d49 100644
--- a/packages/hoppscotch-backend/src/auth/auth.service.spec.ts
+++ b/packages/hoppscotch-backend/src/auth/auth.service.spec.ts
@@ -77,7 +77,7 @@ const encodedRefreshToken =
describe('signInMagicLink', () => {
test('Should throw error if email is not in valid format', async () => {
- const result = await authService.signInMagicLink('bbbgmail.com');
+ const result = await authService.signInMagicLink('bbbgmail.com', 'admin');
expect(result).toEqualLeft({
message: INVALID_EMAIL,
statusCode: HttpStatus.BAD_REQUEST,
@@ -94,6 +94,7 @@ describe('signInMagicLink', () => {
const result = await authService.signInMagicLink(
+ 'admin',
);
expect(result).toEqualRight({
deviceIdentifier: passwordlessData.deviceIdentifier,
@@ -108,6 +109,7 @@ describe('signInMagicLink', () => {
const result = await authService.signInMagicLink(
+ 'admin',
);
expect(result).toEqualRight({
deviceIdentifier: passwordlessData.deviceIdentifier,
diff --git a/packages/hoppscotch-backend/src/auth/auth.service.ts b/packages/hoppscotch-backend/src/auth/auth.service.ts
index e6a4d9549..f9ffbe324 100644
--- a/packages/hoppscotch-backend/src/auth/auth.service.ts
+++ b/packages/hoppscotch-backend/src/auth/auth.service.ts
@@ -27,6 +27,7 @@ import { JwtService } from '@nestjs/jwt';
import { AuthError } from 'src/types/AuthError';
import { AuthUser, IsAdmin } from 'src/types/AuthUser';
import { VerificationToken } from '@prisma/client';
+import { Origin } from './helper';
@Injectable()
export class AuthService {
@@ -195,7 +196,7 @@ export class AuthService {
* @param email User's email
* @returns Either containing DeviceIdentifierToken
*/
- async signInMagicLink(email: string) {
+ async signInMagicLink(email: string, origin: string) {
if (!validateEmail(email))
return E.left({
message: INVALID_EMAIL,
@@ -213,11 +214,25 @@ export class AuthService {
const generatedTokens = await this.generateMagicLinkTokens(user);
+ // check to see if origin is valid
+ let url: string;
+ switch (origin) {
+ case Origin.ADMIN:
+ url = process.env.VITE_ADMIN_URL;
+ break;
+ case Origin.APP:
+ url = process.env.VITE_BASE_URL;
+ break;
+ default:
+ // if origin is invalid by default set URL to Hoppscotch-App
+ url = process.env.VITE_BASE_URL;
+ }
+
await this.mailerService.sendAuthEmail(email, {
template: 'code-your-own',
variables: {
inviteeEmail: email,
- magicLink: `${process.env.VITE_BASE_URL}/magic-link?token=${generatedTokens.token}`,
+ magicLink: `${url}/magic-link?token=${generatedTokens.token}`,
},
});
diff --git a/packages/hoppscotch-backend/src/auth/helper.ts b/packages/hoppscotch-backend/src/auth/helper.ts
index 9a6ca0113..a33ed3882 100644
--- a/packages/hoppscotch-backend/src/auth/helper.ts
+++ b/packages/hoppscotch-backend/src/auth/helper.ts
@@ -11,6 +11,11 @@ enum AuthTokenType {
REFRESH_TOKEN = 'refresh_token',
}
+export enum Origin {
+ ADMIN = 'admin',
+ APP = 'app',
+}
+
/**
* This function allows throw to be used as an expression
* @param errMessage Message present in the error message