1- import { BadRequestException , Injectable , Logger } from '@nestjs/common' ;
2- import { MailerService } from '@nestjs-modules/mailer' ;
1+ import { Injectable } from '@nestjs/common' ;
32import { Prisma } from '@prisma/client' ;
43import { GetUsersDto , PublicUser , User } from '@tegonhq/types' ;
54import { PrismaService } from 'nestjs-prisma' ;
6- import supertokens from 'supertokens-node' ;
7- import { SessionContainer } from 'supertokens-node/recipe/session' ;
85
96import {
107 generateKeyForUserId ,
118 generatePersonalAccessToken ,
129} from 'common/authentication' ;
1310
14- import { SupertokensService } from 'modules/auth/supertokens/supertokens.service' ;
15-
1611import {
1712 UpdateUserBody ,
1813 userSerializer ,
@@ -21,11 +16,7 @@ import {
2116
2217@Injectable ( )
2318export class UsersService {
24- private readonly logger : Logger = new Logger ( 'UserService' ) ;
25- constructor (
26- private prisma : PrismaService ,
27- private mailerService : MailerService ,
28- ) { }
19+ constructor ( private prisma : PrismaService ) { }
2920
3021 async upsertUser (
3122 id : string ,
@@ -127,125 +118,6 @@ export class UsersService {
127118 return userSerializer ( user ) ;
128119 }
129120
130- async changePassword (
131- supertokensService : SupertokensService ,
132- userId : string ,
133- session : SessionContainer ,
134- passwordBody : Record < string , string > ,
135- ) {
136- const oldPassword = passwordBody . oldPassword ;
137- const newPassword = passwordBody . newPassword ;
138-
139- const userInfo = await supertokens . getUser ( userId ) ;
140-
141- if ( userInfo === undefined ) {
142- throw new BadRequestException ( 'User not found' ) ;
143- }
144-
145- const loginMethod = userInfo . loginMethods . find (
146- // eslint-disable-next-line @typescript-eslint/no-explicit-any
147- ( lM : any ) =>
148- lM . recipeUserId . getAsString ( ) ===
149- session . getRecipeUserId ( ) . getAsString ( ) &&
150- lM . recipeId === 'emailpassword' ,
151- ) ;
152-
153- if ( loginMethod === undefined ) {
154- throw new BadRequestException ( `You've signed up with different method` ) ;
155- }
156- const email = loginMethod . email ;
157-
158- const EmailPassword = supertokensService . getEmailPasswordRecipe ( ) ;
159-
160- const isPasswordValid = await EmailPassword . signIn (
161- session ! . getTenantId ( ) ,
162- email ,
163- oldPassword ,
164- ) ;
165-
166- if ( isPasswordValid . status !== 'OK' ) {
167- throw new BadRequestException ( `Your current password didn't match` ) ;
168- }
169-
170- // update the user's password using updateEmailOrPassword
171- const response = await EmailPassword . updateEmailOrPassword ( {
172- recipeUserId : session . getRecipeUserId ( ) ,
173- password : newPassword ,
174- tenantIdForPasswordPolicy : session . getTenantId ( ) ,
175- } ) ;
176-
177- if ( response . status === 'PASSWORD_POLICY_VIOLATED_ERROR' ) {
178- // TODO: handle incorrect password error
179- throw new BadRequestException (
180- `Your new password didn't match with the policy` ,
181- ) ;
182- }
183-
184- return { message : 'Successful' } ;
185- }
186-
187- async sendPasswordResetEmail (
188- supertokensService : SupertokensService ,
189- email : string ,
190- ) {
191- const EmailPassword = supertokensService . getEmailPasswordRecipe ( ) ;
192-
193- const user = await this . prisma . user . findUnique ( {
194- where : { email } ,
195- } ) ;
196-
197- if ( ! user ) {
198- throw new BadRequestException ( 'User not found' ) ;
199- }
200-
201- const response = await EmailPassword . createResetPasswordLink (
202- undefined ,
203- user . id ,
204- email ,
205- ) ;
206-
207- if ( response . status === 'OK' ) {
208- await this . mailerService . sendMail ( {
209- to : user . email ,
210- subject : 'Password Reset' ,
211- template : 'resetPassword' ,
212- context : {
213- username : user . fullname ,
214- resetUrl : response . link ,
215- } ,
216- } ) ;
217- this . logger . log ( 'Reset Email sent to user' ) ;
218- }
219-
220- return response ;
221- }
222-
223- async resetPassword (
224- supertokensService : SupertokensService ,
225- token : string ,
226- newPassword : string ,
227- ) {
228- const EmailPassword = supertokensService . getEmailPasswordRecipe ( ) ;
229-
230- const response = await EmailPassword . resetPasswordUsingToken (
231- undefined ,
232- token ,
233- newPassword ,
234- ) ;
235-
236- if ( response . status === 'PASSWORD_POLICY_VIOLATED_ERROR' ) {
237- throw new BadRequestException (
238- `Your new password didn't match with the policy` ,
239- ) ;
240- } else if ( response . status === 'RESET_PASSWORD_INVALID_TOKEN_ERROR' ) {
241- throw new BadRequestException ( `Invalid reset password token` ) ;
242- } else if ( response . status === 'OK' ) {
243- return { message : 'Successful' } ;
244- }
245-
246- throw new BadRequestException ( response . status ) ;
247- }
248-
249121 async getInvitesForUser ( email : string ) {
250122 const invites = await this . prisma . invite . findMany ( {
251123 where : { emailId : email , deleted : null } ,
0 commit comments