@skroz/profile-api 1.0.12 → 1.0.13

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.
@@ -103,12 +103,12 @@ function createAuthResolver(deps) {
103
103
  confirmEmail(input, ctx) {
104
104
  return __awaiter(this, void 0, void 0, function* () {
105
105
  const service = getAuthService();
106
- const user = yield service.getUserByToken(ProfileAuthService_1.CONFIRMATION_REDIS_PREFIX, input.token);
106
+ const user = yield service.getUserByToken(ProfileAuthService_1.AUTH_TOKEN_REDIS_PREFIX, input.token);
107
107
  if (!user)
108
108
  throw new Error(ctx.t('validation:error.wrongCode'));
109
109
  user.isEmailConfirmed = true;
110
110
  yield user.save();
111
- yield service.removeTokenFromRedis(ProfileAuthService_1.CONFIRMATION_REDIS_PREFIX, user, input.token);
111
+ yield service.removeTokenFromRedis(ProfileAuthService_1.AUTH_TOKEN_REDIS_PREFIX, user, input.token);
112
112
  const userAgent = decodeURI(ctx.req.get('user-agent') || '');
113
113
  yield ctx.req.session.create({
114
114
  userId: user.id,
@@ -123,35 +123,16 @@ function createAuthResolver(deps) {
123
123
  return user;
124
124
  });
125
125
  }
126
- forgotPassword(input, ctx) {
126
+ sendToken(input, ctx) {
127
127
  return __awaiter(this, void 0, void 0, function* () {
128
128
  const { t } = ctx;
129
129
  const service = getAuthService();
130
130
  const user = yield service.db.findUserByEmail(input.email);
131
131
  if (!user)
132
- throw new Error(t('validation:forgot.errors.notRegistered'));
133
- const res = yield service.sendLink(user, 'recovery');
134
- if (logTelegramBot) {
135
- yield logTelegramBot.sendError(`${user.email || user.urlSlug} запросил(а) восстановление пароля`);
136
- }
137
- return {
138
- codeIsSent: res.ok,
139
- limitExpiresAt: res.limitExpiresAt,
140
- };
141
- });
142
- }
143
- sendToken(input, ctx) {
144
- return __awaiter(this, void 0, void 0, function* () {
145
- const { user, t } = ctx;
146
- if (!user)
147
- throw new Error(t('validation:error.unauthorized'));
148
- const service = getAuthService();
149
- if (yield service.db.isEmailTaken(input.email, user.id)) {
150
- throw new Error(t('validation:auth.emailExists'));
151
- }
132
+ throw new Error(t('validation:user.notFound'));
152
133
  const res = yield service.sendLink(user, 'confirmation');
153
134
  if (logTelegramBot) {
154
- yield logTelegramBot.sendError(`Отправлен код подтверждения ${input.email}`);
135
+ yield logTelegramBot.sendError(`Отправлен код верификации ${input.email}`);
155
136
  }
156
137
  return {
157
138
  codeIsSent: res.ok,
@@ -162,13 +143,13 @@ function createAuthResolver(deps) {
162
143
  recoverPassword(input, ctx) {
163
144
  return __awaiter(this, void 0, void 0, function* () {
164
145
  const service = getAuthService();
165
- const user = yield service.getUserByToken(ProfileAuthService_1.RECOVERY_REDIS_PREFIX, input.token);
146
+ const user = yield service.getUserByToken(ProfileAuthService_1.AUTH_TOKEN_REDIS_PREFIX, input.token);
166
147
  if (!user)
167
148
  throw new Error(ctx.t('validation:error.wrongCode'));
168
149
  user.password = yield service.hashPassword(input.password);
169
150
  user.isTempPassword = false;
170
151
  yield user.save();
171
- yield service.removeTokenFromRedis(ProfileAuthService_1.RECOVERY_REDIS_PREFIX, user, input.token);
152
+ yield service.removeTokenFromRedis(ProfileAuthService_1.AUTH_TOKEN_REDIS_PREFIX, user, input.token);
172
153
  const userAgent = decodeURI(ctx.req.get('user-agent') || '');
173
154
  yield ctx.req.session.create({
174
155
  userId: user.id,
@@ -224,16 +205,6 @@ function createAuthResolver(deps) {
224
205
  __metadata("design:paramtypes", [dto_1.ConfirmEmailInput, Object]),
225
206
  __metadata("design:returntype", Promise)
226
207
  ], AuthResolver.prototype, "confirmEmail", null);
227
- __decorate([
228
- (0, graphql_transformers_1.TransformArgs)(dto_1.forgotPasswordTransformers, { arg: 'input' }),
229
- (0, graphql_validators_1.ValidateArgs)(dto_1.forgotPasswordValidators, { arg: 'input', tKey: 'forgot' }),
230
- (0, type_graphql_1.Mutation)(() => dto_1.SendTokenPayload),
231
- __param(0, (0, type_graphql_1.Arg)('input')),
232
- __param(1, (0, type_graphql_1.Ctx)()),
233
- __metadata("design:type", Function),
234
- __metadata("design:paramtypes", [dto_1.ForgotPasswordInput, Object]),
235
- __metadata("design:returntype", Promise)
236
- ], AuthResolver.prototype, "forgotPassword", null);
237
208
  __decorate([
238
209
  (0, graphql_transformers_1.TransformArgs)(dto_1.sendTokenTransformers, { arg: 'input' }),
239
210
  (0, graphql_validators_1.ValidateArgs)(dto_1.sendTokenValidators, { arg: 'input', tKey: 'sendToken' }),
@@ -1,8 +1,7 @@
1
1
  import { Redis } from 'ioredis';
2
2
  import { AuthUser, ProfileAuthConfig, ProfileDbAdapter } from '../types';
3
3
  import { ProfileEmailService } from './ProfileEmailService';
4
- export declare const CONFIRMATION_REDIS_PREFIX = "conf";
5
- export declare const RECOVERY_REDIS_PREFIX = "rec";
4
+ export declare const AUTH_TOKEN_REDIS_PREFIX = "auth";
6
5
  export declare const TOKEN_REDIS_POSTFIX = "token";
7
6
  export declare const LAST_SENT_AT_REDIS_POSTFIX = "lastSentAt";
8
7
  export declare class ProfileAuthService {
@@ -12,10 +12,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.ProfileAuthService = exports.LAST_SENT_AT_REDIS_POSTFIX = exports.TOKEN_REDIS_POSTFIX = exports.RECOVERY_REDIS_PREFIX = exports.CONFIRMATION_REDIS_PREFIX = void 0;
15
+ exports.ProfileAuthService = exports.LAST_SENT_AT_REDIS_POSTFIX = exports.TOKEN_REDIS_POSTFIX = exports.AUTH_TOKEN_REDIS_PREFIX = void 0;
16
16
  const argon2_1 = __importDefault(require("argon2"));
17
- exports.CONFIRMATION_REDIS_PREFIX = 'conf';
18
- exports.RECOVERY_REDIS_PREFIX = 'rec';
17
+ exports.AUTH_TOKEN_REDIS_PREFIX = 'auth';
19
18
  exports.TOKEN_REDIS_POSTFIX = 'token';
20
19
  exports.LAST_SENT_AT_REDIS_POSTFIX = 'lastSentAt';
21
20
  class ProfileAuthService {
@@ -51,9 +50,7 @@ class ProfileAuthService {
51
50
  }
52
51
  sendLink(user, type) {
53
52
  return __awaiter(this, void 0, void 0, function* () {
54
- const prefix = type === 'confirmation'
55
- ? exports.CONFIRMATION_REDIS_PREFIX
56
- : exports.RECOVERY_REDIS_PREFIX;
53
+ const prefix = exports.AUTH_TOKEN_REDIS_PREFIX;
57
54
  const ttlMinutes = type === 'confirmation'
58
55
  ? this.config.confirmationTokenLifetimeMinutes
59
56
  : this.config.recoveryTokenLifetimeMinutes;
@@ -80,7 +77,7 @@ class ProfileAuthService {
80
77
  if (!userIdStr)
81
78
  return null;
82
79
  const userId = Number(userIdStr);
83
- if (isNaN(userId))
80
+ if (Number.isNaN(userId))
84
81
  return null;
85
82
  return this.db.findUserById(userId);
86
83
  });
@@ -64,13 +64,8 @@ class ProfileEmailService {
64
64
  return __awaiter(this, void 0, void 0, function* () {
65
65
  if (!user.email)
66
66
  return false;
67
- const isConfirm = type === 'confirmation';
68
- const emailLocales = isConfirm
69
- ? this.locales.email.confirmEmail
70
- : this.locales.email.forgotPassword;
71
- const template = isConfirm
72
- ? types_1.ProfileEmailTemplate.CONFIRM_EMAIL
73
- : types_1.ProfileEmailTemplate.FORGOT_PASSWORD;
67
+ const emailLocales = this.locales.email.verificationCode;
68
+ const template = types_1.ProfileEmailTemplate.CONFIRM_EMAIL;
74
69
  return this.send(user.email, emailLocales.subject, template, {
75
70
  header: emailLocales.header,
76
71
  text: emailLocales.text,
@@ -82,6 +82,12 @@ export interface ProfileLocales {
82
82
  text: string;
83
83
  linkTitle: string;
84
84
  };
85
+ verificationCode: {
86
+ subject: string;
87
+ header: string;
88
+ text: string;
89
+ linkTitle: string;
90
+ };
85
91
  tempPassword: {
86
92
  subject: string;
87
93
  header: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skroz/profile-api",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
4
4
  "license": "MIT",
5
5
  "repository": "git@gitlab.com:skroz/libs/utils.git",
6
6
  "main": "dist/index.js",
@@ -43,5 +43,5 @@
43
43
  "type-graphql": "^1.1.1",
44
44
  "typeorm": "^0.2.45"
45
45
  },
46
- "gitHead": "49e4b60af88db3e07b62591aed5d8086bd1f4c34"
46
+ "gitHead": "64646f9451ea3748bf27f71a3112536a33f08493"
47
47
  }
@@ -4,8 +4,7 @@ import { TransformArgs } from '@os-team/graphql-transformers';
4
4
  import { ValidateArgs } from '@os-team/graphql-validators';
5
5
  import {
6
6
  ProfileAuthService,
7
- CONFIRMATION_REDIS_PREFIX,
8
- RECOVERY_REDIS_PREFIX,
7
+ AUTH_TOKEN_REDIS_PREFIX,
9
8
  } from '../services/ProfileAuthService';
10
9
  import { ProfileContext } from '../types';
11
10
  import {
@@ -153,7 +152,7 @@ export function createAuthResolver<
153
152
  ) {
154
153
  const service = getAuthService();
155
154
  const user = await service.getUserByToken(
156
- CONFIRMATION_REDIS_PREFIX,
155
+ AUTH_TOKEN_REDIS_PREFIX,
157
156
  input.token
158
157
  );
159
158
  if (!user) throw new Error(ctx.t('validation:error.wrongCode'));
@@ -161,7 +160,7 @@ export function createAuthResolver<
161
160
  user.isEmailConfirmed = true;
162
161
  await user.save();
163
162
  await service.removeTokenFromRedis(
164
- CONFIRMATION_REDIS_PREFIX,
163
+ AUTH_TOKEN_REDIS_PREFIX,
165
164
  user,
166
165
  input.token
167
166
  );
@@ -183,50 +182,21 @@ export function createAuthResolver<
183
182
  return user;
184
183
  }
185
184
 
186
- @TransformArgs(forgotPasswordTransformers, { arg: 'input' })
187
- @ValidateArgs(forgotPasswordValidators, { arg: 'input', tKey: 'forgot' })
188
- @Mutation(() => SendTokenPayload)
189
- async forgotPassword(
190
- @Arg('input') input: ForgotPasswordInput,
191
- @Ctx() ctx: TContext
192
- ) {
193
- const { t } = ctx;
194
- const service = getAuthService();
195
- const user = await service.db.findUserByEmail(input.email);
196
- if (!user) throw new Error(t('validation:forgot.errors.notRegistered'));
197
-
198
- const res = await service.sendLink(user, 'recovery');
199
-
200
- if (logTelegramBot) {
201
- await logTelegramBot.sendError(
202
- `${user.email || user.urlSlug} запросил(а) восстановление пароля`
203
- );
204
- }
205
-
206
- return {
207
- codeIsSent: res.ok,
208
- limitExpiresAt: res.limitExpiresAt,
209
- };
210
- }
211
185
 
212
186
  @TransformArgs(sendTokenTransformers, { arg: 'input' })
213
187
  @ValidateArgs(sendTokenValidators, { arg: 'input', tKey: 'sendToken' })
214
188
  @Mutation(() => SendTokenPayload)
215
189
  async sendToken(@Arg('input') input: SendTokenInput, @Ctx() ctx: TContext) {
216
- const { user, t } = ctx;
217
- if (!user) throw new Error(t('validation:error.unauthorized'));
218
-
190
+ const { t } = ctx;
219
191
  const service = getAuthService();
220
-
221
- if (await service.db.isEmailTaken(input.email, user.id)) {
222
- throw new Error(t('validation:auth.emailExists'));
223
- }
192
+ const user = await service.db.findUserByEmail(input.email);
193
+ if (!user) throw new Error(t('validation:user.notFound'));
224
194
 
225
195
  const res = await service.sendLink(user, 'confirmation');
226
196
 
227
197
  if (logTelegramBot) {
228
198
  await logTelegramBot.sendError(
229
- `Отправлен код подтверждения ${input.email}`
199
+ `Отправлен код верификации ${input.email}`
230
200
  );
231
201
  }
232
202
 
@@ -245,7 +215,7 @@ export function createAuthResolver<
245
215
  ) {
246
216
  const service = getAuthService();
247
217
  const user = await service.getUserByToken(
248
- RECOVERY_REDIS_PREFIX,
218
+ AUTH_TOKEN_REDIS_PREFIX,
249
219
  input.token
250
220
  );
251
221
  if (!user) throw new Error(ctx.t('validation:error.wrongCode'));
@@ -254,7 +224,7 @@ export function createAuthResolver<
254
224
  user.isTempPassword = false;
255
225
  await user.save();
256
226
  await service.removeTokenFromRedis(
257
- RECOVERY_REDIS_PREFIX,
227
+ AUTH_TOKEN_REDIS_PREFIX,
258
228
  user,
259
229
  input.token
260
230
  );
@@ -3,8 +3,7 @@ import { Redis } from 'ioredis';
3
3
  import { AuthUser, ProfileAuthConfig, ProfileDbAdapter } from '../types';
4
4
  import { ProfileEmailService } from './ProfileEmailService';
5
5
 
6
- export const CONFIRMATION_REDIS_PREFIX = 'conf';
7
- export const RECOVERY_REDIS_PREFIX = 'rec';
6
+ export const AUTH_TOKEN_REDIS_PREFIX = 'auth';
8
7
  export const TOKEN_REDIS_POSTFIX = 'token';
9
8
  export const LAST_SENT_AT_REDIS_POSTFIX = 'lastSentAt';
10
9
 
@@ -68,10 +67,7 @@ export class ProfileAuthService {
68
67
  }
69
68
 
70
69
  async sendLink(user: AuthUser, type: 'confirmation' | 'recovery') {
71
- const prefix =
72
- type === 'confirmation'
73
- ? CONFIRMATION_REDIS_PREFIX
74
- : RECOVERY_REDIS_PREFIX;
70
+ const prefix = AUTH_TOKEN_REDIS_PREFIX;
75
71
  const ttlMinutes =
76
72
  type === 'confirmation'
77
73
  ? this.config.confirmationTokenLifetimeMinutes
@@ -107,7 +103,7 @@ export class ProfileAuthService {
107
103
  if (!userIdStr) return null;
108
104
 
109
105
  const userId = Number(userIdStr);
110
- if (isNaN(userId)) return null;
106
+ if (Number.isNaN(userId)) return null;
111
107
 
112
108
  return this.db.findUserById(userId);
113
109
  }
@@ -87,14 +87,8 @@ export class ProfileEmailService {
87
87
  ) {
88
88
  if (!user.email) return false;
89
89
 
90
- const isConfirm = type === 'confirmation';
91
- const emailLocales = isConfirm
92
- ? this.locales.email.confirmEmail
93
- : this.locales.email.forgotPassword;
94
-
95
- const template = isConfirm
96
- ? ProfileEmailTemplate.CONFIRM_EMAIL
97
- : ProfileEmailTemplate.FORGOT_PASSWORD;
90
+ const emailLocales = this.locales.email.verificationCode;
91
+ const template = ProfileEmailTemplate.CONFIRM_EMAIL;
98
92
 
99
93
  return this.send(
100
94
  user.email,
@@ -84,6 +84,12 @@ export interface ProfileLocales {
84
84
  text: string;
85
85
  linkTitle: string;
86
86
  };
87
+ verificationCode: {
88
+ subject: string;
89
+ header: string;
90
+ text: string;
91
+ linkTitle: string;
92
+ };
87
93
  tempPassword: {
88
94
  subject: string;
89
95
  header: string;