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:
parent
97c3e6089d
commit
c49573db65
2 changed files with 25 additions and 11 deletions
|
|
@ -26,8 +26,8 @@ export class UserResolver {
|
||||||
"Gives details of the user executing this query (pass Authorization 'Bearer' header)",
|
"Gives details of the user executing this query (pass Authorization 'Bearer' header)",
|
||||||
})
|
})
|
||||||
@UseGuards(GqlAuthGuard)
|
@UseGuards(GqlAuthGuard)
|
||||||
me(@GqlUser() user) {
|
me(@GqlUser() user: AuthUser) {
|
||||||
return user;
|
return this.userService.convertDbUserToUser(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Mutations */
|
/* Mutations */
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ import { USER_UPDATE_FAILED } from 'src/errors';
|
||||||
import { PubSubService } from 'src/pubsub/pubsub.service';
|
import { PubSubService } from 'src/pubsub/pubsub.service';
|
||||||
import { stringToJson, taskEitherValidateArraySeq } from 'src/utils';
|
import { stringToJson, taskEitherValidateArraySeq } from 'src/utils';
|
||||||
import { UserDataHandler } from './user.data.handler';
|
import { UserDataHandler } from './user.data.handler';
|
||||||
|
import { User as DbUser } from '@prisma/client';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserService {
|
export class UserService {
|
||||||
|
|
@ -28,6 +29,27 @@ export class UserService {
|
||||||
this.userDataHandlers.push(handler);
|
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
|
* Find User with given email id
|
||||||
*
|
*
|
||||||
|
|
@ -236,15 +258,7 @@ export class UserService {
|
||||||
data: sessionObj,
|
data: sessionObj,
|
||||||
});
|
});
|
||||||
|
|
||||||
const updatedUser: User = {
|
const updatedUser = this.convertDbUserToUser(dbUpdatedUser);
|
||||||
...dbUpdatedUser,
|
|
||||||
currentGQLSession: dbUpdatedUser.currentGQLSession
|
|
||||||
? JSON.stringify(dbUpdatedUser.currentGQLSession)
|
|
||||||
: null,
|
|
||||||
currentRESTSession: dbUpdatedUser.currentRESTSession
|
|
||||||
? JSON.stringify(dbUpdatedUser.currentRESTSession)
|
|
||||||
: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Publish subscription for user updates
|
// Publish subscription for user updates
|
||||||
await this.pubsub.publish(`user/${updatedUser.uid}/updated`, updatedUser);
|
await this.pubsub.publish(`user/${updatedUser.uid}/updated`, updatedUser);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue