Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
chore: made the required changes in auth and user modules to accommodate changes made in user schema
Balu Babuc38ad89cd718
diff --git a/packages/hoppscotch-backend/prisma/migrations/20230112140525_auth/migration.sql b/packages/hoppscotch-backend/prisma/migrations/20230112140525_auth/migration.sqldeleted file mode 100644index e13e07dcd..000000000--- a/packages/hoppscotch-backend/prisma/migrations/20230112140525_auth/migration.sql+++ /dev/null@@ -1,158 +0,0 @@--- CreateEnum-CREATE TYPE "TeamMemberRole" AS ENUM ('OWNER', 'VIEWER', 'EDITOR');---- CreateTable-CREATE TABLE "Team" (- "id" TEXT NOT NULL,- "name" TEXT NOT NULL,-- CONSTRAINT "Team_pkey" PRIMARY KEY ("id")-);---- CreateTable-CREATE TABLE "TeamMember" (- "id" TEXT NOT NULL,- "role" "TeamMemberRole" NOT NULL,- "userUid" TEXT NOT NULL,- "teamID" TEXT NOT NULL,-- CONSTRAINT "TeamMember_pkey" PRIMARY KEY ("id")-);---- CreateTable-CREATE TABLE "TeamInvitation" (- "id" TEXT NOT NULL,- "teamID" TEXT NOT NULL,- "creatorUid" TEXT NOT NULL,- "inviteeEmail" TEXT NOT NULL,- "inviteeRole" "TeamMemberRole" NOT NULL,-- CONSTRAINT "TeamInvitation_pkey" PRIMARY KEY ("id")-);---- CreateTable-CREATE TABLE "TeamCollection" (- "id" TEXT NOT NULL,- "parentID" TEXT,- "teamID" TEXT NOT NULL,- "title" TEXT NOT NULL,-- CONSTRAINT "TeamCollection_pkey" PRIMARY KEY ("id")-);---- CreateTable-CREATE TABLE "TeamRequest" (- "id" TEXT NOT NULL,- "collectionID" TEXT NOT NULL,- "teamID" TEXT NOT NULL,- "title" TEXT NOT NULL,- "request" JSONB NOT NULL,-- CONSTRAINT "TeamRequest_pkey" PRIMARY KEY ("id")-);---- CreateTable-CREATE TABLE "Shortcode" (- "id" TEXT NOT NULL,- "request" JSONB NOT NULL,- "creatorUid" TEXT,- "createdOn" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,-- CONSTRAINT "Shortcode_pkey" PRIMARY KEY ("id")-);---- CreateTable-CREATE TABLE "TeamEnvironment" (- "id" TEXT NOT NULL,- "teamID" TEXT NOT NULL,- "name" TEXT NOT NULL,- "variables" JSONB NOT NULL,-- CONSTRAINT "TeamEnvironment_pkey" PRIMARY KEY ("id")-);---- CreateTable-CREATE TABLE "User" (- "id" TEXT NOT NULL,- "name" TEXT,- "email" TEXT,- "image" TEXT,- "isAdmin" BOOLEAN NOT NULL DEFAULT false,- "refreshToken" TEXT,- "createdOn" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,-- CONSTRAINT "User_pkey" PRIMARY KEY ("id")-);---- CreateTable-CREATE TABLE "Account" (- "id" TEXT NOT NULL,- "userId" TEXT NOT NULL,- "provider" TEXT NOT NULL,- "providerAccountId" TEXT NOT NULL,- "providerRefreshToken" TEXT,- "providerAccessToken" TEXT,- "providerScope" TEXT,- "loggedIn" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,-- CONSTRAINT "Account_pkey" PRIMARY KEY ("id")-);---- CreateTable-CREATE TABLE "PasswordlessVerification" (- "deviceIdentifier" TEXT NOT NULL,- "token" TEXT NOT NULL,- "userUid" TEXT NOT NULL,- "expiresOn" TIMESTAMPTZ(3) NOT NULL-);---- CreateIndex-CREATE UNIQUE INDEX "TeamMember_teamID_userUid_key" ON "TeamMember"("teamID", "userUid");---- CreateIndex-CREATE INDEX "TeamInvitation_teamID_idx" ON "TeamInvitation"("teamID");---- CreateIndex-CREATE UNIQUE INDEX "TeamInvitation_teamID_inviteeEmail_key" ON "TeamInvitation"("teamID", "inviteeEmail");---- CreateIndex-CREATE UNIQUE INDEX "Shortcode_id_creatorUid_key" ON "Shortcode"("id", "creatorUid");---- CreateIndex-CREATE UNIQUE INDEX "User_email_key" ON "User"("email");---- CreateIndex-CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId");---- CreateIndex-CREATE UNIQUE INDEX "PasswordlessVerification_token_key" ON "PasswordlessVerification"("token");---- CreateIndex-CREATE UNIQUE INDEX "PasswordlessVerification_deviceIdentifier_token_key" ON "PasswordlessVerification"("deviceIdentifier", "token");---- AddForeignKey-ALTER TABLE "TeamMember" ADD CONSTRAINT "TeamMember_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;---- AddForeignKey-ALTER TABLE "TeamInvitation" ADD CONSTRAINT "TeamInvitation_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;---- AddForeignKey-ALTER TABLE "TeamCollection" ADD CONSTRAINT "TeamCollection_parentID_fkey" FOREIGN KEY ("parentID") REFERENCES "TeamCollection"("id") ON DELETE SET NULL ON UPDATE CASCADE;---- AddForeignKey-ALTER TABLE "TeamCollection" ADD CONSTRAINT "TeamCollection_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;---- AddForeignKey-ALTER TABLE "TeamRequest" ADD CONSTRAINT "TeamRequest_collectionID_fkey" FOREIGN KEY ("collectionID") REFERENCES "TeamCollection"("id") ON DELETE CASCADE ON UPDATE CASCADE;---- AddForeignKey-ALTER TABLE "TeamRequest" ADD CONSTRAINT "TeamRequest_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;---- AddForeignKey-ALTER TABLE "TeamEnvironment" ADD CONSTRAINT "TeamEnvironment_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;---- AddForeignKey-ALTER TABLE "Account" ADD CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;---- AddForeignKey-ALTER TABLE "PasswordlessVerification" ADD CONSTRAINT "PasswordlessVerification_userUid_fkey" FOREIGN KEY ("userUid") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;diff --git a/packages/hoppscotch-backend/prisma/migrations/20230118233921_timestamp_fix/migration.sql b/packages/hoppscotch-backend/prisma/migrations/20230118233921_timestamp_fix/migration.sqldeleted file mode 100644index e30bff27c..000000000--- a/packages/hoppscotch-backend/prisma/migrations/20230118233921_timestamp_fix/migration.sql+++ /dev/null@@ -1,8 +0,0 @@--- AlterTable-ALTER TABLE "Account" ALTER COLUMN "loggedIn" SET DATA TYPE TIMESTAMP(3);---- AlterTable-ALTER TABLE "PasswordlessVerification" ALTER COLUMN "expiresOn" SET DATA TYPE TIMESTAMP(3);---- AlterTable-ALTER TABLE "User" ALTER COLUMN "createdOn" SET DATA TYPE TIMESTAMP(3);diff --git a/packages/hoppscotch-backend/prisma/migrations/migration_lock.toml b/packages/hoppscotch-backend/prisma/migrations/migration_lock.tomldeleted file mode 100644index fbffa92c2..000000000--- a/packages/hoppscotch-backend/prisma/migrations/migration_lock.toml+++ /dev/null@@ -1,3 +0,0 @@-# Please do not edit this file manually-# It should be added in your version-control system (i.e. Git)-provider = "postgresql"\ No newline at end of filediff --git a/packages/hoppscotch-backend/src/auth/auth.service.ts b/packages/hoppscotch-backend/src/auth/auth.service.tsindex f2254b104..f1bb3ccc9 100644--- a/packages/hoppscotch-backend/src/auth/auth.service.ts+++ b/packages/hoppscotch-backend/src/auth/auth.service.ts@@ -48,7 +48,7 @@ export class AuthService {const idToken = await this.prismaService.passwordlessVerification.create({data: {deviceIdentifier: salt,- userUid: user.id,+ userUid: user.uid,expiresOn: expiresOn,},});@@ -77,7 +77,7 @@ export class AuthService {try {const user = await this.prismaService.user.update({where: {- id: userUid,+ uid: userUid,},data: {refreshToken: tokenHash,@@ -290,7 +290,7 @@ export class AuthService {statusCode: HttpStatus.NOT_FOUND,});- const generatedAuthTokens = await this.generateAuthTokens(user.id);+ const generatedAuthTokens = await this.generateAuthTokens(user.uid);if (E.isLeft(generatedAuthTokens))return E.left({message: generatedAuthTokens.left.message,diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.tsindex 1f42d79df..92cea1849 100644--- a/packages/hoppscotch-backend/src/user/user.service.ts+++ b/packages/hoppscotch-backend/src/user/user.service.ts@@ -24,7 +24,7 @@ export class UserService {try {const user = await this.prisma.user.findUniqueOrThrow({where: {- id: userUid,+ uid: userUid,},});return O.some(user);@@ -52,9 +52,9 @@ export class UserService {async createUserSSO(accessToken: string, refreshToken: string, profile) {const createdUser = await this.prisma.user.create({data: {- name: !profile.displayName ? null : profile.displayName,+ displayName: !profile.displayName ? null : profile.displayName,email: profile.emails[0].value,- image: !profile.photos ? null : profile.photos[0].value,+ photoURL: !profile.photos ? null : profile.photos[0].value,accounts: {create: {provider: profile.provider,@@ -83,7 +83,7 @@ export class UserService {providerAccessToken: accessToken ? accessToken : null,user: {connect: {- id: user.id,+ uid: user.uid,},},},diff --git a/packages/hoppscotch-backend/src/utils.ts b/packages/hoppscotch-backend/src/utils.tsindex 93ee6d44b..e1d2aba51 100644--- a/packages/hoppscotch-backend/src/utils.ts+++ b/packages/hoppscotch-backend/src/utils.ts@@ -10,6 +10,7 @@ import * as E from 'fp-ts/Either';import { AuthErrorHandler } from './types/AuthErrorHandler';import { AuthTokens } from './types/AuthTokens';import { Response } from 'express';+import { DateTime } from 'luxon';/*** A workaround to throw an exception in an expression.@@ -134,7 +135,6 @@ export const validateEmail = (email: string) => {).test(email);};-//TODO: set expiresOn to cookies/*** Sets and returns the cookies in the response object on successful authentication* @param res Express Response Object@@ -146,15 +146,30 @@ export const authCookieHandler = (authTokens: AuthTokens,redirect: boolean,) => {+ const currentTime = DateTime.now();+ const accessTokenValidity = currentTime.plus({+ millisecond: parseInt(process.env.ACCESS_TOKEN_VALIDITY),+ });+ const refreshTokenValidity = currentTime.plus({+ millisecond: parseInt(process.env.REFRESH_TOKEN_VALIDITY),+ });+ console.log(process.env.ACCESS_TOKEN_VALIDITY, accessTokenValidity);+ console.log(process.env.REFRESH_TOKEN_VALIDITY, refreshTokenValidity);+ console.log(process.env.REDIRECT_URL);+res.cookie('access_token', authTokens.access_token, {httpOnly: true,secure: true,sameSite: 'lax',+ maxAge: accessTokenValidity.toMillis(),+ expires: accessTokenValidity.toJSDate(),});res.cookie('refresh_token', authTokens.refresh_token, {httpOnly: true,secure: true,sameSite: 'lax',+ maxAge: refreshTokenValidity.toMillis(),+ expires: refreshTokenValidity.toJSDate(),});if (redirect) {res.status(HttpStatus.OK).redirect('http://localhost:3170/graphql');