Skip to content

Commit 1795f42

Browse files
authored
YH-1239: add tg auth (#803)
1 parent 02cb5ba commit 1795f42

23 files changed

Lines changed: 214 additions & 28 deletions

File tree

‎public/locales/en/auth.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"add.consent": "I give <processingLink>consent to receive advertising and informational newsletters</processingLink>."
2828
},
2929
"have.account": "Already have an account?",
30-
"login.link": "Log in"
30+
"login.link": "Log in",
31+
"social.media": "Register via social networks"
3132
},
3233
"form": {
3334
"email": {

‎public/locales/en/translation.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@
133133
"password": {
134134
"wrong": "User with this email has another password."
135135
}
136+
},
137+
"telegram": {
138+
"verify_denied": "Telegram verification denied",
139+
"data_outdated": "Telegram data is outdated",
140+
"invalid_data": "Telegram data is invalid"
136141
}
137142
},
138143
"questions": {
@@ -242,6 +247,9 @@
242247
},
243248
"set": {
244249
"failed": "Failed to switch profile"
250+
},
251+
"specialization": {
252+
"already_exists": "Specialization already exists"
245253
}
246254
},
247255
"user": {

‎public/locales/ru/auth.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"add.consent": "Даю <processingLink>согласие на получение рекламных и информационных рассылок</processingLink>"
2828
},
2929
"have.account": "Уже есть аккаунт?",
30-
"login.link": "Войти"
30+
"login.link": "Войти",
31+
"social.media": "Зарегистрироваться через социальные сети"
3132
},
3233
"form": {
3334
"email": {

‎src/entities/auth/api/authApi.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
RefreshResponse,
2020
SignUpBodyRequest,
2121
SignUpResponse,
22+
TelegramLoginBodyRequest,
23+
TelegramLoginResponse,
2224
} from '../model/types/auth';
2325

2426
export const authApi = baseApi.injectEndpoints({
@@ -48,6 +50,28 @@ export const authApi = baseApi.injectEndpoints({
4850
}
4951
},
5052
}),
53+
telegram: build.mutation<TelegramLoginResponse, TelegramLoginBodyRequest>({
54+
query: (body) => ({
55+
url: authApiUrls.telegram,
56+
method: 'POST',
57+
body,
58+
}),
59+
async onQueryStarted(_, { queryFulfilled, extra }) {
60+
try {
61+
const result = await queryFulfilled;
62+
setToLS(LS_ACCESS_TOKEN_KEY, result.data.access_token);
63+
const typedExtra = extra as ExtraArgument;
64+
typedExtra.navigate(ROUTES.platformRoute);
65+
} catch (error) {
66+
if (error && typeof error === 'object' && 'error' in error) {
67+
const errObj = error as { error: { data: { message: string } } };
68+
toast.error(i18n.t('toast.' + errObj.error.data.message));
69+
}
70+
// eslint-disable-next-line no-console
71+
console.error(error);
72+
}
73+
},
74+
}),
5175
register: build.mutation<SignUpResponse, SignUpBodyRequest>({
5276
query: (body) => ({
5377
url: authApiUrls.register,
@@ -126,6 +150,7 @@ export const authApi = baseApi.injectEndpoints({
126150

127151
export const {
128152
useLoginMutation,
153+
useTelegramMutation,
129154
useRegisterMutation,
130155
useProfileQuery,
131156
useLazyLogoutQuery,

‎src/entities/auth/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ export type {
77
User,
88
Role,
99
RoleName,
10+
TelegramUser,
1011
} from './model/types/auth';
1112
export { listAdminRoles } from './model/constants/authConstants';
1213
export { authApi } from './api/authApi';
1314
export {
1415
useLoginMutation,
1516
useRegisterMutation,
17+
useTelegramMutation,
1618
useProfileQuery,
1719
useLazyLogoutQuery,
1820
useLazyRefreshQuery,

‎src/entities/auth/model/constants/authConstants.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const authApiUrls = {
66
profile: 'auth/profile',
77
logout: 'auth/logout',
88
refresh: 'auth/refresh',
9+
telegram: 'auth/telegram',
910
};
1011

1112
export const listAdminRoles: RoleName[] = ['author', 'admin'];

‎src/entities/auth/model/types/auth.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ export interface RolePermission {
116116
name: string;
117117
}
118118

119+
export interface TelegramUser {
120+
id: number;
121+
first_name: string;
122+
auth_date: number;
123+
hash: string;
124+
last_name?: string;
125+
username?: string;
126+
photo_url?: string;
127+
}
128+
119129
export type LoginBodyRequest = LoginFormValues;
120130
export type LoginResponse = AuthResponse;
121131

@@ -124,3 +134,6 @@ export type SignUpResponse = AuthResponse;
124134

125135
export type RefreshResponse = AuthResponse;
126136
export type ProfileResponse = FullProfile;
137+
138+
export type TelegramLoginBodyRequest = TelegramUser;
139+
export type TelegramLoginResponse = AuthResponse;

‎src/entities/auth/ui/RegistrationLabel/RegistrationLabel.module.css‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
display: flex;
33
justify-content: space-between;
44
align-items: center;
5+
align-self: center;
56
margin-top: 48px;
67
padding: 16px;
78
width: 100%;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { LoginForm } from './ui/LoginForm/LoginForm';
2+
export { TelegramLogin } from './ui/TelegramLoginButton/ui/TelegramLogin/TelegramLogin';

‎src/features/authentication/login/ui/LoginForm/LoginForm.module.css‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.form-wrapper {
2-
margin-bottom: 20px;
2+
margin-bottom: 24px;
33
width: 100%;
44
max-width: 408px;
55
}

0 commit comments

Comments
 (0)