update handle session
This commit is contained in:
Submodule epess-database updated: 8c3fa0fbbc...b07eee4fd5
@@ -1,5 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { PrismaService } from '../Prisma/prisma.service';
|
import { PrismaService } from '../Prisma/prisma.service';
|
||||||
|
import { clerkClient } from '@clerk/express';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ClerkService {
|
export class ClerkService {
|
||||||
constructor(private readonly prisma: PrismaService) {}
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
@@ -91,8 +92,26 @@ export class ClerkService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
eventSessionCreated(data: any) {
|
async eventSessionCreated(data: any) {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
|
// check if user exists in database or create user
|
||||||
|
const user = await this.prisma.user.findUnique({
|
||||||
|
where: { id: data.user_id },
|
||||||
|
});
|
||||||
|
if (!user) {
|
||||||
|
// get user info from clerk
|
||||||
|
const userInfo = await clerkClient.users.getUser(data.user_id);
|
||||||
|
console.log(userInfo);
|
||||||
|
await this.prisma.user.create({
|
||||||
|
data: {
|
||||||
|
id: data.user_id,
|
||||||
|
email: userInfo.emailAddresses[0].emailAddress,
|
||||||
|
name: `${userInfo.firstName} ${userInfo.lastName}`,
|
||||||
|
avatarUrl: userInfo.imageUrl,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// to do: get session info
|
||||||
}
|
}
|
||||||
|
|
||||||
eventSessionRevoked(data: any) {
|
eventSessionRevoked(data: any) {
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import {
|
|||||||
import { Builder } from '../Graphql/graphql.builder';
|
import { Builder } from '../Graphql/graphql.builder';
|
||||||
import { PrismaService } from '../Prisma/prisma.service';
|
import { PrismaService } from '../Prisma/prisma.service';
|
||||||
import { clerkClient } from '@clerk/express';
|
import { clerkClient } from '@clerk/express';
|
||||||
import type { Session } from '@clerk/express';
|
|
||||||
import { UnauthorizedException } from '@nestjs/common';
|
import { UnauthorizedException } from '@nestjs/common';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -48,6 +47,10 @@ export class UserSchema extends PothosSchema {
|
|||||||
nullable: true,
|
nullable: true,
|
||||||
description: 'The role of the user.',
|
description: 'The role of the user.',
|
||||||
}),
|
}),
|
||||||
|
avatarUrl: t.exposeString('avatarUrl', {
|
||||||
|
nullable: true,
|
||||||
|
description: 'The avatar URL of the user.',
|
||||||
|
}),
|
||||||
createdAt: t.expose('createdAt', {
|
createdAt: t.expose('createdAt', {
|
||||||
type: 'DateTime',
|
type: 'DateTime',
|
||||||
nullable: true,
|
nullable: true,
|
||||||
@@ -160,7 +163,7 @@ export class UserSchema extends PothosSchema {
|
|||||||
type: this.user(),
|
type: this.user(),
|
||||||
args: this.builder.generator.findUniqueArgs('User'),
|
args: this.builder.generator.findUniqueArgs('User'),
|
||||||
resolve: async (query, root, args, ctx, info) => {
|
resolve: async (query, root, args, ctx, info) => {
|
||||||
return await this.prisma.user.findUnique({
|
return await this.prisma.user.findUniqueOrThrow({
|
||||||
...query,
|
...query,
|
||||||
where: args.where,
|
where: args.where,
|
||||||
});
|
});
|
||||||
@@ -176,7 +179,7 @@ export class UserSchema extends PothosSchema {
|
|||||||
// check if the token is valid
|
// check if the token is valid
|
||||||
const session = await clerkClient.sessions.getSession(args.sessionId);
|
const session = await clerkClient.sessions.getSession(args.sessionId);
|
||||||
Logger.log(session, 'Session');
|
Logger.log(session, 'Session');
|
||||||
return await this.prisma.user.findFirst({
|
return await this.prisma.user.findFirstOrThrow({
|
||||||
...query,
|
...query,
|
||||||
where: {
|
where: {
|
||||||
id: session.userId,
|
id: session.userId,
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user