Compare commits
36 Commits
refactor/p
...
feat/share
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
131b014e64 | ||
|
|
877f210ad7 | ||
|
|
23bb9bc3d1 | ||
|
|
4dcb0afb18 | ||
|
|
2e445224da | ||
|
|
032164f263 | ||
|
|
61efaaa248 | ||
|
|
c8c3314430 | ||
|
|
3eef6c9b04 | ||
|
|
2064af92fc | ||
|
|
1b00ff6c9e | ||
|
|
8fe1483de5 | ||
|
|
892be11445 | ||
|
|
00ad3307d4 | ||
|
|
2e396b3402 | ||
|
|
0be35633cf | ||
|
|
91c07e15b3 | ||
|
|
130aa8503c | ||
|
|
1703f2ee8a | ||
|
|
f780ca7a92 | ||
|
|
3747da9939 | ||
|
|
8007191d7a | ||
|
|
a1b952329b | ||
|
|
1124ba1ba3 | ||
|
|
6023ee27cf | ||
|
|
d56224b17b | ||
|
|
b7462ab014 | ||
|
|
b74c1abf6f | ||
|
|
7876c42312 | ||
|
|
f149582a62 | ||
|
|
12f2840013 | ||
|
|
28a88759c6 | ||
|
|
d1c9c3583f | ||
|
|
2462492c86 | ||
|
|
7a9f0c8756 | ||
|
|
46caf9b198 |
@@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- A unique constraint covering the columns `[id]` on the table `Shortcode` will be added. If there are existing duplicate values, this will fail.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Shortcode" ADD COLUMN "embedProperties" JSONB,
|
||||||
|
ADD COLUMN "updatedOn" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "Shortcode_id_key" ON "Shortcode"("id");
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Shortcode" ADD CONSTRAINT "Shortcode_creatorUid_fkey" FOREIGN KEY ("creatorUid") REFERENCES "User"("uid") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||||
@@ -68,11 +68,13 @@ model TeamRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model Shortcode {
|
model Shortcode {
|
||||||
id String @id
|
id String @id @unique
|
||||||
request Json
|
request Json
|
||||||
creatorUid String?
|
embedProperties Json?
|
||||||
createdOn DateTime @default(now())
|
creatorUid String?
|
||||||
|
User User? @relation(fields: [creatorUid], references: [uid])
|
||||||
|
createdOn DateTime @default(now())
|
||||||
|
updatedOn DateTime @updatedAt @default(now())
|
||||||
@@unique(fields: [id, creatorUid], name: "creator_uid_shortcode_unique")
|
@@unique(fields: [id, creatorUid], name: "creator_uid_shortcode_unique")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,6 +104,7 @@ model User {
|
|||||||
currentGQLSession Json?
|
currentGQLSession Json?
|
||||||
createdOn DateTime @default(now()) @db.Timestamp(3)
|
createdOn DateTime @default(now()) @db.Timestamp(3)
|
||||||
invitedUsers InvitedUsers[]
|
invitedUsers InvitedUsers[]
|
||||||
|
shortcodes Shortcode[]
|
||||||
}
|
}
|
||||||
|
|
||||||
model Account {
|
model Account {
|
||||||
|
|||||||
@@ -318,18 +318,6 @@ export const TEAM_INVITATION_NOT_FOUND =
|
|||||||
*/
|
*/
|
||||||
export const SHORTCODE_NOT_FOUND = 'shortcode/not_found' as const;
|
export const SHORTCODE_NOT_FOUND = 'shortcode/not_found' as const;
|
||||||
|
|
||||||
/**
|
|
||||||
* Invalid ShortCode format
|
|
||||||
* (ShortcodeService)
|
|
||||||
*/
|
|
||||||
export const SHORTCODE_INVALID_JSON = 'shortcode/invalid_json' as const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ShortCode already exists in DB
|
|
||||||
* (ShortcodeService)
|
|
||||||
*/
|
|
||||||
export const SHORTCODE_ALREADY_EXISTS = 'shortcode/already_exists' as const;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invalid or non-existent TEAM ENVIRONMENT ID
|
* Invalid or non-existent TEAM ENVIRONMENT ID
|
||||||
* (TeamEnvironmentsService)
|
* (TeamEnvironmentsService)
|
||||||
@@ -621,3 +609,24 @@ export const MAILER_SMTP_URL_UNDEFINED = 'mailer/smtp_url_undefined' as const;
|
|||||||
*/
|
*/
|
||||||
export const MAILER_FROM_ADDRESS_UNDEFINED =
|
export const MAILER_FROM_ADDRESS_UNDEFINED =
|
||||||
'mailer/from_address_undefined' as const;
|
'mailer/from_address_undefined' as const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SharedRequest invalid request JSON format
|
||||||
|
* (ShortcodeService)
|
||||||
|
*/
|
||||||
|
export const SHORTCODE_INVALID_REQUEST_JSON =
|
||||||
|
'shortcode/request_invalid_format' as const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SharedRequest invalid properties JSON format
|
||||||
|
* (ShortcodeService)
|
||||||
|
*/
|
||||||
|
export const SHORTCODE_INVALID_PROPERTIES_JSON =
|
||||||
|
'shortcode/properties_invalid_format' as const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SharedRequest invalid properties not found
|
||||||
|
* (ShortcodeService)
|
||||||
|
*/
|
||||||
|
export const SHORTCODE_PROPERTIES_NOT_FOUND =
|
||||||
|
'shortcode/properties_not_found' as const;
|
||||||
|
|||||||
@@ -69,5 +69,7 @@ export type TopicDef = {
|
|||||||
[topic: `team_req/${string}/req_deleted`]: string;
|
[topic: `team_req/${string}/req_deleted`]: string;
|
||||||
[topic: `team/${string}/invite_added`]: TeamInvitation;
|
[topic: `team/${string}/invite_added`]: TeamInvitation;
|
||||||
[topic: `team/${string}/invite_removed`]: string;
|
[topic: `team/${string}/invite_removed`]: string;
|
||||||
[topic: `shortcode/${string}/${'created' | 'revoked'}`]: Shortcode;
|
[
|
||||||
|
topic: `shortcode/${string}/${'created' | 'revoked' | 'updated'}`
|
||||||
|
]: Shortcode;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Field, ID, ObjectType } from '@nestjs/graphql';
|
|||||||
@ObjectType()
|
@ObjectType()
|
||||||
export class Shortcode {
|
export class Shortcode {
|
||||||
@Field(() => ID, {
|
@Field(() => ID, {
|
||||||
description: 'The shortcode. 12 digit alphanumeric.',
|
description: 'The 12 digit alphanumeric code',
|
||||||
})
|
})
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
@@ -12,6 +12,12 @@ export class Shortcode {
|
|||||||
})
|
})
|
||||||
request: string;
|
request: string;
|
||||||
|
|
||||||
|
@Field({
|
||||||
|
description: 'JSON string representing the properties for an embed',
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
properties: string;
|
||||||
|
|
||||||
@Field({
|
@Field({
|
||||||
description: 'Timestamp of when the Shortcode was created',
|
description: 'Timestamp of when the Shortcode was created',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { JwtModule } from '@nestjs/jwt';
|
|
||||||
import { PrismaModule } from 'src/prisma/prisma.module';
|
import { PrismaModule } from 'src/prisma/prisma.module';
|
||||||
import { PubSubModule } from 'src/pubsub/pubsub.module';
|
import { PubSubModule } from 'src/pubsub/pubsub.module';
|
||||||
import { UserModule } from 'src/user/user.module';
|
import { UserModule } from 'src/user/user.module';
|
||||||
@@ -7,14 +6,7 @@ import { ShortcodeResolver } from './shortcode.resolver';
|
|||||||
import { ShortcodeService } from './shortcode.service';
|
import { ShortcodeService } from './shortcode.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [PrismaModule, UserModule, PubSubModule],
|
||||||
PrismaModule,
|
|
||||||
UserModule,
|
|
||||||
PubSubModule,
|
|
||||||
JwtModule.register({
|
|
||||||
secret: process.env.JWT_SECRET,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
providers: [ShortcodeService, ShortcodeResolver],
|
providers: [ShortcodeService, ShortcodeResolver],
|
||||||
exports: [ShortcodeService],
|
exports: [ShortcodeService],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
Args,
|
Args,
|
||||||
Context,
|
|
||||||
ID,
|
ID,
|
||||||
Mutation,
|
Mutation,
|
||||||
Query,
|
Query,
|
||||||
@@ -11,14 +10,12 @@ import * as E from 'fp-ts/Either';
|
|||||||
import { UseGuards } from '@nestjs/common';
|
import { UseGuards } from '@nestjs/common';
|
||||||
import { Shortcode } from './shortcode.model';
|
import { Shortcode } from './shortcode.model';
|
||||||
import { ShortcodeService } from './shortcode.service';
|
import { ShortcodeService } from './shortcode.service';
|
||||||
import { UserService } from 'src/user/user.service';
|
|
||||||
import { throwErr } from 'src/utils';
|
import { throwErr } from 'src/utils';
|
||||||
import { GqlUser } from 'src/decorators/gql-user.decorator';
|
import { GqlUser } from 'src/decorators/gql-user.decorator';
|
||||||
import { GqlAuthGuard } from 'src/guards/gql-auth.guard';
|
import { GqlAuthGuard } from 'src/guards/gql-auth.guard';
|
||||||
import { User } from 'src/user/user.model';
|
import { User } from 'src/user/user.model';
|
||||||
import { PubSubService } from 'src/pubsub/pubsub.service';
|
import { PubSubService } from 'src/pubsub/pubsub.service';
|
||||||
import { AuthUser } from '../types/AuthUser';
|
import { AuthUser } from '../types/AuthUser';
|
||||||
import { JwtService } from '@nestjs/jwt';
|
|
||||||
import { PaginationArgs } from 'src/types/input-types.args';
|
import { PaginationArgs } from 'src/types/input-types.args';
|
||||||
import { GqlThrottlerGuard } from 'src/guards/gql-throttler.guard';
|
import { GqlThrottlerGuard } from 'src/guards/gql-throttler.guard';
|
||||||
import { SkipThrottle } from '@nestjs/throttler';
|
import { SkipThrottle } from '@nestjs/throttler';
|
||||||
@@ -28,9 +25,7 @@ import { SkipThrottle } from '@nestjs/throttler';
|
|||||||
export class ShortcodeResolver {
|
export class ShortcodeResolver {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly shortcodeService: ShortcodeService,
|
private readonly shortcodeService: ShortcodeService,
|
||||||
private readonly userService: UserService,
|
|
||||||
private readonly pubsub: PubSubService,
|
private readonly pubsub: PubSubService,
|
||||||
private jwtService: JwtService,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/* Queries */
|
/* Queries */
|
||||||
@@ -64,20 +59,53 @@ export class ShortcodeResolver {
|
|||||||
@Mutation(() => Shortcode, {
|
@Mutation(() => Shortcode, {
|
||||||
description: 'Create a shortcode for the given request.',
|
description: 'Create a shortcode for the given request.',
|
||||||
})
|
})
|
||||||
|
@UseGuards(GqlAuthGuard)
|
||||||
async createShortcode(
|
async createShortcode(
|
||||||
|
@GqlUser() user: AuthUser,
|
||||||
@Args({
|
@Args({
|
||||||
name: 'request',
|
name: 'request',
|
||||||
description: 'JSON string of the request object',
|
description: 'JSON string of the request object',
|
||||||
})
|
})
|
||||||
request: string,
|
request: string,
|
||||||
@Context() ctx: any,
|
@Args({
|
||||||
|
name: 'properties',
|
||||||
|
description: 'JSON string of the properties of the embed',
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
properties: string,
|
||||||
) {
|
) {
|
||||||
const decodedAccessToken = this.jwtService.verify(
|
|
||||||
ctx.req.cookies['access_token'],
|
|
||||||
);
|
|
||||||
const result = await this.shortcodeService.createShortcode(
|
const result = await this.shortcodeService.createShortcode(
|
||||||
request,
|
request,
|
||||||
decodedAccessToken?.sub,
|
properties,
|
||||||
|
user,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (E.isLeft(result)) throwErr(result.left);
|
||||||
|
return result.right;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mutation(() => Shortcode, {
|
||||||
|
description: 'Update a user generated Shortcode',
|
||||||
|
})
|
||||||
|
@UseGuards(GqlAuthGuard)
|
||||||
|
async updateEmbedProperties(
|
||||||
|
@GqlUser() user: AuthUser,
|
||||||
|
@Args({
|
||||||
|
name: 'code',
|
||||||
|
type: () => ID,
|
||||||
|
description: 'The Shortcode to update',
|
||||||
|
})
|
||||||
|
code: string,
|
||||||
|
@Args({
|
||||||
|
name: 'properties',
|
||||||
|
description: 'JSON string of the properties of the embed',
|
||||||
|
})
|
||||||
|
properties: string,
|
||||||
|
) {
|
||||||
|
const result = await this.shortcodeService.updateEmbedProperties(
|
||||||
|
code,
|
||||||
|
user.uid,
|
||||||
|
properties,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (E.isLeft(result)) throwErr(result.left);
|
if (E.isLeft(result)) throwErr(result.left);
|
||||||
@@ -114,6 +142,16 @@ export class ShortcodeResolver {
|
|||||||
return this.pubsub.asyncIterator(`shortcode/${user.uid}/created`);
|
return this.pubsub.asyncIterator(`shortcode/${user.uid}/created`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscription(() => Shortcode, {
|
||||||
|
description: 'Listen for Shortcode updates',
|
||||||
|
resolve: (value) => value,
|
||||||
|
})
|
||||||
|
@SkipThrottle()
|
||||||
|
@UseGuards(GqlAuthGuard)
|
||||||
|
myShortcodesUpdated(@GqlUser() user: AuthUser) {
|
||||||
|
return this.pubsub.asyncIterator(`shortcode/${user.uid}/updated`);
|
||||||
|
}
|
||||||
|
|
||||||
@Subscription(() => Shortcode, {
|
@Subscription(() => Shortcode, {
|
||||||
description: 'Listen for shortcode deletion',
|
description: 'Listen for shortcode deletion',
|
||||||
resolve: (value) => value,
|
resolve: (value) => value,
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
import { mockDeep, mockReset } from 'jest-mock-extended';
|
import { mockDeep, mockReset } from 'jest-mock-extended';
|
||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
import {
|
import {
|
||||||
SHORTCODE_ALREADY_EXISTS,
|
SHORTCODE_INVALID_PROPERTIES_JSON,
|
||||||
SHORTCODE_INVALID_JSON,
|
SHORTCODE_INVALID_REQUEST_JSON,
|
||||||
SHORTCODE_NOT_FOUND,
|
SHORTCODE_NOT_FOUND,
|
||||||
|
SHORTCODE_PROPERTIES_NOT_FOUND,
|
||||||
} from 'src/errors';
|
} from 'src/errors';
|
||||||
import { Shortcode } from './shortcode.model';
|
import { Shortcode } from './shortcode.model';
|
||||||
import { ShortcodeService } from './shortcode.service';
|
import { ShortcodeService } from './shortcode.service';
|
||||||
import { UserService } from 'src/user/user.service';
|
import { UserService } from 'src/user/user.service';
|
||||||
|
import { AuthUser } from 'src/types/AuthUser';
|
||||||
|
|
||||||
const mockPrisma = mockDeep<PrismaService>();
|
const mockPrisma = mockDeep<PrismaService>();
|
||||||
|
|
||||||
@@ -22,7 +24,7 @@ const mockFB = {
|
|||||||
doc: mockDocFunc,
|
doc: mockDocFunc,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const mockUserService = new UserService(mockFB as any, mockPubSub as any);
|
const mockUserService = new UserService(mockPrisma as any, mockPubSub as any);
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -38,18 +40,34 @@ beforeEach(() => {
|
|||||||
});
|
});
|
||||||
const createdOn = new Date();
|
const createdOn = new Date();
|
||||||
|
|
||||||
const shortCodeWithOutUser = {
|
const user: AuthUser = {
|
||||||
id: '123',
|
uid: '123344',
|
||||||
request: '{}',
|
email: 'dwight@dundermifflin.com',
|
||||||
|
displayName: 'Dwight Schrute',
|
||||||
|
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
|
||||||
|
isAdmin: false,
|
||||||
|
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
|
||||||
createdOn: createdOn,
|
createdOn: createdOn,
|
||||||
creatorUid: null,
|
currentGQLSession: {},
|
||||||
|
currentRESTSession: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
const shortCodeWithUser = {
|
const mockEmbed = {
|
||||||
id: '123',
|
id: '123',
|
||||||
request: '{}',
|
request: '{}',
|
||||||
|
embedProperties: '{}',
|
||||||
createdOn: createdOn,
|
createdOn: createdOn,
|
||||||
creatorUid: 'user_uid_1',
|
creatorUid: user.uid,
|
||||||
|
updatedOn: createdOn,
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockShortcode = {
|
||||||
|
id: '123',
|
||||||
|
request: '{}',
|
||||||
|
embedProperties: null,
|
||||||
|
createdOn: createdOn,
|
||||||
|
creatorUid: user.uid,
|
||||||
|
updatedOn: createdOn,
|
||||||
};
|
};
|
||||||
|
|
||||||
const shortcodes = [
|
const shortcodes = [
|
||||||
@@ -58,33 +76,38 @@ const shortcodes = [
|
|||||||
request: {
|
request: {
|
||||||
hello: 'there',
|
hello: 'there',
|
||||||
},
|
},
|
||||||
creatorUid: 'testuser',
|
embedProperties: {
|
||||||
|
foo: 'bar',
|
||||||
|
},
|
||||||
|
creatorUid: user.uid,
|
||||||
createdOn: new Date(),
|
createdOn: new Date(),
|
||||||
|
updatedOn: createdOn,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'blablabla1',
|
id: 'blablabla1',
|
||||||
request: {
|
request: {
|
||||||
hello: 'there',
|
hello: 'there',
|
||||||
},
|
},
|
||||||
creatorUid: 'testuser',
|
embedProperties: {
|
||||||
|
foo: 'bar',
|
||||||
|
},
|
||||||
|
creatorUid: user.uid,
|
||||||
createdOn: new Date(),
|
createdOn: new Date(),
|
||||||
|
updatedOn: createdOn,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
describe('ShortcodeService', () => {
|
describe('ShortcodeService', () => {
|
||||||
describe('getShortCode', () => {
|
describe('getShortCode', () => {
|
||||||
test('should return a valid shortcode with valid shortcode ID', async () => {
|
test('should return a valid Shortcode with valid Shortcode ID', async () => {
|
||||||
mockPrisma.shortcode.findFirstOrThrow.mockResolvedValueOnce(
|
mockPrisma.shortcode.findFirstOrThrow.mockResolvedValueOnce(mockEmbed);
|
||||||
shortCodeWithOutUser,
|
|
||||||
);
|
|
||||||
|
|
||||||
const result = await shortcodeService.getShortCode(
|
const result = await shortcodeService.getShortCode(mockEmbed.id);
|
||||||
shortCodeWithOutUser.id,
|
|
||||||
);
|
|
||||||
expect(result).toEqualRight(<Shortcode>{
|
expect(result).toEqualRight(<Shortcode>{
|
||||||
id: shortCodeWithOutUser.id,
|
id: mockEmbed.id,
|
||||||
createdOn: shortCodeWithOutUser.createdOn,
|
createdOn: mockEmbed.createdOn,
|
||||||
request: JSON.stringify(shortCodeWithOutUser.request),
|
request: JSON.stringify(mockEmbed.request),
|
||||||
|
properties: JSON.stringify(mockEmbed.embedProperties),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -99,10 +122,10 @@ describe('ShortcodeService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('fetchUserShortCodes', () => {
|
describe('fetchUserShortCodes', () => {
|
||||||
test('should return list of shortcodes with valid inputs and no cursor', async () => {
|
test('should return list of Shortcode with valid inputs and no cursor', async () => {
|
||||||
mockPrisma.shortcode.findMany.mockResolvedValueOnce(shortcodes);
|
mockPrisma.shortcode.findMany.mockResolvedValueOnce(shortcodes);
|
||||||
|
|
||||||
const result = await shortcodeService.fetchUserShortCodes('testuser', {
|
const result = await shortcodeService.fetchUserShortCodes(user.uid, {
|
||||||
cursor: null,
|
cursor: null,
|
||||||
take: 10,
|
take: 10,
|
||||||
});
|
});
|
||||||
@@ -110,20 +133,22 @@ describe('ShortcodeService', () => {
|
|||||||
{
|
{
|
||||||
id: shortcodes[0].id,
|
id: shortcodes[0].id,
|
||||||
request: JSON.stringify(shortcodes[0].request),
|
request: JSON.stringify(shortcodes[0].request),
|
||||||
|
properties: JSON.stringify(shortcodes[0].embedProperties),
|
||||||
createdOn: shortcodes[0].createdOn,
|
createdOn: shortcodes[0].createdOn,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: shortcodes[1].id,
|
id: shortcodes[1].id,
|
||||||
request: JSON.stringify(shortcodes[1].request),
|
request: JSON.stringify(shortcodes[1].request),
|
||||||
|
properties: JSON.stringify(shortcodes[1].embedProperties),
|
||||||
createdOn: shortcodes[1].createdOn,
|
createdOn: shortcodes[1].createdOn,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return list of shortcodes with valid inputs and cursor', async () => {
|
test('should return list of Shortcode with valid inputs and cursor', async () => {
|
||||||
mockPrisma.shortcode.findMany.mockResolvedValue([shortcodes[1]]);
|
mockPrisma.shortcode.findMany.mockResolvedValue([shortcodes[1]]);
|
||||||
|
|
||||||
const result = await shortcodeService.fetchUserShortCodes('testuser', {
|
const result = await shortcodeService.fetchUserShortCodes(user.uid, {
|
||||||
cursor: 'blablabla',
|
cursor: 'blablabla',
|
||||||
take: 10,
|
take: 10,
|
||||||
});
|
});
|
||||||
@@ -131,6 +156,7 @@ describe('ShortcodeService', () => {
|
|||||||
{
|
{
|
||||||
id: shortcodes[1].id,
|
id: shortcodes[1].id,
|
||||||
request: JSON.stringify(shortcodes[1].request),
|
request: JSON.stringify(shortcodes[1].request),
|
||||||
|
properties: JSON.stringify(shortcodes[1].embedProperties),
|
||||||
createdOn: shortcodes[1].createdOn,
|
createdOn: shortcodes[1].createdOn,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
@@ -139,7 +165,7 @@ describe('ShortcodeService', () => {
|
|||||||
test('should return an empty array for an invalid cursor', async () => {
|
test('should return an empty array for an invalid cursor', async () => {
|
||||||
mockPrisma.shortcode.findMany.mockResolvedValue([]);
|
mockPrisma.shortcode.findMany.mockResolvedValue([]);
|
||||||
|
|
||||||
const result = await shortcodeService.fetchUserShortCodes('testuser', {
|
const result = await shortcodeService.fetchUserShortCodes(user.uid, {
|
||||||
cursor: 'invalidcursor',
|
cursor: 'invalidcursor',
|
||||||
take: 10,
|
take: 10,
|
||||||
});
|
});
|
||||||
@@ -171,77 +197,111 @@ describe('ShortcodeService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('createShortcode', () => {
|
describe('createShortcode', () => {
|
||||||
test('should throw SHORTCODE_INVALID_JSON error if incoming request data is invalid', async () => {
|
test('should throw SHORTCODE_INVALID_REQUEST_JSON error if incoming request data is invalid', async () => {
|
||||||
const result = await shortcodeService.createShortcode(
|
const result = await shortcodeService.createShortcode(
|
||||||
'invalidRequest',
|
'invalidRequest',
|
||||||
'user_uid_1',
|
null,
|
||||||
|
user,
|
||||||
);
|
);
|
||||||
expect(result).toEqualLeft(SHORTCODE_INVALID_JSON);
|
expect(result).toEqualLeft(SHORTCODE_INVALID_REQUEST_JSON);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should successfully create a new shortcode with valid user uid', async () => {
|
test('should throw SHORTCODE_INVALID_PROPERTIES_JSON error if incoming properties data is invalid', async () => {
|
||||||
// generateUniqueShortCodeID --> getShortCode
|
const result = await shortcodeService.createShortcode(
|
||||||
|
'{}',
|
||||||
|
'invalid_data',
|
||||||
|
user,
|
||||||
|
);
|
||||||
|
expect(result).toEqualLeft(SHORTCODE_INVALID_PROPERTIES_JSON);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should successfully create a new Embed with valid user uid', async () => {
|
||||||
|
// generateUniqueShortCodeID --> getShortcode
|
||||||
mockPrisma.shortcode.findFirstOrThrow.mockRejectedValueOnce(
|
mockPrisma.shortcode.findFirstOrThrow.mockRejectedValueOnce(
|
||||||
'NotFoundError',
|
'NotFoundError',
|
||||||
);
|
);
|
||||||
mockPrisma.shortcode.create.mockResolvedValueOnce(shortCodeWithUser);
|
mockPrisma.shortcode.create.mockResolvedValueOnce(mockEmbed);
|
||||||
|
|
||||||
const result = await shortcodeService.createShortcode('{}', 'user_uid_1');
|
const result = await shortcodeService.createShortcode('{}', '{}', user);
|
||||||
expect(result).toEqualRight({
|
expect(result).toEqualRight(<Shortcode>{
|
||||||
id: shortCodeWithUser.id,
|
id: mockEmbed.id,
|
||||||
createdOn: shortCodeWithUser.createdOn,
|
createdOn: mockEmbed.createdOn,
|
||||||
request: JSON.stringify(shortCodeWithUser.request),
|
request: JSON.stringify(mockEmbed.request),
|
||||||
|
properties: JSON.stringify(mockEmbed.embedProperties),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should successfully create a new shortcode with null user uid', async () => {
|
test('should successfully create a new ShortCode with valid user uid', async () => {
|
||||||
// generateUniqueShortCodeID --> getShortCode
|
// generateUniqueShortCodeID --> getShortcode
|
||||||
mockPrisma.shortcode.findFirstOrThrow.mockRejectedValueOnce(
|
mockPrisma.shortcode.findFirstOrThrow.mockRejectedValueOnce(
|
||||||
'NotFoundError',
|
'NotFoundError',
|
||||||
);
|
);
|
||||||
mockPrisma.shortcode.create.mockResolvedValueOnce(shortCodeWithUser);
|
mockPrisma.shortcode.create.mockResolvedValueOnce(mockShortcode);
|
||||||
|
|
||||||
const result = await shortcodeService.createShortcode('{}', null);
|
const result = await shortcodeService.createShortcode('{}', null, user);
|
||||||
expect(result).toEqualRight({
|
expect(result).toEqualRight(<Shortcode>{
|
||||||
id: shortCodeWithUser.id,
|
id: mockShortcode.id,
|
||||||
createdOn: shortCodeWithUser.createdOn,
|
createdOn: mockShortcode.createdOn,
|
||||||
request: JSON.stringify(shortCodeWithOutUser.request),
|
request: JSON.stringify(mockShortcode.request),
|
||||||
|
properties: mockShortcode.embedProperties,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should send pubsub message to `shortcode/{uid}/created` on successful creation of shortcode', async () => {
|
test('should send pubsub message to `shortcode/{uid}/created` on successful creation of a Shortcode', async () => {
|
||||||
// generateUniqueShortCodeID --> getShortCode
|
// generateUniqueShortCodeID --> getShortcode
|
||||||
mockPrisma.shortcode.findFirstOrThrow.mockRejectedValueOnce(
|
mockPrisma.shortcode.findFirstOrThrow.mockRejectedValueOnce(
|
||||||
'NotFoundError',
|
'NotFoundError',
|
||||||
);
|
);
|
||||||
mockPrisma.shortcode.create.mockResolvedValueOnce(shortCodeWithUser);
|
mockPrisma.shortcode.create.mockResolvedValueOnce(mockShortcode);
|
||||||
|
|
||||||
|
const result = await shortcodeService.createShortcode('{}', null, user);
|
||||||
|
|
||||||
const result = await shortcodeService.createShortcode('{}', 'user_uid_1');
|
|
||||||
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
||||||
`shortcode/${shortCodeWithUser.creatorUid}/created`,
|
`shortcode/${mockShortcode.creatorUid}/created`,
|
||||||
{
|
<Shortcode>{
|
||||||
id: shortCodeWithUser.id,
|
id: mockShortcode.id,
|
||||||
createdOn: shortCodeWithUser.createdOn,
|
createdOn: mockShortcode.createdOn,
|
||||||
request: JSON.stringify(shortCodeWithUser.request),
|
request: JSON.stringify(mockShortcode.request),
|
||||||
|
properties: mockShortcode.embedProperties,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should send pubsub message to `shortcode/{uid}/created` on successful creation of an Embed', async () => {
|
||||||
|
// generateUniqueShortCodeID --> getShortcode
|
||||||
|
mockPrisma.shortcode.findFirstOrThrow.mockRejectedValueOnce(
|
||||||
|
'NotFoundError',
|
||||||
|
);
|
||||||
|
mockPrisma.shortcode.create.mockResolvedValueOnce(mockEmbed);
|
||||||
|
|
||||||
|
const result = await shortcodeService.createShortcode('{}', '{}', user);
|
||||||
|
|
||||||
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
||||||
|
`shortcode/${mockEmbed.creatorUid}/created`,
|
||||||
|
<Shortcode>{
|
||||||
|
id: mockEmbed.id,
|
||||||
|
createdOn: mockEmbed.createdOn,
|
||||||
|
request: JSON.stringify(mockEmbed.request),
|
||||||
|
properties: JSON.stringify(mockEmbed.embedProperties),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('revokeShortCode', () => {
|
describe('revokeShortCode', () => {
|
||||||
test('should return true on successful deletion of shortcode with valid inputs', async () => {
|
test('should return true on successful deletion of Shortcode with valid inputs', async () => {
|
||||||
mockPrisma.shortcode.delete.mockResolvedValueOnce(shortCodeWithUser);
|
mockPrisma.shortcode.delete.mockResolvedValueOnce(mockEmbed);
|
||||||
|
|
||||||
const result = await shortcodeService.revokeShortCode(
|
const result = await shortcodeService.revokeShortCode(
|
||||||
shortCodeWithUser.id,
|
mockEmbed.id,
|
||||||
shortCodeWithUser.creatorUid,
|
mockEmbed.creatorUid,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(mockPrisma.shortcode.delete).toHaveBeenCalledWith({
|
expect(mockPrisma.shortcode.delete).toHaveBeenCalledWith({
|
||||||
where: {
|
where: {
|
||||||
creator_uid_shortcode_unique: {
|
creator_uid_shortcode_unique: {
|
||||||
creatorUid: shortCodeWithUser.creatorUid,
|
creatorUid: mockEmbed.creatorUid,
|
||||||
id: shortCodeWithUser.id,
|
id: mockEmbed.id,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -249,52 +309,53 @@ describe('ShortcodeService', () => {
|
|||||||
expect(result).toEqualRight(true);
|
expect(result).toEqualRight(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return SHORTCODE_NOT_FOUND error when shortcode is invalid and user uid is valid', async () => {
|
test('should return SHORTCODE_NOT_FOUND error when Shortcode is invalid and user uid is valid', async () => {
|
||||||
mockPrisma.shortcode.delete.mockRejectedValue('RecordNotFound');
|
mockPrisma.shortcode.delete.mockRejectedValue('RecordNotFound');
|
||||||
expect(
|
expect(
|
||||||
shortcodeService.revokeShortCode('invalid', 'testuser'),
|
shortcodeService.revokeShortCode('invalid', 'testuser'),
|
||||||
).resolves.toEqualLeft(SHORTCODE_NOT_FOUND);
|
).resolves.toEqualLeft(SHORTCODE_NOT_FOUND);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return SHORTCODE_NOT_FOUND error when shortcode is valid and user uid is invalid', async () => {
|
test('should return SHORTCODE_NOT_FOUND error when Shortcode is valid and user uid is invalid', async () => {
|
||||||
mockPrisma.shortcode.delete.mockRejectedValue('RecordNotFound');
|
mockPrisma.shortcode.delete.mockRejectedValue('RecordNotFound');
|
||||||
expect(
|
expect(
|
||||||
shortcodeService.revokeShortCode('blablablabla', 'invalidUser'),
|
shortcodeService.revokeShortCode('blablablabla', 'invalidUser'),
|
||||||
).resolves.toEqualLeft(SHORTCODE_NOT_FOUND);
|
).resolves.toEqualLeft(SHORTCODE_NOT_FOUND);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return SHORTCODE_NOT_FOUND error when both shortcode and user uid are invalid', async () => {
|
test('should return SHORTCODE_NOT_FOUND error when both Shortcode and user uid are invalid', async () => {
|
||||||
mockPrisma.shortcode.delete.mockRejectedValue('RecordNotFound');
|
mockPrisma.shortcode.delete.mockRejectedValue('RecordNotFound');
|
||||||
expect(
|
expect(
|
||||||
shortcodeService.revokeShortCode('invalid', 'invalid'),
|
shortcodeService.revokeShortCode('invalid', 'invalid'),
|
||||||
).resolves.toEqualLeft(SHORTCODE_NOT_FOUND);
|
).resolves.toEqualLeft(SHORTCODE_NOT_FOUND);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should send pubsub message to `shortcode/{uid}/revoked` on successful deletion of shortcode', async () => {
|
test('should send pubsub message to `shortcode/{uid}/revoked` on successful deletion of Shortcode', async () => {
|
||||||
mockPrisma.shortcode.delete.mockResolvedValueOnce(shortCodeWithUser);
|
mockPrisma.shortcode.delete.mockResolvedValueOnce(mockEmbed);
|
||||||
|
|
||||||
const result = await shortcodeService.revokeShortCode(
|
const result = await shortcodeService.revokeShortCode(
|
||||||
shortCodeWithUser.id,
|
mockEmbed.id,
|
||||||
shortCodeWithUser.creatorUid,
|
mockEmbed.creatorUid,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
||||||
`shortcode/${shortCodeWithUser.creatorUid}/revoked`,
|
`shortcode/${mockEmbed.creatorUid}/revoked`,
|
||||||
{
|
{
|
||||||
id: shortCodeWithUser.id,
|
id: mockEmbed.id,
|
||||||
createdOn: shortCodeWithUser.createdOn,
|
createdOn: mockEmbed.createdOn,
|
||||||
request: JSON.stringify(shortCodeWithUser.request),
|
request: JSON.stringify(mockEmbed.request),
|
||||||
|
properties: JSON.stringify(mockEmbed.embedProperties),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('deleteUserShortCodes', () => {
|
describe('deleteUserShortCodes', () => {
|
||||||
test('should successfully delete all users shortcodes with valid user uid', async () => {
|
test('should successfully delete all users Shortcodes with valid user uid', async () => {
|
||||||
mockPrisma.shortcode.deleteMany.mockResolvedValueOnce({ count: 1 });
|
mockPrisma.shortcode.deleteMany.mockResolvedValueOnce({ count: 1 });
|
||||||
|
|
||||||
const result = await shortcodeService.deleteUserShortCodes(
|
const result = await shortcodeService.deleteUserShortCodes(
|
||||||
shortCodeWithUser.creatorUid,
|
mockEmbed.creatorUid,
|
||||||
);
|
);
|
||||||
expect(result).toEqual(1);
|
expect(result).toEqual(1);
|
||||||
});
|
});
|
||||||
@@ -303,9 +364,81 @@ describe('ShortcodeService', () => {
|
|||||||
mockPrisma.shortcode.deleteMany.mockResolvedValueOnce({ count: 0 });
|
mockPrisma.shortcode.deleteMany.mockResolvedValueOnce({ count: 0 });
|
||||||
|
|
||||||
const result = await shortcodeService.deleteUserShortCodes(
|
const result = await shortcodeService.deleteUserShortCodes(
|
||||||
shortCodeWithUser.creatorUid,
|
mockEmbed.creatorUid,
|
||||||
);
|
);
|
||||||
expect(result).toEqual(0);
|
expect(result).toEqual(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('updateShortcode', () => {
|
||||||
|
test('should return SHORTCODE_PROPERTIES_NOT_FOUND error when updatedProps in invalid', async () => {
|
||||||
|
const result = await shortcodeService.updateEmbedProperties(
|
||||||
|
mockEmbed.id,
|
||||||
|
user.uid,
|
||||||
|
'',
|
||||||
|
);
|
||||||
|
expect(result).toEqualLeft(SHORTCODE_PROPERTIES_NOT_FOUND);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return SHORTCODE_PROPERTIES_NOT_FOUND error when updatedProps in invalid JSON format', async () => {
|
||||||
|
const result = await shortcodeService.updateEmbedProperties(
|
||||||
|
mockEmbed.id,
|
||||||
|
user.uid,
|
||||||
|
'{kk',
|
||||||
|
);
|
||||||
|
expect(result).toEqualLeft(SHORTCODE_INVALID_PROPERTIES_JSON);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return SHORTCODE_NOT_FOUND error when Shortcode ID is invalid', async () => {
|
||||||
|
mockPrisma.shortcode.update.mockRejectedValue('RecordNotFound');
|
||||||
|
const result = await shortcodeService.updateEmbedProperties(
|
||||||
|
'invalidID',
|
||||||
|
user.uid,
|
||||||
|
'{}',
|
||||||
|
);
|
||||||
|
expect(result).toEqualLeft(SHORTCODE_NOT_FOUND);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should successfully update a Shortcodes with valid inputs', async () => {
|
||||||
|
mockPrisma.shortcode.update.mockResolvedValueOnce({
|
||||||
|
...mockEmbed,
|
||||||
|
embedProperties: '{"foo":"bar"}',
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await shortcodeService.updateEmbedProperties(
|
||||||
|
mockEmbed.id,
|
||||||
|
user.uid,
|
||||||
|
'{"foo":"bar"}',
|
||||||
|
);
|
||||||
|
expect(result).toEqualRight({
|
||||||
|
id: mockEmbed.id,
|
||||||
|
createdOn: mockEmbed.createdOn,
|
||||||
|
request: JSON.stringify(mockEmbed.request),
|
||||||
|
properties: JSON.stringify('{"foo":"bar"}'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should send pubsub message to `shortcode/{uid}/updated` on successful Update of Shortcode', async () => {
|
||||||
|
mockPrisma.shortcode.update.mockResolvedValueOnce({
|
||||||
|
...mockEmbed,
|
||||||
|
embedProperties: '{"foo":"bar"}',
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await shortcodeService.updateEmbedProperties(
|
||||||
|
mockEmbed.id,
|
||||||
|
user.uid,
|
||||||
|
'{"foo":"bar"}',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
||||||
|
`shortcode/${mockEmbed.creatorUid}/updated`,
|
||||||
|
{
|
||||||
|
id: mockEmbed.id,
|
||||||
|
createdOn: mockEmbed.createdOn,
|
||||||
|
request: JSON.stringify(mockEmbed.request),
|
||||||
|
properties: JSON.stringify('{"foo":"bar"}'),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||||
import * as T from 'fp-ts/Task';
|
import * as T from 'fp-ts/Task';
|
||||||
import * as O from 'fp-ts/Option';
|
|
||||||
import * as TO from 'fp-ts/TaskOption';
|
import * as TO from 'fp-ts/TaskOption';
|
||||||
import * as E from 'fp-ts/Either';
|
import * as E from 'fp-ts/Either';
|
||||||
import { PrismaService } from 'src/prisma/prisma.service';
|
import { PrismaService } from 'src/prisma/prisma.service';
|
||||||
import { SHORTCODE_INVALID_JSON, SHORTCODE_NOT_FOUND } from 'src/errors';
|
import {
|
||||||
|
SHORTCODE_INVALID_PROPERTIES_JSON,
|
||||||
|
SHORTCODE_INVALID_REQUEST_JSON,
|
||||||
|
SHORTCODE_NOT_FOUND,
|
||||||
|
SHORTCODE_PROPERTIES_NOT_FOUND,
|
||||||
|
} from 'src/errors';
|
||||||
import { UserDataHandler } from 'src/user/user.data.handler';
|
import { UserDataHandler } from 'src/user/user.data.handler';
|
||||||
import { Shortcode } from './shortcode.model';
|
import { Shortcode } from './shortcode.model';
|
||||||
import { Shortcode as DBShortCode } from '@prisma/client';
|
import { Shortcode as DBShortCode } from '@prisma/client';
|
||||||
@@ -46,10 +50,14 @@ export class ShortcodeService implements UserDataHandler, OnModuleInit {
|
|||||||
* @param shortcodeInfo Prisma Shortcode type
|
* @param shortcodeInfo Prisma Shortcode type
|
||||||
* @returns GQL Shortcode
|
* @returns GQL Shortcode
|
||||||
*/
|
*/
|
||||||
private returnShortCode(shortcodeInfo: DBShortCode): Shortcode {
|
private cast(shortcodeInfo: DBShortCode): Shortcode {
|
||||||
return <Shortcode>{
|
return <Shortcode>{
|
||||||
id: shortcodeInfo.id,
|
id: shortcodeInfo.id,
|
||||||
request: JSON.stringify(shortcodeInfo.request),
|
request: JSON.stringify(shortcodeInfo.request),
|
||||||
|
properties:
|
||||||
|
shortcodeInfo.embedProperties != null
|
||||||
|
? JSON.stringify(shortcodeInfo.embedProperties)
|
||||||
|
: null,
|
||||||
createdOn: shortcodeInfo.createdOn,
|
createdOn: shortcodeInfo.createdOn,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -94,7 +102,7 @@ export class ShortcodeService implements UserDataHandler, OnModuleInit {
|
|||||||
const shortcodeInfo = await this.prisma.shortcode.findFirstOrThrow({
|
const shortcodeInfo = await this.prisma.shortcode.findFirstOrThrow({
|
||||||
where: { id: shortcode },
|
where: { id: shortcode },
|
||||||
});
|
});
|
||||||
return E.right(this.returnShortCode(shortcodeInfo));
|
return E.right(this.cast(shortcodeInfo));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return E.left(SHORTCODE_NOT_FOUND);
|
return E.left(SHORTCODE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
@@ -104,14 +112,22 @@ export class ShortcodeService implements UserDataHandler, OnModuleInit {
|
|||||||
* Create a new ShortCode
|
* Create a new ShortCode
|
||||||
*
|
*
|
||||||
* @param request JSON string of request details
|
* @param request JSON string of request details
|
||||||
* @param userUID user UID, if present
|
* @param userInfo user UI
|
||||||
|
* @param properties JSON string of embed properties, if present
|
||||||
* @returns Either of ShortCode or error
|
* @returns Either of ShortCode or error
|
||||||
*/
|
*/
|
||||||
async createShortcode(request: string, userUID: string | null) {
|
async createShortcode(
|
||||||
const shortcodeData = stringToJson(request);
|
request: string,
|
||||||
if (E.isLeft(shortcodeData)) return E.left(SHORTCODE_INVALID_JSON);
|
properties: string | null = null,
|
||||||
|
userInfo: AuthUser,
|
||||||
|
) {
|
||||||
|
const requestData = stringToJson(request);
|
||||||
|
if (E.isLeft(requestData) || !requestData.right)
|
||||||
|
return E.left(SHORTCODE_INVALID_REQUEST_JSON);
|
||||||
|
|
||||||
const user = await this.userService.findUserById(userUID);
|
const parsedProperties = stringToJson(properties);
|
||||||
|
if (E.isLeft(parsedProperties))
|
||||||
|
return E.left(SHORTCODE_INVALID_PROPERTIES_JSON);
|
||||||
|
|
||||||
const generatedShortCode = await this.generateUniqueShortCodeID();
|
const generatedShortCode = await this.generateUniqueShortCodeID();
|
||||||
if (E.isLeft(generatedShortCode)) return E.left(generatedShortCode.left);
|
if (E.isLeft(generatedShortCode)) return E.left(generatedShortCode.left);
|
||||||
@@ -119,8 +135,9 @@ export class ShortcodeService implements UserDataHandler, OnModuleInit {
|
|||||||
const createdShortCode = await this.prisma.shortcode.create({
|
const createdShortCode = await this.prisma.shortcode.create({
|
||||||
data: {
|
data: {
|
||||||
id: generatedShortCode.right,
|
id: generatedShortCode.right,
|
||||||
request: shortcodeData.right,
|
request: requestData.right,
|
||||||
creatorUid: O.isNone(user) ? null : user.value.uid,
|
embedProperties: parsedProperties.right ?? undefined,
|
||||||
|
creatorUid: userInfo.uid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -128,11 +145,11 @@ export class ShortcodeService implements UserDataHandler, OnModuleInit {
|
|||||||
if (createdShortCode.creatorUid) {
|
if (createdShortCode.creatorUid) {
|
||||||
this.pubsub.publish(
|
this.pubsub.publish(
|
||||||
`shortcode/${createdShortCode.creatorUid}/created`,
|
`shortcode/${createdShortCode.creatorUid}/created`,
|
||||||
this.returnShortCode(createdShortCode),
|
this.cast(createdShortCode),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return E.right(this.returnShortCode(createdShortCode));
|
return E.right(this.cast(createdShortCode));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -156,7 +173,7 @@ export class ShortcodeService implements UserDataHandler, OnModuleInit {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const fetchedShortCodes: Shortcode[] = shortCodes.map((code) =>
|
const fetchedShortCodes: Shortcode[] = shortCodes.map((code) =>
|
||||||
this.returnShortCode(code),
|
this.cast(code),
|
||||||
);
|
);
|
||||||
|
|
||||||
return fetchedShortCodes;
|
return fetchedShortCodes;
|
||||||
@@ -182,7 +199,7 @@ export class ShortcodeService implements UserDataHandler, OnModuleInit {
|
|||||||
|
|
||||||
this.pubsub.publish(
|
this.pubsub.publish(
|
||||||
`shortcode/${deletedShortCodes.creatorUid}/revoked`,
|
`shortcode/${deletedShortCodes.creatorUid}/revoked`,
|
||||||
this.returnShortCode(deletedShortCodes),
|
this.cast(deletedShortCodes),
|
||||||
);
|
);
|
||||||
|
|
||||||
return E.right(true);
|
return E.right(true);
|
||||||
@@ -205,4 +222,45 @@ export class ShortcodeService implements UserDataHandler, OnModuleInit {
|
|||||||
|
|
||||||
return deletedShortCodes.count;
|
return deletedShortCodes.count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a created Shortcode
|
||||||
|
* @param shortcodeID Shortcode ID
|
||||||
|
* @param uid User Uid
|
||||||
|
* @returns Updated Shortcode
|
||||||
|
*/
|
||||||
|
async updateEmbedProperties(
|
||||||
|
shortcodeID: string,
|
||||||
|
uid: string,
|
||||||
|
updatedProps: string,
|
||||||
|
) {
|
||||||
|
if (!updatedProps) return E.left(SHORTCODE_PROPERTIES_NOT_FOUND);
|
||||||
|
|
||||||
|
const parsedProperties = stringToJson(updatedProps);
|
||||||
|
if (E.isLeft(parsedProperties) || !parsedProperties.right)
|
||||||
|
return E.left(SHORTCODE_INVALID_PROPERTIES_JSON);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const updatedShortcode = await this.prisma.shortcode.update({
|
||||||
|
where: {
|
||||||
|
creator_uid_shortcode_unique: {
|
||||||
|
creatorUid: uid,
|
||||||
|
id: shortcodeID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
embedProperties: parsedProperties.right,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
this.pubsub.publish(
|
||||||
|
`shortcode/${updatedShortcode.creatorUid}/updated`,
|
||||||
|
this.cast(updatedShortcode),
|
||||||
|
);
|
||||||
|
|
||||||
|
return E.right(this.cast(updatedShortcode));
|
||||||
|
} catch (error) {
|
||||||
|
return E.left(SHORTCODE_NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hoppscotch/cli",
|
"name": "@hoppscotch/cli",
|
||||||
"version": "0.3.3",
|
"version": "0.4.0",
|
||||||
"description": "A CLI to run Hoppscotch test scripts in CI environments.",
|
"description": "A CLI to run Hoppscotch test scripts in CI environments.",
|
||||||
"homepage": "https://hoppscotch.io",
|
"homepage": "https://hoppscotch.io",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@@ -10,6 +10,9 @@
|
|||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "pnpm exec tsup",
|
"build": "pnpm exec tsup",
|
||||||
"dev": "pnpm exec tsup --watch",
|
"dev": "pnpm exec tsup --watch",
|
||||||
@@ -38,24 +41,24 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@hoppscotch/data": "workspace:^",
|
"@hoppscotch/data": "workspace:^",
|
||||||
"@hoppscotch/js-sandbox": "workspace:^",
|
"@hoppscotch/js-sandbox": "workspace:^",
|
||||||
"@relmify/jest-fp-ts": "^2.0.2",
|
"@relmify/jest-fp-ts": "^2.1.1",
|
||||||
"@swc/core": "^1.2.181",
|
"@swc/core": "^1.3.92",
|
||||||
"@types/jest": "^27.4.1",
|
"@types/jest": "^29.5.5",
|
||||||
"@types/lodash": "^4.14.181",
|
"@types/lodash": "^4.14.199",
|
||||||
"@types/qs": "^6.9.7",
|
"@types/qs": "^6.9.8",
|
||||||
"axios": "^0.21.4",
|
"axios": "^0.21.4",
|
||||||
"chalk": "^4.1.1",
|
"chalk": "^4.1.2",
|
||||||
"commander": "^8.0.0",
|
"commander": "^11.0.0",
|
||||||
"esm": "^3.2.25",
|
"esm": "^3.2.25",
|
||||||
"fp-ts": "^2.12.1",
|
"fp-ts": "^2.16.1",
|
||||||
"io-ts": "^2.2.16",
|
"io-ts": "^2.2.20",
|
||||||
"jest": "^27.5.1",
|
"jest": "^29.7.0",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"prettier": "^2.8.4",
|
"prettier": "^3.0.3",
|
||||||
"qs": "^6.10.3",
|
"qs": "^6.11.2",
|
||||||
"ts-jest": "^27.1.4",
|
"ts-jest": "^29.1.1",
|
||||||
"tsup": "^5.12.7",
|
"tsup": "^7.2.0",
|
||||||
"typescript": "^4.6.4",
|
"typescript": "^5.2.2",
|
||||||
"zod": "^3.22.2"
|
"zod": "^3.22.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@
|
|||||||
"delete_user_success": "User deleted successfully!!",
|
"delete_user_success": "User deleted successfully!!",
|
||||||
"email": "Email",
|
"email": "Email",
|
||||||
"email_failure": "Failed to send invitation",
|
"email_failure": "Failed to send invitation",
|
||||||
|
"email_signin_failure": "Failed to login with Email",
|
||||||
"email_success": "Email invitation sent successfully",
|
"email_success": "Email invitation sent successfully",
|
||||||
"enter_team_email": "Please enter email of team owner!!",
|
"enter_team_email": "Please enter email of team owner!!",
|
||||||
"error": "Something went wrong",
|
"error": "Something went wrong",
|
||||||
@@ -50,6 +51,7 @@
|
|||||||
"logout": "Logout",
|
"logout": "Logout",
|
||||||
"magic_link_sign_in": "Click on the link to sign in.",
|
"magic_link_sign_in": "Click on the link to sign in.",
|
||||||
"magic_link_success": "We sent a magic link to",
|
"magic_link_success": "We sent a magic link to",
|
||||||
|
"microsoft_signin_failure": "Failed to login with Microsoft",
|
||||||
"non_admin_logged_in": "Logged in as non admin user.",
|
"non_admin_logged_in": "Logged in as non admin user.",
|
||||||
"non_admin_login": "You are logged in. But you're not an admin",
|
"non_admin_login": "You are logged in. But you're not an admin",
|
||||||
"privacy_policy": "Privacy Policy",
|
"privacy_policy": "Privacy Policy",
|
||||||
|
|||||||
61
packages/hoppscotch-sh-admin/src/components.d.ts
vendored
61
packages/hoppscotch-sh-admin/src/components.d.ts
vendored
@@ -1,39 +1,40 @@
|
|||||||
// generated by unplugin-vue-components
|
// generated by unplugin-vue-components
|
||||||
// We suggest you to commit this file into source control
|
// We suggest you to commit this file into source control
|
||||||
// Read more: https://github.com/vuejs/core/pull/3399
|
// Read more: https://github.com/vuejs/core/pull/3399
|
||||||
import '@vue/runtime-core';
|
import '@vue/runtime-core'
|
||||||
|
|
||||||
export {};
|
export {}
|
||||||
|
|
||||||
declare module '@vue/runtime-core' {
|
declare module '@vue/runtime-core' {
|
||||||
export interface GlobalComponents {
|
export interface GlobalComponents {
|
||||||
AppHeader: typeof import('./components/app/Header.vue')['default'];
|
AppHeader: typeof import('./components/app/Header.vue')['default']
|
||||||
AppLogin: typeof import('./components/app/Login.vue')['default'];
|
AppLogin: typeof import('./components/app/Login.vue')['default']
|
||||||
AppLogout: typeof import('./components/app/Logout.vue')['default'];
|
AppLogout: typeof import('./components/app/Logout.vue')['default']
|
||||||
AppModal: typeof import('./components/app/Modal.vue')['default'];
|
AppModal: typeof import('./components/app/Modal.vue')['default']
|
||||||
AppSidebar: typeof import('./components/app/Sidebar.vue')['default'];
|
AppSidebar: typeof import('./components/app/Sidebar.vue')['default']
|
||||||
AppToast: typeof import('./components/app/Toast.vue')['default'];
|
AppToast: typeof import('./components/app/Toast.vue')['default']
|
||||||
DashboardMetricsCard: typeof import('./components/dashboard/MetricsCard.vue')['default'];
|
DashboardMetricsCard: typeof import('./components/dashboard/MetricsCard.vue')['default']
|
||||||
HoppButtonPrimary: typeof import('@hoppscotch/ui')['HoppButtonPrimary'];
|
HoppButtonPrimary: typeof import('@hoppscotch/ui')['HoppButtonPrimary']
|
||||||
HoppButtonSecondary: typeof import('@hoppscotch/ui')['HoppButtonSecondary'];
|
HoppButtonSecondary: typeof import('@hoppscotch/ui')['HoppButtonSecondary']
|
||||||
HoppSmartAnchor: typeof import('@hoppscotch/ui')['HoppSmartAnchor'];
|
HoppSmartAnchor: typeof import('@hoppscotch/ui')['HoppSmartAnchor']
|
||||||
HoppSmartAutoComplete: typeof import('@hoppscotch/ui')['HoppSmartAutoComplete'];
|
HoppSmartAutoComplete: typeof import('@hoppscotch/ui')['HoppSmartAutoComplete']
|
||||||
HoppSmartConfirmModal: typeof import('@hoppscotch/ui')['HoppSmartConfirmModal'];
|
HoppSmartConfirmModal: typeof import('@hoppscotch/ui')['HoppSmartConfirmModal']
|
||||||
HoppSmartInput: typeof import('@hoppscotch/ui')['HoppSmartInput'];
|
HoppSmartInput: typeof import('@hoppscotch/ui')['HoppSmartInput']
|
||||||
HoppSmartItem: typeof import('@hoppscotch/ui')['HoppSmartItem'];
|
HoppSmartItem: typeof import('@hoppscotch/ui')['HoppSmartItem']
|
||||||
HoppSmartModal: typeof import('@hoppscotch/ui')['HoppSmartModal'];
|
HoppSmartModal: typeof import('@hoppscotch/ui')['HoppSmartModal']
|
||||||
HoppSmartPicture: typeof import('@hoppscotch/ui')['HoppSmartPicture'];
|
HoppSmartPicture: typeof import('@hoppscotch/ui')['HoppSmartPicture']
|
||||||
HoppSmartSpinner: typeof import('@hoppscotch/ui')['HoppSmartSpinner'];
|
HoppSmartSpinner: typeof import('@hoppscotch/ui')['HoppSmartSpinner']
|
||||||
IconLucideChevronDown: typeof import('~icons/lucide/chevron-down')['default'];
|
IconLucideChevronDown: typeof import('~icons/lucide/chevron-down')['default']
|
||||||
IconLucideInbox: typeof import('~icons/lucide/inbox')['default'];
|
IconLucideInbox: typeof import('~icons/lucide/inbox')['default']
|
||||||
TeamsAdd: typeof import('./components/teams/Add.vue')['default'];
|
TeamsAdd: typeof import('./components/teams/Add.vue')['default']
|
||||||
TeamsDetails: typeof import('./components/teams/Details.vue')['default'];
|
TeamsDetails: typeof import('./components/teams/Details.vue')['default']
|
||||||
TeamsInvite: typeof import('./components/teams/Invite.vue')['default'];
|
TeamsInvite: typeof import('./components/teams/Invite.vue')['default']
|
||||||
TeamsMembers: typeof import('./components/teams/Members.vue')['default'];
|
TeamsMembers: typeof import('./components/teams/Members.vue')['default']
|
||||||
TeamsPendingInvites: typeof import('./components/teams/PendingInvites.vue')['default'];
|
TeamsPendingInvites: typeof import('./components/teams/PendingInvites.vue')['default']
|
||||||
TeamsTable: typeof import('./components/teams/Table.vue')['default'];
|
TeamsTable: typeof import('./components/teams/Table.vue')['default']
|
||||||
Tippy: typeof import('vue-tippy')['Tippy'];
|
Tippy: typeof import('vue-tippy')['Tippy']
|
||||||
UsersInviteModal: typeof import('./components/users/InviteModal.vue')['default'];
|
UsersInviteModal: typeof import('./components/users/InviteModal.vue')['default']
|
||||||
UsersTable: typeof import('./components/users/Table.vue')['default'];
|
UsersTable: typeof import('./components/users/Table.vue')['default']
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,8 +89,8 @@ const t = useI18n();
|
|||||||
const { isOpen, isExpanded } = useSidebar();
|
const { isOpen, isExpanded } = useSidebar();
|
||||||
|
|
||||||
const currentUser = useReadonlyStream(
|
const currentUser = useReadonlyStream(
|
||||||
auth.getProbableUserStream(),
|
auth.getCurrentUserStream(),
|
||||||
auth.getProbableUser()
|
auth.getCurrentUser()
|
||||||
);
|
);
|
||||||
|
|
||||||
const expandSidebar = () => {
|
const expandSidebar = () => {
|
||||||
|
|||||||
@@ -184,91 +184,71 @@ onMounted(() => {
|
|||||||
subscribeToStream(currentUser$, (user) => {
|
subscribeToStream(currentUser$, (user) => {
|
||||||
if (user && !user.isAdmin) {
|
if (user && !user.isAdmin) {
|
||||||
nonAdminUser.value = true;
|
nonAdminUser.value = true;
|
||||||
toast.error(`${t('state.non_admin_login')}`);
|
toast.error(t('state.non_admin_login'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
async function signInWithGoogle() {
|
const signInWithGoogle = () => {
|
||||||
signingInWithGoogle.value = true;
|
signingInWithGoogle.value = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await auth.signInUserWithGoogle();
|
auth.signInUserWithGoogle();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
/*
|
toast.error(t('state.google_signin_failure'));
|
||||||
A auth/account-exists-with-different-credential Firebase error wont happen between Google and any other providers
|
|
||||||
Seems Google account overwrites accounts of other providers https://github.com/firebase/firebase-android-sdk/issues/25
|
|
||||||
*/
|
|
||||||
toast.error(`${t('state.google_signin_failure')}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
signingInWithGoogle.value = false;
|
signingInWithGoogle.value = false;
|
||||||
}
|
};
|
||||||
async function signInWithGithub() {
|
|
||||||
|
const signInWithGithub = () => {
|
||||||
signingInWithGitHub.value = true;
|
signingInWithGitHub.value = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await auth.signInUserWithGithub();
|
auth.signInUserWithGithub();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
/*
|
toast.error(t('state.github_signin_failure'));
|
||||||
A auth/account-exists-with-different-credential Firebase error wont happen between Google and any other providers
|
|
||||||
Seems Google account overwrites accounts of other providers https://github.com/firebase/firebase-android-sdk/issues/25
|
|
||||||
*/
|
|
||||||
toast.error(`${t('state.github_signin_failure')}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
signingInWithGitHub.value = false;
|
signingInWithGitHub.value = false;
|
||||||
}
|
};
|
||||||
|
|
||||||
async function signInWithMicrosoft() {
|
const signInWithMicrosoft = () => {
|
||||||
signingInWithMicrosoft.value = true;
|
signingInWithMicrosoft.value = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await auth.signInUserWithMicrosoft();
|
auth.signInUserWithMicrosoft();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
/*
|
toast.error(t('state.microsoft_signin_failure'));
|
||||||
A auth/account-exists-with-different-credential Firebase error wont happen between MS with Google or Github
|
|
||||||
If a Github account exists and user then logs in with MS email we get a "Something went wrong toast" and console errors and MS replaces GH as only provider.
|
|
||||||
The error messages are as follows:
|
|
||||||
FirebaseError: Firebase: Error (auth/popup-closed-by-user).
|
|
||||||
@firebase/auth: Auth (9.6.11): INTERNAL ASSERTION FAILED: Pending promise was never set
|
|
||||||
They may be related to https://github.com/firebase/firebaseui-web/issues/947
|
|
||||||
*/
|
|
||||||
toast.error(`${t('state.error')}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
signingInWithMicrosoft.value = false;
|
signingInWithMicrosoft.value = false;
|
||||||
}
|
};
|
||||||
async function signInWithEmail() {
|
|
||||||
signingInWithEmail.value = true;
|
|
||||||
|
|
||||||
await auth
|
const signInWithEmail = async () => {
|
||||||
.signInWithEmail(form.value.email)
|
signingInWithEmail.value = true;
|
||||||
.then(() => {
|
try {
|
||||||
mode.value = 'email-sent';
|
await auth.signInWithEmail(form.value.email);
|
||||||
setLocalConfig('emailForSignIn', form.value.email);
|
mode.value = 'email-sent';
|
||||||
})
|
setLocalConfig('emailForSignIn', form.value.email);
|
||||||
.catch((e: any) => {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
toast.error(e.message);
|
toast.error(t('state.email_signin_failure'));
|
||||||
signingInWithEmail.value = false;
|
}
|
||||||
})
|
signingInWithEmail.value = false;
|
||||||
.finally(() => {
|
};
|
||||||
signingInWithEmail.value = false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
try {
|
try {
|
||||||
await auth.signOutUser();
|
await auth.signOutUser();
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
toast.success(`${t('state.logged_out')}`);
|
toast.success(t('state.logged_out'));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
toast.error(`${t('state.error')}`);
|
toast.error(t('state.error'));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ import {
|
|||||||
} from '../../helpers/backend/graphql';
|
} from '../../helpers/backend/graphql';
|
||||||
import { useToast } from '~/composables/toast';
|
import { useToast } from '~/composables/toast';
|
||||||
import { useMutation, useQuery } from '@urql/vue';
|
import { useMutation, useQuery } from '@urql/vue';
|
||||||
import { Email, EmailCodec } from '~/helpers/backend/Email';
|
import { Email, EmailCodec } from '~/helpers/Email';
|
||||||
import IconTrash from '~icons/lucide/trash';
|
import IconTrash from '~icons/lucide/trash';
|
||||||
import IconPlus from '~icons/lucide/plus';
|
import IconPlus from '~icons/lucide/plus';
|
||||||
import IconCircleDot from '~icons/lucide/circle-dot';
|
import IconCircleDot from '~icons/lucide/circle-dot';
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
import { platform } from '~/platform';
|
|
||||||
import { AuthEvent, HoppUser } from '~/platform/auth';
|
|
||||||
import { Subscription } from 'rxjs';
|
|
||||||
import { onBeforeUnmount, onMounted, watch, WatchStopHandle } from 'vue';
|
|
||||||
import { useReadonlyStream } from './stream';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A Vue composable function that is called when the auth status
|
|
||||||
* is being updated to being logged in (fired multiple times),
|
|
||||||
* this is also called on component mount if the login
|
|
||||||
* was already resolved before mount.
|
|
||||||
*/
|
|
||||||
export function onLoggedIn(exec: (user: HoppUser) => void) {
|
|
||||||
const currentUser = useReadonlyStream(
|
|
||||||
platform.auth.getCurrentUserStream(),
|
|
||||||
platform.auth.getCurrentUser()
|
|
||||||
);
|
|
||||||
|
|
||||||
let watchStop: WatchStopHandle | null = null;
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
if (currentUser.value) exec(currentUser.value);
|
|
||||||
|
|
||||||
watchStop = watch(currentUser, (newVal, prev) => {
|
|
||||||
if (prev === null && newVal !== null) {
|
|
||||||
exec(newVal);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
|
||||||
watchStop?.();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A Vue composable function that calls its param function
|
|
||||||
* when a new event (login, logout etc.) happens in
|
|
||||||
* the auth system.
|
|
||||||
*
|
|
||||||
* NOTE: Unlike `onLoggedIn` for which the callback will be called once on mount with the current state,
|
|
||||||
* here the callback will only be called on authentication event occurances.
|
|
||||||
* You might want to check the auth state from an `onMounted` hook or something
|
|
||||||
* if you want to access the initial state
|
|
||||||
*
|
|
||||||
* @param func A function which accepts an event
|
|
||||||
*/
|
|
||||||
export function onAuthEvent(func: (ev: AuthEvent) => void) {
|
|
||||||
const authEvents$ = platform.auth.getAuthEventsStream();
|
|
||||||
|
|
||||||
let sub: Subscription | null = null;
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
sub = authEvents$.subscribe((ev) => {
|
|
||||||
func(ev);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
|
||||||
sub?.unsubscribe();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
import axios from 'axios';
|
|
||||||
import { BehaviorSubject, Subject } from 'rxjs';
|
import { BehaviorSubject, Subject } from 'rxjs';
|
||||||
import {
|
import {
|
||||||
getLocalConfig,
|
getLocalConfig,
|
||||||
removeLocalConfig,
|
removeLocalConfig,
|
||||||
setLocalConfig,
|
setLocalConfig,
|
||||||
} from './localpersistence';
|
} from './localpersistence';
|
||||||
import { Ref, ref, watch } from 'vue';
|
import { Ref, ref } from 'vue';
|
||||||
import * as O from 'fp-ts/Option';
|
import * as O from 'fp-ts/Option';
|
||||||
|
import authQuery from './backend/rest/authQuery';
|
||||||
|
import { COOKIES_NOT_FOUND, UNAUTHORIZED } from './errors';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A common (and required) set of fields that describe a user.
|
* A common (and required) set of fields that describe a user.
|
||||||
*/
|
*/
|
||||||
@@ -23,22 +25,16 @@ export type HoppUser = {
|
|||||||
/** URL to the profile picture of the user */
|
/** URL to the profile picture of the user */
|
||||||
photoURL: string | null;
|
photoURL: string | null;
|
||||||
|
|
||||||
// Regarding `provider` and `accessToken`:
|
|
||||||
// The current implementation and use case for these 2 fields are super weird due to legacy.
|
|
||||||
// Currrently these fields are only basically populated for Github Auth as we need the access token issued
|
|
||||||
// by it to implement Gist submission. I would really love refactor to make this thing more sane.
|
|
||||||
|
|
||||||
/** Name of the provider authenticating (NOTE: See notes on `platform/auth.ts`) */
|
/** Name of the provider authenticating (NOTE: See notes on `platform/auth.ts`) */
|
||||||
provider?: string;
|
provider?: string;
|
||||||
/** Access Token for the auth of the user against the given `provider`. */
|
/** Access Token for the auth of the user against the given `provider`. */
|
||||||
accessToken?: string;
|
accessToken?: string;
|
||||||
emailVerified: boolean;
|
emailVerified: boolean;
|
||||||
|
/** Flag to check for admin status */
|
||||||
isAdmin: boolean;
|
isAdmin: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type AuthEvent =
|
export type AuthEvent =
|
||||||
| { event: 'probable_login'; user: HoppUser } // We have previous login state, but the app is waiting for authentication
|
|
||||||
| { event: 'login'; user: HoppUser } // We are authenticated
|
| { event: 'login'; user: HoppUser } // We are authenticated
|
||||||
| { event: 'logout' } // No authentication and we have no previous state
|
| { event: 'logout' } // No authentication and we have no previous state
|
||||||
| { event: 'token_refresh' }; // We have previous login state, but the app is waiting for authentication
|
| { event: 'token_refresh' }; // We have previous login state, but the app is waiting for authentication
|
||||||
@@ -51,17 +47,11 @@ export type GithubSignInResult =
|
|||||||
export const authEvents$ = new Subject<
|
export const authEvents$ = new Subject<
|
||||||
AuthEvent | { event: 'token_refresh' }
|
AuthEvent | { event: 'token_refresh' }
|
||||||
>();
|
>();
|
||||||
const currentUser$ = new BehaviorSubject<HoppUser | null>(null);
|
|
||||||
export const probableUser$ = new BehaviorSubject<HoppUser | null>(null);
|
|
||||||
|
|
||||||
async function logout() {
|
const currentUser$ = new BehaviorSubject<HoppUser | null>(null);
|
||||||
await axios.get(`${import.meta.env.VITE_BACKEND_API_URL}/auth/logout`, {
|
|
||||||
withCredentials: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const signOut = async (reloadWindow = false) => {
|
const signOut = async (reloadWindow = false) => {
|
||||||
await logout();
|
await authQuery.logout();
|
||||||
|
|
||||||
// Reload the window if both `access_token` and `refresh_token`is invalid
|
// Reload the window if both `access_token` and `refresh_token`is invalid
|
||||||
// there by the user is taken to the login page
|
// there by the user is taken to the login page
|
||||||
@@ -69,7 +59,6 @@ const signOut = async (reloadWindow = false) => {
|
|||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
probableUser$.next(null);
|
|
||||||
currentUser$.next(null);
|
currentUser$.next(null);
|
||||||
removeLocalConfig('login_state');
|
removeLocalConfig('login_state');
|
||||||
|
|
||||||
@@ -78,142 +67,66 @@ const signOut = async (reloadWindow = false) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
async function signInUserWithGithubFB() {
|
const getInitialUserDetails = async () => {
|
||||||
window.location.href = `${
|
const res = await authQuery.getUserDetails();
|
||||||
import.meta.env.VITE_BACKEND_API_URL
|
|
||||||
}/auth/github?redirect_uri=${import.meta.env.VITE_ADMIN_URL}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function signInUserWithGoogleFB() {
|
|
||||||
window.location.href = `${
|
|
||||||
import.meta.env.VITE_BACKEND_API_URL
|
|
||||||
}/auth/google?redirect_uri=${import.meta.env.VITE_ADMIN_URL}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function signInUserWithMicrosoftFB() {
|
|
||||||
window.location.href = `${
|
|
||||||
import.meta.env.VITE_BACKEND_API_URL
|
|
||||||
}/auth/microsoft?redirect_uri=${import.meta.env.VITE_ADMIN_URL}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getInitialUserDetails() {
|
|
||||||
const res = await axios.post<{
|
|
||||||
data?: {
|
|
||||||
me?: {
|
|
||||||
uid: string;
|
|
||||||
displayName: string;
|
|
||||||
email: string;
|
|
||||||
photoURL: string;
|
|
||||||
isAdmin: boolean;
|
|
||||||
createdOn: string;
|
|
||||||
// emailVerified: boolean
|
|
||||||
};
|
|
||||||
};
|
|
||||||
errors?: Array<{
|
|
||||||
message: string;
|
|
||||||
}>;
|
|
||||||
}>(
|
|
||||||
`${import.meta.env.VITE_BACKEND_GQL_URL}`,
|
|
||||||
{
|
|
||||||
query: `query Me {
|
|
||||||
me {
|
|
||||||
uid
|
|
||||||
displayName
|
|
||||||
email
|
|
||||||
photoURL
|
|
||||||
isAdmin
|
|
||||||
createdOn
|
|
||||||
}
|
|
||||||
}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
withCredentials: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.data;
|
return res.data;
|
||||||
}
|
};
|
||||||
|
|
||||||
const isGettingInitialUser: Ref<null | boolean> = ref(null);
|
const isGettingInitialUser: Ref<null | boolean> = ref(null);
|
||||||
|
|
||||||
function setUser(user: HoppUser | null) {
|
const setUser = (user: HoppUser | null) => {
|
||||||
currentUser$.next(user);
|
currentUser$.next(user);
|
||||||
probableUser$.next(user);
|
|
||||||
|
|
||||||
setLocalConfig('login_state', JSON.stringify(user));
|
setLocalConfig('login_state', JSON.stringify(user));
|
||||||
}
|
};
|
||||||
|
|
||||||
async function setInitialUser() {
|
const setInitialUser = async () => {
|
||||||
isGettingInitialUser.value = true;
|
isGettingInitialUser.value = true;
|
||||||
const res = await getInitialUserDetails();
|
const res = await getInitialUserDetails();
|
||||||
|
|
||||||
const error = res.errors && res.errors[0];
|
if (res.errors?.[0]) {
|
||||||
|
const [error] = res.errors;
|
||||||
|
|
||||||
// no cookies sent. so the user is not logged in
|
if (error.message === COOKIES_NOT_FOUND) {
|
||||||
if (error && error.message === 'auth/cookies_not_found') {
|
|
||||||
setUser(null);
|
|
||||||
isGettingInitialUser.value = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// cookies sent, but it is expired, we need to refresh the token
|
|
||||||
if (error && error.message === 'Unauthorized') {
|
|
||||||
const isRefreshSuccess = await refreshToken();
|
|
||||||
|
|
||||||
if (isRefreshSuccess) {
|
|
||||||
setInitialUser();
|
|
||||||
} else {
|
|
||||||
setUser(null);
|
setUser(null);
|
||||||
await signOut(true);
|
} else if (error.message === UNAUTHORIZED) {
|
||||||
isGettingInitialUser.value = false;
|
const isRefreshSuccess = await refreshToken();
|
||||||
|
|
||||||
|
if (isRefreshSuccess) {
|
||||||
|
setInitialUser();
|
||||||
|
} else {
|
||||||
|
setUser(null);
|
||||||
|
signOut(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else if (res.data?.me) {
|
||||||
return;
|
const { uid, displayName, email, photoURL, isAdmin } = res.data.me;
|
||||||
}
|
|
||||||
|
|
||||||
// no errors, we have a valid user
|
|
||||||
if (res.data && res.data.me) {
|
|
||||||
const hoppBackendUser = res.data.me;
|
|
||||||
|
|
||||||
const hoppUser: HoppUser = {
|
const hoppUser: HoppUser = {
|
||||||
uid: hoppBackendUser.uid,
|
uid,
|
||||||
displayName: hoppBackendUser.displayName,
|
displayName,
|
||||||
email: hoppBackendUser.email,
|
email,
|
||||||
photoURL: hoppBackendUser.photoURL,
|
photoURL,
|
||||||
// all our signin methods currently guarantees the email is verified
|
|
||||||
emailVerified: true,
|
emailVerified: true,
|
||||||
isAdmin: hoppBackendUser.isAdmin,
|
isAdmin,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!hoppUser.isAdmin) {
|
if (!hoppUser.isAdmin) {
|
||||||
const isAdmin = await elevateUser();
|
hoppUser.isAdmin = await elevateUser();
|
||||||
hoppUser.isAdmin = isAdmin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setUser(hoppUser);
|
setUser(hoppUser);
|
||||||
|
|
||||||
isGettingInitialUser.value = false;
|
|
||||||
|
|
||||||
authEvents$.next({
|
authEvents$.next({
|
||||||
event: 'login',
|
event: 'login',
|
||||||
user: hoppUser,
|
user: hoppUser,
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
isGettingInitialUser.value = false;
|
||||||
|
};
|
||||||
|
|
||||||
const refreshToken = async () => {
|
const refreshToken = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get(
|
const res = await authQuery.refreshToken();
|
||||||
`${import.meta.env.VITE_BACKEND_API_URL}/auth/refresh`,
|
|
||||||
{
|
|
||||||
withCredentials: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
authEvents$.next({
|
authEvents$.next({
|
||||||
event: 'token_refresh',
|
event: 'token_refresh',
|
||||||
});
|
});
|
||||||
@@ -223,157 +136,67 @@ const refreshToken = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function elevateUser() {
|
const elevateUser = async () => {
|
||||||
const res = await axios.get(
|
const res = await authQuery.elevateUser();
|
||||||
`${import.meta.env.VITE_BACKEND_API_URL}/auth/verify/admin`,
|
return Boolean(res.data?.isAdmin);
|
||||||
{
|
};
|
||||||
withCredentials: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return !!res.data?.isAdmin;
|
const sendMagicLink = async (email: string) => {
|
||||||
}
|
const res = await authQuery.sendMagicLink(email);
|
||||||
|
if (!res.data?.deviceIdentifier) {
|
||||||
async function sendMagicLink(email: string) {
|
|
||||||
const res = await axios.post(
|
|
||||||
`${import.meta.env.VITE_BACKEND_API_URL}/auth/signin?origin=admin`,
|
|
||||||
{
|
|
||||||
email,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
withCredentials: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (res.data && res.data.deviceIdentifier) {
|
|
||||||
setLocalConfig('deviceIdentifier', res.data.deviceIdentifier);
|
|
||||||
} else {
|
|
||||||
throw new Error('test: does not get device identifier');
|
throw new Error('test: does not get device identifier');
|
||||||
}
|
}
|
||||||
|
setLocalConfig('deviceIdentifier', res.data.deviceIdentifier);
|
||||||
return res.data;
|
return res.data;
|
||||||
}
|
};
|
||||||
|
|
||||||
export const auth = {
|
export const auth = {
|
||||||
getCurrentUserStream: () => currentUser$,
|
getCurrentUserStream: () => currentUser$,
|
||||||
getAuthEventsStream: () => authEvents$,
|
getAuthEventsStream: () => authEvents$,
|
||||||
getProbableUserStream: () => probableUser$,
|
|
||||||
|
|
||||||
getCurrentUser: () => currentUser$.value,
|
getCurrentUser: () => currentUser$.value,
|
||||||
getProbableUser: () => probableUser$.value,
|
|
||||||
|
|
||||||
getBackendHeaders() {
|
performAuthInit: () => {
|
||||||
return {};
|
const currentUser = JSON.parse(getLocalConfig('login_state') ?? 'null');
|
||||||
},
|
currentUser$.next(currentUser);
|
||||||
getGQLClientOptions() {
|
return setInitialUser();
|
||||||
return {
|
|
||||||
fetchOptions: {
|
|
||||||
credentials: 'include',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
signInWithEmail: (email: string) => sendMagicLink(email),
|
||||||
* it is not possible for us to know if the current cookie is expired because we cannot access http-only cookies from js
|
|
||||||
* hence just returning if the currentUser$ has a value associated with it
|
|
||||||
*/
|
|
||||||
willBackendHaveAuthError() {
|
|
||||||
return !currentUser$.value;
|
|
||||||
},
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
onBackendGQLClientShouldReconnect(func: () => void) {
|
|
||||||
authEvents$.subscribe((event) => {
|
|
||||||
if (
|
|
||||||
event.event == 'login' ||
|
|
||||||
event.event == 'logout' ||
|
|
||||||
event.event == 'token_refresh'
|
|
||||||
) {
|
|
||||||
func();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
isSignInWithEmailLink: (url: string) => {
|
||||||
* we cannot access our auth cookies from javascript, so leaving this as null
|
|
||||||
*/
|
|
||||||
getDevOptsBackendIDToken() {
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
async performAuthInit() {
|
|
||||||
const probableUser = JSON.parse(getLocalConfig('login_state') ?? 'null');
|
|
||||||
probableUser$.next(probableUser);
|
|
||||||
await setInitialUser();
|
|
||||||
},
|
|
||||||
|
|
||||||
waitProbableLoginToConfirm() {
|
|
||||||
return new Promise<void>((resolve, reject) => {
|
|
||||||
if (this.getCurrentUser()) {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!probableUser$.value) reject(new Error('no_probable_user'));
|
|
||||||
|
|
||||||
const unwatch = watch(isGettingInitialUser, (val) => {
|
|
||||||
if (val === true || val === false) {
|
|
||||||
resolve();
|
|
||||||
unwatch();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
async signInWithEmail(email: string) {
|
|
||||||
await sendMagicLink(email);
|
|
||||||
},
|
|
||||||
|
|
||||||
isSignInWithEmailLink(url: string) {
|
|
||||||
const urlObject = new URL(url);
|
const urlObject = new URL(url);
|
||||||
const searchParams = new URLSearchParams(urlObject.search);
|
const searchParams = new URLSearchParams(urlObject.search);
|
||||||
|
return Boolean(searchParams.get('token'));
|
||||||
return !!searchParams.get('token');
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async verifyEmailAddress() {
|
signInUserWithGoogle: () => {
|
||||||
return;
|
window.location.href = `${
|
||||||
|
import.meta.env.VITE_BACKEND_API_URL
|
||||||
|
}/auth/google?redirect_uri=${import.meta.env.VITE_ADMIN_URL}`;
|
||||||
},
|
},
|
||||||
async signInUserWithGoogle() {
|
|
||||||
await signInUserWithGoogleFB();
|
signInUserWithGithub: () => {
|
||||||
|
window.location.href = `${
|
||||||
|
import.meta.env.VITE_BACKEND_API_URL
|
||||||
|
}/auth/github?redirect_uri=${import.meta.env.VITE_ADMIN_URL}`;
|
||||||
},
|
},
|
||||||
async signInUserWithGithub() {
|
|
||||||
await signInUserWithGithubFB();
|
signInUserWithMicrosoft: () => {
|
||||||
return undefined;
|
window.location.href = `${
|
||||||
|
import.meta.env.VITE_BACKEND_API_URL
|
||||||
|
}/auth/microsoft?redirect_uri=${import.meta.env.VITE_ADMIN_URL}`;
|
||||||
},
|
},
|
||||||
async signInUserWithMicrosoft() {
|
|
||||||
await signInUserWithMicrosoftFB();
|
signInWithEmailLink: (url: string) => {
|
||||||
},
|
|
||||||
async signInWithEmailLink(email: string, url: string) {
|
|
||||||
const urlObject = new URL(url);
|
const urlObject = new URL(url);
|
||||||
const searchParams = new URLSearchParams(urlObject.search);
|
const searchParams = new URLSearchParams(urlObject.search);
|
||||||
|
|
||||||
const token = searchParams.get('token');
|
const token = searchParams.get('token');
|
||||||
const deviceIdentifier = getLocalConfig('deviceIdentifier');
|
const deviceIdentifier = getLocalConfig('deviceIdentifier');
|
||||||
|
|
||||||
await axios.post(
|
return authQuery.signInWithEmailLink(token, deviceIdentifier);
|
||||||
`${import.meta.env.VITE_BACKEND_API_URL}/auth/verify`,
|
|
||||||
{
|
|
||||||
token: token,
|
|
||||||
deviceIdentifier,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
withCredentials: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
},
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
async setEmailAddress(_email: string) {
|
|
||||||
return;
|
|
||||||
},
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
async setDisplayName(name: string) {
|
|
||||||
return;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async performAuthRefresh() {
|
performAuthRefresh: async () => {
|
||||||
const isRefreshSuccess = await refreshToken();
|
const isRefreshSuccess = await refreshToken();
|
||||||
|
|
||||||
if (isRefreshSuccess) {
|
if (isRefreshSuccess) {
|
||||||
@@ -386,12 +209,10 @@ export const auth = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async signOutUser(reloadWindow = false) {
|
signOutUser: (reloadWindow = false) => signOut(reloadWindow),
|
||||||
await signOut(reloadWindow);
|
|
||||||
},
|
|
||||||
|
|
||||||
async processMagicLink() {
|
processMagicLink: async () => {
|
||||||
if (this.isSignInWithEmailLink(window.location.href)) {
|
if (auth.isSignInWithEmailLink(window.location.href)) {
|
||||||
const deviceIdentifier = getLocalConfig('deviceIdentifier');
|
const deviceIdentifier = getLocalConfig('deviceIdentifier');
|
||||||
|
|
||||||
if (!deviceIdentifier) {
|
if (!deviceIdentifier) {
|
||||||
@@ -400,7 +221,7 @@ export const auth = {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.signInWithEmailLink(deviceIdentifier, window.location.href);
|
await auth.signInWithEmailLink(window.location.href);
|
||||||
|
|
||||||
removeLocalConfig('deviceIdentifier');
|
removeLocalConfig('deviceIdentifier');
|
||||||
window.location.href = import.meta.env.VITE_ADMIN_URL;
|
window.location.href = import.meta.env.VITE_ADMIN_URL;
|
||||||
|
|||||||
20
packages/hoppscotch-sh-admin/src/helpers/axiosConfig.ts
Normal file
20
packages/hoppscotch-sh-admin/src/helpers/axiosConfig.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
const baseConfig = {
|
||||||
|
headers: {
|
||||||
|
'Content-type': 'application/json',
|
||||||
|
},
|
||||||
|
withCredentials: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const gqlApi = axios.create({
|
||||||
|
...baseConfig,
|
||||||
|
baseURL: import.meta.env.VITE_BACKEND_GQL_URL,
|
||||||
|
});
|
||||||
|
|
||||||
|
const restApi = axios.create({
|
||||||
|
...baseConfig,
|
||||||
|
baseURL: import.meta.env.VITE_BACKEND_API_URL,
|
||||||
|
});
|
||||||
|
|
||||||
|
export { gqlApi, restApi };
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { gqlApi, restApi } from '~/helpers/axiosConfig';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
getUserDetails: () =>
|
||||||
|
gqlApi.post('', {
|
||||||
|
query: `query Me {
|
||||||
|
me {
|
||||||
|
uid
|
||||||
|
displayName
|
||||||
|
email
|
||||||
|
photoURL
|
||||||
|
isAdmin
|
||||||
|
createdOn
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
}),
|
||||||
|
refreshToken: () => restApi.get('/auth/refresh'),
|
||||||
|
elevateUser: () => restApi.get('/auth/verify/admin'),
|
||||||
|
sendMagicLink: (email: string) =>
|
||||||
|
restApi.post('/auth/signin?origin=admin', {
|
||||||
|
email,
|
||||||
|
}),
|
||||||
|
signInWithEmailLink: (
|
||||||
|
token: string | null,
|
||||||
|
deviceIdentifier: string | null
|
||||||
|
) =>
|
||||||
|
restApi.post('/auth/verify', {
|
||||||
|
token,
|
||||||
|
deviceIdentifier,
|
||||||
|
}),
|
||||||
|
logout: () => restApi.get('/auth/logout'),
|
||||||
|
};
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
export const throwError = (message: string): never => {
|
|
||||||
throw new Error(message)
|
|
||||||
}
|
|
||||||
9
packages/hoppscotch-sh-admin/src/helpers/errors.ts
Normal file
9
packages/hoppscotch-sh-admin/src/helpers/errors.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
/* No cookies were found in the auth request
|
||||||
|
* (AuthService)
|
||||||
|
*/
|
||||||
|
export const COOKIES_NOT_FOUND = 'auth/cookies_not_found' as const;
|
||||||
|
|
||||||
|
export const UNAUTHORIZED = 'Unauthorized' as const;
|
||||||
|
|
||||||
|
// Sometimes the backend returns Unauthorized error message as follows:
|
||||||
|
export const GRAPHQL_UNAUTHORIZED = '[GraphQL] Unauthorized' as const;
|
||||||
@@ -16,6 +16,7 @@ import { HOPP_MODULES } from './modules';
|
|||||||
import { auth } from './helpers/auth';
|
import { auth } from './helpers/auth';
|
||||||
import { pipe } from 'fp-ts/function';
|
import { pipe } from 'fp-ts/function';
|
||||||
import * as O from 'fp-ts/Option';
|
import * as O from 'fp-ts/Option';
|
||||||
|
import { GRAPHQL_UNAUTHORIZED } from './helpers/errors';
|
||||||
|
|
||||||
// Top-level await is not available in our targets
|
// Top-level await is not available in our targets
|
||||||
(async () => {
|
(async () => {
|
||||||
@@ -40,12 +41,12 @@ import * as O from 'fp-ts/Option';
|
|||||||
async refreshAuth() {
|
async refreshAuth() {
|
||||||
pipe(
|
pipe(
|
||||||
await auth.performAuthRefresh(),
|
await auth.performAuthRefresh(),
|
||||||
O.getOrElseW(async () => await auth.signOutUser(true))
|
O.getOrElseW(() => auth.signOutUser(true))
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
didAuthError(error, _operation) {
|
didAuthError(error, _operation) {
|
||||||
return error.message === '[GraphQL] Unauthorized';
|
return error.message === GRAPHQL_UNAUTHORIZED;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ import { auth } from '~/helpers/auth';
|
|||||||
const signingInWithEmail = ref(false);
|
const signingInWithEmail = ref(false);
|
||||||
const error = ref(null);
|
const error = ref(null);
|
||||||
|
|
||||||
onBeforeMount(() => {
|
onBeforeMount(async () => {
|
||||||
auth.performAuthInit();
|
await auth.performAuthInit();
|
||||||
});
|
});
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
|
|||||||
8155
pnpm-lock.yaml
generated
8155
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user