@@ -4,7 +4,7 @@ import { AuthStatus, UploadProfilePictureResponse } from "@/types/user";
4
4
import Cookie from "js-cookie" ;
5
5
import Swal from "sweetalert2" ;
6
6
7
- const toast = Swal . mixin ( {
7
+ export const ToastComponent = Swal . mixin ( {
8
8
toast : true ,
9
9
position : "top-end" ,
10
10
showConfirmButton : false ,
@@ -88,7 +88,7 @@ export const verifyToken = async (token: string) => {
88
88
} ;
89
89
90
90
export const login = async ( email : string , password : string ) => {
91
- toast . fire ( {
91
+ ToastComponent . fire ( {
92
92
icon : "info" ,
93
93
title : "Logging in..." ,
94
94
} ) ;
@@ -105,7 +105,7 @@ export const login = async (email: string, password: string) => {
105
105
const data = await response . json ( ) ;
106
106
107
107
if ( response . status !== 200 ) {
108
- toast . fire ( {
108
+ ToastComponent . fire ( {
109
109
icon : "error" ,
110
110
title : data . message ,
111
111
} ) ;
@@ -134,7 +134,7 @@ export const register = async (
134
134
password : string ,
135
135
username : string
136
136
) => {
137
- toast . fire ( {
137
+ ToastComponent . fire ( {
138
138
icon : "info" ,
139
139
title : "Registering..." ,
140
140
} ) ;
@@ -152,7 +152,7 @@ export const register = async (
152
152
const data = await response . json ( ) ;
153
153
154
154
if ( response . status !== 201 ) {
155
- toast . fire ( {
155
+ ToastComponent . fire ( {
156
156
icon : "error" ,
157
157
title : data . message ,
158
158
} ) ;
@@ -175,7 +175,7 @@ export const getUser = async (userId = "") => {
175
175
const data = await response . json ( ) ;
176
176
177
177
if ( response . status !== 200 ) {
178
- toast . fire ( {
178
+ ToastComponent . fire ( {
179
179
icon : "error" ,
180
180
title : data . message ,
181
181
} ) ;
@@ -185,10 +185,38 @@ export const getUser = async (userId = "") => {
185
185
return data ;
186
186
} ;
187
187
188
+ export const resetPasswordFromProfile = async ( newPassword : string ) => {
189
+ const userId = getUserId ( ) ;
190
+ const token = getToken ( ) ;
191
+ const url = `${ NEXT_PUBLIC_IAM_USER_SERVICE } /${ userId } /reset-password-from-profile` ;
192
+ const response = await fetch ( url , {
193
+ method : "POST" ,
194
+ headers : {
195
+ "Content-Type" : "application/json" ,
196
+ Authorization : `Bearer ${ token } ` ,
197
+ } ,
198
+ body : JSON . stringify ( { password : newPassword } ) ,
199
+ } ) ;
200
+
201
+ const data = await response . json ( ) ;
202
+ if ( response . status === 400 ) {
203
+ ToastComponent . fire ( {
204
+ icon : "error" ,
205
+ title : data . message ,
206
+ } ) ;
207
+ return false ;
208
+ } else if ( response . status === 200 ) {
209
+ ToastComponent . fire ( {
210
+ icon : "success" ,
211
+ title : data . message ,
212
+ } ) ;
213
+ return true ;
214
+ }
215
+ } ;
216
+
188
217
export const updateUser = async ( userData : {
189
218
username ?: string ;
190
219
email ?: string ;
191
- password ?: string ;
192
220
bio ?: string ;
193
221
linkedin ?: string ;
194
222
github ?: string ;
@@ -208,14 +236,14 @@ export const updateUser = async (userData: {
208
236
console . log ( data ) ;
209
237
210
238
if ( response . status !== 200 ) {
211
- toast . fire ( {
239
+ ToastComponent . fire ( {
212
240
icon : "error" ,
213
241
title : data . message ,
214
242
} ) ;
215
243
return false ;
216
244
}
217
245
218
- toast . fire ( {
246
+ ToastComponent . fire ( {
219
247
icon : "success" ,
220
248
title : "Profile updated successfully" ,
221
249
} ) ;
@@ -241,7 +269,7 @@ export const getFileUrl = async (
241
269
const data : UploadProfilePictureResponse = await res . json ( ) ;
242
270
243
271
if ( res . status !== 200 ) {
244
- toast . fire ( {
272
+ ToastComponent . fire ( {
245
273
icon : "error" ,
246
274
title : res . statusText ,
247
275
} ) ;
@@ -251,7 +279,7 @@ export const getFileUrl = async (
251
279
} ;
252
280
253
281
export const requestPasswordReset = async ( email : string ) => {
254
- toast . fire ( {
282
+ ToastComponent . fire ( {
255
283
icon : "info" ,
256
284
title : "Requesting password reset..." ,
257
285
} ) ;
@@ -269,14 +297,14 @@ export const requestPasswordReset = async (email: string) => {
269
297
console . log ( data ) ;
270
298
271
299
if ( response . status !== 200 ) {
272
- toast . fire ( {
300
+ ToastComponent . fire ( {
273
301
icon : "error" ,
274
302
title : data . message ,
275
303
} ) ;
276
304
return false ;
277
305
}
278
306
279
- toast . fire ( {
307
+ ToastComponent . fire ( {
280
308
icon : "success" ,
281
309
title : "Password reset requested. Please check your email." ,
282
310
} ) ;
@@ -298,7 +326,7 @@ export const checkPasswordResetCode = async (code: string) => {
298
326
const data = await response . json ( ) ;
299
327
300
328
if ( response . status !== 200 ) {
301
- toast . fire ( {
329
+ ToastComponent . fire ( {
302
330
icon : "error" ,
303
331
title : data . message ,
304
332
} ) ;
@@ -322,7 +350,7 @@ export const resetPasswordWithCode = async (code: string, password: string) => {
322
350
const data = await response . json ( ) ;
323
351
324
352
if ( response . status !== 200 ) {
325
- toast . fire ( {
353
+ ToastComponent . fire ( {
326
354
icon : "error" ,
327
355
title : data . message ,
328
356
} ) ;
0 commit comments