import { Injectable } from '@nestjs/common' ; import { Repository } from 'typeorm' ; import { InjectRepository } from '@nestjs/typeorm' ; import { User } from '../../user.entity' ; import { SecurityService } from '../../../common/security/security.service' ; @ Injectable ( ) export class AnonymousService { constructor ( @ InjectRepository ( User ) private readonly userRepository , private readonly securityService ) { } async createAnonymousUserAndSession ( ) { try { const user = await this . addAnonymousUserToDatabase ( ) ; const sessionToken = await this . securityService . createSessionToken ( { roles : user . roles , id : user . id . toString ( ) , loginProvider : 'anonymous' , } ) ; const csrfToken = await this . securityService . createCsrfToken ( ) ; const result = { user , sessionToken , csrfToken } ; return result ; } catch ( err ) { return err ; } } private async addAnonymousUserToDatabase ( ) { const user = new User ( ) ; user . isAnonymous = true ; user . roles = [ 'anon' ] ; return await this . userRepository . save ( user ) ; } }