fix: serialize user sessions properly (#62)

* fix: cast prisma user object to user object

* chore: improved consistency

* chore: cast function renamed

---------

Co-authored-by: Mir Arif Hasan <arif.ishan05@gmail.com>
This commit is contained in:
Akash K
2023-04-03 09:47:40 +05:30
committed by GitHub
parent 97c3e6089d
commit c49573db65
2 changed files with 25 additions and 11 deletions

View File

@@ -26,8 +26,8 @@ export class UserResolver {
"Gives details of the user executing this query (pass Authorization 'Bearer' header)",
})
@UseGuards(GqlAuthGuard)
me(@GqlUser() user) {
return user;
me(@GqlUser() user: AuthUser) {
return this.userService.convertDbUserToUser(user);
}
/* Mutations */

View File

@@ -14,6 +14,7 @@ import { USER_UPDATE_FAILED } from 'src/errors';
import { PubSubService } from 'src/pubsub/pubsub.service';
import { stringToJson, taskEitherValidateArraySeq } from 'src/utils';
import { UserDataHandler } from './user.data.handler';
import { User as DbUser } from '@prisma/client';
@Injectable()
export class UserService {
@@ -28,6 +29,27 @@ export class UserService {
this.userDataHandlers.push(handler);
}
/**
* Converts a prisma user object to a user object
*
* @param dbUser Prisma User object
* @returns User object
*/
convertDbUserToUser(dbUser: DbUser): User {
const dbCurrentRESTSession = dbUser.currentRESTSession;
const dbCurrentGQLSession = dbUser.currentGQLSession;
return {
...dbUser,
currentRESTSession: dbCurrentRESTSession
? JSON.stringify(dbCurrentRESTSession)
: null,
currentGQLSession: dbCurrentGQLSession
? JSON.stringify(dbCurrentGQLSession)
: null,
};
}
/**
* Find User with given email id
*
@@ -236,15 +258,7 @@ export class UserService {
data: sessionObj,
});
const updatedUser: User = {
...dbUpdatedUser,
currentGQLSession: dbUpdatedUser.currentGQLSession
? JSON.stringify(dbUpdatedUser.currentGQLSession)
: null,
currentRESTSession: dbUpdatedUser.currentRESTSession
? JSON.stringify(dbUpdatedUser.currentRESTSession)
: null,
};
const updatedUser = this.convertDbUserToUser(dbUpdatedUser);
// Publish subscription for user updates
await this.pubsub.publish(`user/${updatedUser.uid}/updated`, updatedUser);