@@ -13,7 +13,7 @@ import {
13
13
} from '@nestjs/common' ;
14
14
import { AuthzService } from './authz.service' ;
15
15
import { CommonService } from '../../../../libs/common/src/common.service' ;
16
- import { ApiBody , ApiOperation , ApiResponse , ApiTags } from '@nestjs/swagger' ;
16
+ import { ApiBody , ApiOperation , ApiQuery , ApiResponse , ApiTags } from '@nestjs/swagger' ;
17
17
import { ApiResponseDto } from '../dtos/apiResponse.dto' ;
18
18
import { UserEmailVerificationDto } from '../user/dto/create-user.dto' ;
19
19
import IResponseType from '@credebl/common/interfaces/response.interface' ;
@@ -28,27 +28,55 @@ import { ResetPasswordDto } from './dtos/reset-password.dto';
28
28
import { ForgotPasswordDto } from './dtos/forgot-password.dto' ;
29
29
import { ResetTokenPasswordDto } from './dtos/reset-token-password' ;
30
30
import { RefreshTokenDto } from './dtos/refresh-token.dto' ;
31
-
31
+ import { getDefaultClient } from '../user/utils' ;
32
+ import { ClientAliasValidationPipe } from './decorators/user-auth-client' ;
32
33
33
34
@Controller ( 'auth' )
34
35
@ApiTags ( 'auth' )
35
36
@UseFilters ( CustomExceptionFilter )
36
37
export class AuthzController {
37
38
private logger = new Logger ( 'AuthzController' ) ;
38
39
39
- constructor ( private readonly authzService : AuthzService ,
40
- private readonly commonService : CommonService ) { }
40
+ constructor (
41
+ private readonly authzService : AuthzService ,
42
+ private readonly commonService : CommonService
43
+ ) { }
44
+
45
+ /**
46
+ * Fetch client aliase.
47
+ *
48
+ * @returns Returns client alias and its url.
49
+ */
50
+ @Get ( '/clientAliases' )
51
+ @ApiResponse ( { status : HttpStatus . OK , description : 'Success' , type : ApiResponseDto } )
52
+ @ApiOperation ( {
53
+ summary : 'Get client aliases' ,
54
+ description : 'Fetch client aliases and itr url'
55
+ } )
56
+ async getClientAlias ( @Res ( ) res : Response ) : Promise < Response > {
57
+ const clientAliases = await this . authzService . getClientAlias ( ) ;
58
+ const finalResponse : IResponseType = {
59
+ statusCode : HttpStatus . OK ,
60
+ message : ResponseMessages . user . success . fetchClientAliases ,
61
+ data : clientAliases
62
+ } ;
63
+
64
+ return res . status ( HttpStatus . OK ) . json ( finalResponse ) ;
65
+ }
41
66
42
67
/**
43
68
* Verify user’s email address.
44
- *
69
+ *
45
70
* @param email The email address of the user.
46
71
* @param verificationcode The verification code sent to the user's email.
47
- * @returns Returns the email verification status.
72
+ * @returns Returns the email verification status.
48
73
*/
49
74
@Get ( '/verify' )
50
75
@ApiResponse ( { status : HttpStatus . OK , description : 'Success' , type : ApiResponseDto } )
51
- @ApiOperation ( { summary : 'Verify user’s email' , description : 'Checks if the provided verification code is valid for the given email.' } )
76
+ @ApiOperation ( {
77
+ summary : 'Verify user’s email' ,
78
+ description : 'Checks if the provided verification code is valid for the given email.'
79
+ } )
52
80
async verifyEmail ( @Query ( ) query : EmailVerificationDto , @Res ( ) res : Response ) : Promise < Response > {
53
81
await this . authzService . verifyEmail ( query ) ;
54
82
const finalResponse : IResponseType = {
@@ -60,15 +88,28 @@ export class AuthzController {
60
88
}
61
89
62
90
/**
63
- * Sends a verification email to the user.
64
- *
65
- * @body UserEmailVerificationDto.
66
- * @returns The status of the verification email.
67
- */
91
+ * Sends a verification email to the user.
92
+ *
93
+ * @body UserEmailVerificationDto.
94
+ * @returns The status of the verification email.
95
+ */
68
96
@Post ( '/verification-mail' )
69
97
@ApiResponse ( { status : HttpStatus . CREATED , description : 'Created' , type : ApiResponseDto } )
98
+ @ApiQuery ( {
99
+ name : 'clientAlias' ,
100
+ required : false ,
101
+ enum : ( process . env . SUPPORTED_SSO_CLIENTS || '' )
102
+ . split ( ',' )
103
+ . map ( ( alias ) => alias . trim ( ) ?. toUpperCase ( ) )
104
+ . filter ( Boolean )
105
+ } )
70
106
@ApiOperation ( { summary : 'Send verification email' , description : 'Send verification email to new user' } )
71
- async create ( @Body ( ) userEmailVerification : UserEmailVerificationDto , @Res ( ) res : Response ) : Promise < Response > {
107
+ async create (
108
+ @Query ( 'clientAlias' , ClientAliasValidationPipe ) clientAlias : string ,
109
+ @Body ( ) userEmailVerification : UserEmailVerificationDto ,
110
+ @Res ( ) res : Response
111
+ ) : Promise < Response > {
112
+ userEmailVerification . clientAlias = clientAlias ?? ( await getDefaultClient ( ) ) . alias ;
72
113
await this . authzService . sendVerificationMail ( userEmailVerification ) ;
73
114
const finalResponse : IResponseType = {
74
115
statusCode : HttpStatus . CREATED ,
@@ -78,30 +119,32 @@ export class AuthzController {
78
119
}
79
120
80
121
/**
81
- * Registers a new user on the platform.
82
- *
83
- * @body AddUserDetailsDto
84
- * @returns User's registration status and user details
85
- */
122
+ * Registers a new user on the platform.
123
+ *
124
+ * @body AddUserDetailsDto
125
+ * @returns User's registration status and user details
126
+ */
86
127
@Post ( '/signup' )
87
128
@ApiResponse ( { status : HttpStatus . CREATED , description : 'Created' , type : ApiResponseDto } )
88
- @ApiOperation ( { summary : 'Register new user to platform' , description : 'Register new user to platform with the provided details.' } )
129
+ @ApiOperation ( {
130
+ summary : 'Register new user to platform' ,
131
+ description : 'Register new user to platform with the provided details.'
132
+ } )
89
133
async addUserDetails ( @Body ( ) userInfo : AddUserDetailsDto , @Res ( ) res : Response ) : Promise < Response > {
90
134
const userData = await this . authzService . addUserDetails ( userInfo ) ;
91
- const finalResponse = {
92
- statusCode : HttpStatus . CREATED ,
93
- message : ResponseMessages . user . success . create ,
94
- data : userData
95
- } ;
135
+ const finalResponse = {
136
+ statusCode : HttpStatus . CREATED ,
137
+ message : ResponseMessages . user . success . create ,
138
+ data : userData
139
+ } ;
96
140
return res . status ( HttpStatus . CREATED ) . json ( finalResponse ) ;
97
-
98
141
}
99
142
/**
100
- * Authenticates a user and returns an access token.
101
- *
102
- * @body LoginUserDto
103
- * @returns User's access token details
104
- */
143
+ * Authenticates a user and returns an access token.
144
+ *
145
+ * @body LoginUserDto
146
+ * @returns User's access token details
147
+ */
105
148
@Post ( '/signin' )
106
149
@ApiOperation ( {
107
150
summary : 'Authenticate the user for the access' ,
@@ -110,7 +153,6 @@ export class AuthzController {
110
153
@ApiResponse ( { status : HttpStatus . OK , description : 'Success' , type : AuthTokenResponse } )
111
154
@ApiBody ( { type : LoginUserDto } )
112
155
async login ( @Body ( ) loginUserDto : LoginUserDto , @Res ( ) res : Response ) : Promise < Response > {
113
-
114
156
if ( loginUserDto . email ) {
115
157
const userData = await this . authzService . login ( loginUserDto . email , loginUserDto . password ) ;
116
158
@@ -126,60 +168,58 @@ export class AuthzController {
126
168
}
127
169
}
128
170
129
-
130
171
/**
131
- * Resets user's password.
132
- *
133
- * @body ResetPasswordDto
134
- * @returns The password reset status.
135
- */
172
+ * Resets user's password.
173
+ *
174
+ * @body ResetPasswordDto
175
+ * @returns The password reset status.
176
+ */
136
177
@Post ( '/reset-password' )
137
178
@ApiOperation ( {
138
179
summary : 'Reset password' ,
139
180
description : 'Allows users to reset a new password which should be different form existing password.'
140
- } )
181
+ } )
141
182
@ApiResponse ( { status : HttpStatus . OK , description : 'Success' , type : ApiResponseDto } )
142
183
async resetPassword ( @Body ( ) resetPasswordDto : ResetPasswordDto , @Res ( ) res : Response ) : Promise < Response > {
143
-
144
- const userData = await this . authzService . resetPassword ( resetPasswordDto ) ;
145
- const finalResponse : IResponseType = {
146
- statusCode : HttpStatus . OK ,
147
- message : ResponseMessages . user . success . resetPassword ,
148
- data : userData
149
- } ;
150
- return res . status ( HttpStatus . OK ) . json ( finalResponse ) ;
184
+ const userData = await this . authzService . resetPassword ( resetPasswordDto ) ;
185
+ const finalResponse : IResponseType = {
186
+ statusCode : HttpStatus . OK ,
187
+ message : ResponseMessages . user . success . resetPassword ,
188
+ data : userData
189
+ } ;
190
+ return res . status ( HttpStatus . OK ) . json ( finalResponse ) ;
151
191
}
152
192
153
- /**
154
- * Initiates the password reset process by sending a reset link to the user's email.
155
- *
156
- * @body ForgotPasswordDto
157
- * @returns Status message indicating whether the reset link was sent successfully.
158
- */
193
+ /**
194
+ * Initiates the password reset process by sending a reset link to the user's email.
195
+ *
196
+ * @body ForgotPasswordDto
197
+ * @returns Status message indicating whether the reset link was sent successfully.
198
+ */
159
199
@Post ( '/forgot-password' )
160
200
@ApiOperation ( {
161
201
summary : 'Forgot password' ,
162
202
description : 'Sends a password reset link to the user’s email.'
163
203
} )
164
204
@ApiResponse ( { status : HttpStatus . OK , description : 'Success' , type : ApiResponseDto } )
165
205
async forgotPassword ( @Body ( ) forgotPasswordDto : ForgotPasswordDto , @Res ( ) res : Response ) : Promise < Response > {
166
- const userData = await this . authzService . forgotPassword ( forgotPasswordDto ) ;
167
- const finalResponse : IResponseType = {
168
- statusCode : HttpStatus . OK ,
169
- message : ResponseMessages . user . success . resetPasswordLink ,
170
- data : userData
171
- } ;
206
+ const userData = await this . authzService . forgotPassword ( forgotPasswordDto ) ;
207
+ const finalResponse : IResponseType = {
208
+ statusCode : HttpStatus . OK ,
209
+ message : ResponseMessages . user . success . resetPasswordLink ,
210
+ data : userData
211
+ } ;
172
212
173
- return res . status ( HttpStatus . OK ) . json ( finalResponse ) ;
213
+ return res . status ( HttpStatus . OK ) . json ( finalResponse ) ;
174
214
}
175
215
176
- /**
177
- * Resets the user's password using a verification token.
178
- *
179
- * @param email The email address of the user.
180
- * @body ResetTokenPasswordDto
181
- * @returns Status message indicating whether the password reset was successful.
182
- */
216
+ /**
217
+ * Resets the user's password using a verification token.
218
+ *
219
+ * @param email The email address of the user.
220
+ * @body ResetTokenPasswordDto
221
+ * @returns Status message indicating whether the password reset was successful.
222
+ */
183
223
@Post ( '/password-reset/:email' )
184
224
@ApiOperation ( {
185
225
summary : 'Reset password with verification token' ,
@@ -189,41 +229,38 @@ export class AuthzController {
189
229
async resetNewPassword (
190
230
@Param ( 'email' ) email : string ,
191
231
@Body ( ) resetTokenPasswordDto : ResetTokenPasswordDto ,
192
- @Res ( ) res : Response ) : Promise < Response > {
193
- resetTokenPasswordDto . email = email . trim ( ) ;
194
- const userData = await this . authzService . resetNewPassword ( resetTokenPasswordDto ) ;
195
- const finalResponse : IResponseType = {
196
- statusCode : HttpStatus . OK ,
197
- message : ResponseMessages . user . success . resetPassword ,
198
- data : userData
199
- } ;
200
- return res . status ( HttpStatus . OK ) . json ( finalResponse ) ;
232
+ @Res ( ) res : Response
233
+ ) : Promise < Response > {
234
+ resetTokenPasswordDto . email = email . trim ( ) ;
235
+ const userData = await this . authzService . resetNewPassword ( resetTokenPasswordDto ) ;
236
+ const finalResponse : IResponseType = {
237
+ statusCode : HttpStatus . OK ,
238
+ message : ResponseMessages . user . success . resetPassword ,
239
+ data : userData
240
+ } ;
241
+ return res . status ( HttpStatus . OK ) . json ( finalResponse ) ;
201
242
}
202
243
203
- /**
204
- * Generates a new access token using a refresh token.
205
- *
206
- * @body RefreshTokenDto
207
- * @returns New access token and its details.
208
- */
244
+ /**
245
+ * Generates a new access token using a refresh token.
246
+ *
247
+ * @body RefreshTokenDto
248
+ * @returns New access token and its details.
249
+ */
209
250
@Post ( '/refresh-token' )
210
251
@ApiOperation ( {
211
252
summary : 'Token from refresh token' ,
212
253
description : 'Generates a new access token using a refresh token.'
213
254
} )
214
255
@ApiResponse ( { status : HttpStatus . OK , description : 'Success' , type : ApiResponseDto } )
215
- async refreshToken (
216
- @Body ( ) refreshTokenDto : RefreshTokenDto ,
217
- @Res ( ) res : Response ) : Promise < Response > {
218
- const tokenData = await this . authzService . refreshToken ( refreshTokenDto . refreshToken ) ;
219
- const finalResponse : IResponseType = {
220
- statusCode : HttpStatus . OK ,
221
- message : ResponseMessages . user . success . refreshToken ,
222
- data : tokenData
223
- } ;
256
+ async refreshToken ( @Body ( ) refreshTokenDto : RefreshTokenDto , @Res ( ) res : Response ) : Promise < Response > {
257
+ const tokenData = await this . authzService . refreshToken ( refreshTokenDto . refreshToken ) ;
258
+ const finalResponse : IResponseType = {
259
+ statusCode : HttpStatus . OK ,
260
+ message : ResponseMessages . user . success . refreshToken ,
261
+ data : tokenData
262
+ } ;
224
263
225
- return res . status ( HttpStatus . OK ) . json ( finalResponse ) ;
226
-
264
+ return res . status ( HttpStatus . OK ) . json ( finalResponse ) ;
227
265
}
228
-
229
- }
266
+ }
0 commit comments