itube-specs 0.0.233 → 0.0.234
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/composables/use-user.ts +244 -0
- package/package.json +1 -1
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IRecoveryPasswordForm,
|
|
3
|
+
IProfileData,
|
|
4
|
+
IChangePasswordForm,
|
|
5
|
+
ILoginForm,
|
|
6
|
+
IPasswordForm,
|
|
7
|
+
IRegistrateForm,
|
|
8
|
+
IChangeEmail
|
|
9
|
+
} from '../types';
|
|
10
|
+
import { ApiHelper } from '../runtime';
|
|
11
|
+
|
|
12
|
+
async function recoveryPasswordApi(data: IRecoveryPasswordForm): Promise<any> {
|
|
13
|
+
return await ApiHelper.fetch('/authorization/recovery-password', {
|
|
14
|
+
method: 'POST',
|
|
15
|
+
headers: {
|
|
16
|
+
'X-Cache-Time': 259200,
|
|
17
|
+
'Content-Type': 'application/json',
|
|
18
|
+
},
|
|
19
|
+
body: data,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function changePasswordApi(data: IChangePasswordForm): Promise<any> {
|
|
24
|
+
return await ApiHelper.fetch('/authorization/change-password', {
|
|
25
|
+
method: 'POST',
|
|
26
|
+
headers: {
|
|
27
|
+
'X-Cache-Time': 259200,
|
|
28
|
+
'Content-Type': 'application/json',
|
|
29
|
+
},
|
|
30
|
+
body: data,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function changeEmailApi(data: IChangeEmail): Promise<any> {
|
|
35
|
+
console.log(data);
|
|
36
|
+
return await ApiHelper.fetch('/authorization/change-email', {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
headers: {
|
|
39
|
+
'X-Cache-Time': 259200,
|
|
40
|
+
'Content-Type': 'application/json',
|
|
41
|
+
},
|
|
42
|
+
body: data,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function postProfileApi(data: FormData): Promise<any> {
|
|
47
|
+
return await ApiHelper.fetch('/authorization/post-profile', {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
headers: {
|
|
50
|
+
'X-Cache-Time': 259200,
|
|
51
|
+
},
|
|
52
|
+
body: data,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function getProfileApi(): Promise<IProfileData | null> {
|
|
57
|
+
return await ApiHelper.fetch('/authorization/get-profile', {
|
|
58
|
+
method: 'GET',
|
|
59
|
+
headers: {
|
|
60
|
+
'X-Cache-Time': 259200,
|
|
61
|
+
'Content-Type': 'application/json',
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function passwordApi(data: IPasswordForm): Promise<any> {
|
|
67
|
+
return await ApiHelper.fetch('/authorization/password', {
|
|
68
|
+
method: 'POST',
|
|
69
|
+
headers: {
|
|
70
|
+
'X-Cache-Time': 259200,
|
|
71
|
+
'Content-Type': 'application/json',
|
|
72
|
+
},
|
|
73
|
+
body: data,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function deleteAvatarApi(): Promise<any> {
|
|
78
|
+
return await ApiHelper.fetch('/authorization/profile-avatar', {
|
|
79
|
+
method: 'DELETE',
|
|
80
|
+
headers: {
|
|
81
|
+
'X-Cache-Time': 259200,
|
|
82
|
+
'Content-Type': 'application/json',
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function loginApi(data: ILoginForm): Promise<any> {
|
|
88
|
+
return await ApiHelper.fetch('/authorization/login', {
|
|
89
|
+
method: 'POST',
|
|
90
|
+
headers: {
|
|
91
|
+
'X-Cache-Time': 259200,
|
|
92
|
+
'Content-Type': 'application/json',
|
|
93
|
+
},
|
|
94
|
+
body: data,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function registerApi(data: IRegistrateForm): Promise<any> {
|
|
99
|
+
return await ApiHelper.fetch('/authorization/register', {
|
|
100
|
+
method: 'POST',
|
|
101
|
+
headers: {
|
|
102
|
+
'X-Cache-Time': 259200,
|
|
103
|
+
'Content-Type': 'application/json',
|
|
104
|
+
},
|
|
105
|
+
body: data,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function confirmRegistrationApi(token: string): Promise<any> {
|
|
110
|
+
return await ApiHelper.fetch('/authorization/confirm', {
|
|
111
|
+
method: 'GET',
|
|
112
|
+
headers: {
|
|
113
|
+
'X-Cache-Time': 259200,
|
|
114
|
+
'X-Domain': useRuntimeConfig()?.public?.xDomain || useAppConfig().xDomain,
|
|
115
|
+
'Content-Type': 'application/json',
|
|
116
|
+
},
|
|
117
|
+
params: {
|
|
118
|
+
token,
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const { snackbarText, showErrorSnack } = useSnackbar();
|
|
124
|
+
|
|
125
|
+
const register = async (form: IRegistrateForm) => {
|
|
126
|
+
const response = await registerApi(form);
|
|
127
|
+
if (response) {
|
|
128
|
+
useCookie('jwtoken', { path: '/', secure: true, httpOnly: false }).value = response.token;
|
|
129
|
+
await getProfile();
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const getProfile = async (update = false) => {
|
|
134
|
+
const profileData = useState<IProfileData | null>('profileData', () => null);
|
|
135
|
+
|
|
136
|
+
if (!profileData.value || update) {
|
|
137
|
+
try {
|
|
138
|
+
profileData.value = await getProfileApi();
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.error('Error get profile:', error);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
|
|
146
|
+
const result = {} as Omit<T, K>;
|
|
147
|
+
for (const key in obj) {
|
|
148
|
+
if (!keys.includes(key as unknown as K)) {
|
|
149
|
+
result[key] = obj[key];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const updateProfile = async (form: IProfileData) => {
|
|
156
|
+
try {
|
|
157
|
+
const formData = new FormData();
|
|
158
|
+
const config = JSON.stringify(omit(form, ['avatar']));
|
|
159
|
+
|
|
160
|
+
formData.append('config', config);
|
|
161
|
+
|
|
162
|
+
if (form.avatar) {
|
|
163
|
+
formData.append('avatar', form.avatar);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
await postProfileApi(formData);
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error('Error edit profile:', error);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const login = async (form: ILoginForm) => {
|
|
173
|
+
const response = await loginApi(form);
|
|
174
|
+
if (response) {
|
|
175
|
+
useCookie('jwtoken', { path: '/', secure: true, httpOnly: false }).value = response.token;
|
|
176
|
+
await getProfile();
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const changePassword = async (form: IChangePasswordForm) => {
|
|
181
|
+
try {
|
|
182
|
+
await changePasswordApi(form);
|
|
183
|
+
snackbarText.value = 'Password changed';
|
|
184
|
+
} catch (err) {
|
|
185
|
+
showErrorSnack('Error while changing password');
|
|
186
|
+
console.log(err, 'error change password');
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const changeEmail = async (form: IChangeEmail) => {
|
|
191
|
+
try {
|
|
192
|
+
await changeEmailApi(form);
|
|
193
|
+
snackbarText.value = 'email_changed';
|
|
194
|
+
} catch (err) {
|
|
195
|
+
showErrorSnack('Error while changing email');
|
|
196
|
+
console.log(err, 'error change email');
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const recoveryPassword = async (form: IRecoveryPasswordForm) => {
|
|
201
|
+
try {
|
|
202
|
+
await recoveryPasswordApi(form);
|
|
203
|
+
snackbarText.value = 'Password recovered';
|
|
204
|
+
await useRouter().push('/profile');
|
|
205
|
+
} catch (err) {
|
|
206
|
+
showErrorSnack('Error while recovery password');
|
|
207
|
+
console.log(err, 'error change password');
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const password = async (form: IPasswordForm) => {
|
|
212
|
+
await passwordApi(form);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const deleteAvatar = async () => {
|
|
216
|
+
await deleteAvatarApi();
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const signOut = () => {
|
|
220
|
+
const cookie = useCookie('jwtoken', { path: '/', secure: true, httpOnly: false });
|
|
221
|
+
cookie.value = undefined; // или null
|
|
222
|
+
document.cookie = "jwtoken=; path=/; domain=gay.cool; expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure";;
|
|
223
|
+
|
|
224
|
+
const profile = useState<IProfileData | null>('profileData');
|
|
225
|
+
profile.value = null;
|
|
226
|
+
|
|
227
|
+
const router = useRouter();
|
|
228
|
+
router.push('/');
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
export const useUser = () => ({
|
|
232
|
+
isAuthorized: computed(() => process.client && !!useCookie('jwtoken').value),
|
|
233
|
+
register,
|
|
234
|
+
login,
|
|
235
|
+
password,
|
|
236
|
+
signOut,
|
|
237
|
+
getProfile,
|
|
238
|
+
updateProfile,
|
|
239
|
+
changePassword,
|
|
240
|
+
recoveryPassword,
|
|
241
|
+
deleteAvatar,
|
|
242
|
+
changeEmail,
|
|
243
|
+
confirmRegistrationApi,
|
|
244
|
+
});
|