update handle session
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../Prisma/prisma.service';
|
||||
import { clerkClient } from '@clerk/express';
|
||||
@Injectable()
|
||||
export class ClerkService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
@@ -91,8 +92,26 @@ export class ClerkService {
|
||||
});
|
||||
}
|
||||
|
||||
eventSessionCreated(data: any) {
|
||||
async eventSessionCreated(data: any) {
|
||||
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) {
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
import { Builder } from '../Graphql/graphql.builder';
|
||||
import { PrismaService } from '../Prisma/prisma.service';
|
||||
import { clerkClient } from '@clerk/express';
|
||||
import type { Session } from '@clerk/express';
|
||||
import { UnauthorizedException } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
@@ -48,6 +47,10 @@ export class UserSchema extends PothosSchema {
|
||||
nullable: true,
|
||||
description: 'The role of the user.',
|
||||
}),
|
||||
avatarUrl: t.exposeString('avatarUrl', {
|
||||
nullable: true,
|
||||
description: 'The avatar URL of the user.',
|
||||
}),
|
||||
createdAt: t.expose('createdAt', {
|
||||
type: 'DateTime',
|
||||
nullable: true,
|
||||
@@ -160,7 +163,7 @@ export class UserSchema extends PothosSchema {
|
||||
type: this.user(),
|
||||
args: this.builder.generator.findUniqueArgs('User'),
|
||||
resolve: async (query, root, args, ctx, info) => {
|
||||
return await this.prisma.user.findUnique({
|
||||
return await this.prisma.user.findUniqueOrThrow({
|
||||
...query,
|
||||
where: args.where,
|
||||
});
|
||||
@@ -176,7 +179,7 @@ export class UserSchema extends PothosSchema {
|
||||
// check if the token is valid
|
||||
const session = await clerkClient.sessions.getSession(args.sessionId);
|
||||
Logger.log(session, 'Session');
|
||||
return await this.prisma.user.findFirst({
|
||||
return await this.prisma.user.findFirstOrThrow({
|
||||
...query,
|
||||
where: {
|
||||
id: session.userId,
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user