@loomcore/api 0.1.21 → 0.1.22

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.
@@ -38,13 +38,12 @@ export class AuthController {
38
38
  apiUtils.apiResponse(res, 201, { data: user || undefined }, UserSpec, PublicUserSchema);
39
39
  }
40
40
  async requestTokenUsingRefreshToken(req, res, next) {
41
- const userContext = req.userContext;
42
41
  const refreshToken = req.query.refreshToken;
43
- if (!userContext || !refreshToken || typeof refreshToken !== 'string') {
44
- throw new BadRequestError('Missing required fields: userContext and refreshToken are required.');
42
+ if (!refreshToken || typeof refreshToken !== 'string') {
43
+ throw new BadRequestError('Missing required fields: refreshToken is required.');
45
44
  }
46
45
  const deviceId = this.authService.getDeviceIdFromCookie(req);
47
- const tokens = await this.authService.requestTokenUsingRefreshToken(userContext, refreshToken, deviceId);
46
+ const tokens = await this.authService.requestTokenUsingRefreshToken(refreshToken, deviceId);
48
47
  if (tokens) {
49
48
  apiUtils.apiResponse(res, 200, { data: tokens }, TokenResponseSpec);
50
49
  }
@@ -21,7 +21,7 @@ export declare class AuthService extends MultiTenantApiService<IUser> {
21
21
  } | null>;
22
22
  getUserByEmail(email: string): Promise<IUser | null>;
23
23
  createUser(userContext: IUserContext, user: Partial<IUser>): Promise<IUser | null>;
24
- requestTokenUsingRefreshToken(userContext: IUserContext, refreshToken: string, deviceId: string): Promise<ITokenResponse | null>;
24
+ requestTokenUsingRefreshToken(refreshToken: string, deviceId: string): Promise<ITokenResponse | null>;
25
25
  changeLoggedInUsersPassword(userContext: IUserContext, body: any): Promise<UpdateResult>;
26
26
  changePassword(userContext: IUserContext, queryObject: any, password: string): Promise<UpdateResult>;
27
27
  createNewTokens(userContext: IUserContext, activeRefreshToken: IRefreshToken): Promise<{
@@ -29,8 +29,8 @@ export declare class AuthService extends MultiTenantApiService<IUser> {
29
29
  refreshToken: string;
30
30
  expiresOn: number;
31
31
  }>;
32
- getActiveRefreshToken(userContext: IUserContext, refreshToken: string, deviceId: string): Promise<IRefreshToken | null>;
33
- createNewRefreshToken(userId: string, deviceId: string, existingExpiresOn?: number | null, orgId?: string): Promise<IRefreshToken | null>;
32
+ getActiveRefreshToken(refreshToken: string, deviceId: string): Promise<IRefreshToken | null>;
33
+ createNewRefreshToken(userId: string, deviceId: string, orgId?: string): Promise<IRefreshToken | null>;
34
34
  sendResetPasswordEmail(emailAddress: string): Promise<void>;
35
35
  resetPassword(email: string, passwordResetToken: string, password: string): Promise<UpdateResult>;
36
36
  deleteRefreshTokensForDevice(deviceId: string): Promise<import("../databases/models/delete-result.js").DeleteResult>;
@@ -44,7 +44,7 @@ export class AuthService extends MultiTenantApiService {
44
44
  async logUserIn(userContext, deviceId) {
45
45
  const payload = userContext;
46
46
  const accessToken = this.generateJwt(payload);
47
- const refreshTokenObject = await this.createNewRefreshToken(userContext.user._id, deviceId, null, userContext._orgId);
47
+ const refreshTokenObject = await this.createNewRefreshToken(userContext.user._id, deviceId, userContext._orgId);
48
48
  const accessTokenExpiresOn = this.getExpiresOnFromSeconds(config.auth.jwtExpirationInSeconds);
49
49
  let loginResponse = null;
50
50
  if (refreshTokenObject) {
@@ -88,10 +88,16 @@ export class AuthService extends MultiTenantApiService {
88
88
  const createdUser = await this.create(userContext, user);
89
89
  return createdUser;
90
90
  }
91
- async requestTokenUsingRefreshToken(userContext, refreshToken, deviceId) {
91
+ async requestTokenUsingRefreshToken(refreshToken, deviceId) {
92
92
  let tokens = null;
93
- const activeRefreshToken = await this.getActiveRefreshToken(userContext, refreshToken, deviceId);
93
+ const activeRefreshToken = await this.getActiveRefreshToken(refreshToken, deviceId);
94
94
  if (activeRefreshToken) {
95
+ const systemUserContext = getSystemUserContext();
96
+ const user = await this.getById(systemUserContext, activeRefreshToken.userId);
97
+ const userContext = {
98
+ _orgId: user._orgId,
99
+ user: user
100
+ };
95
101
  tokens = await this.createNewTokens(userContext, activeRefreshToken);
96
102
  }
97
103
  return tokens;
@@ -121,8 +127,8 @@ export class AuthService extends MultiTenantApiService {
121
127
  };
122
128
  return tokenResponse;
123
129
  }
124
- async getActiveRefreshToken(userContext, refreshToken, deviceId) {
125
- const refreshTokenResult = await this.refreshTokenService.findOne(userContext, { filters: { token: { eq: refreshToken }, deviceId: { eq: deviceId } } });
130
+ async getActiveRefreshToken(refreshToken, deviceId) {
131
+ const refreshTokenResult = await this.refreshTokenService.findOne(EmptyUserContext, { filters: { token: { eq: refreshToken }, deviceId: { eq: deviceId } } });
126
132
  let activeRefreshToken = null;
127
133
  if (refreshTokenResult) {
128
134
  const now = Date.now();
@@ -133,8 +139,8 @@ export class AuthService extends MultiTenantApiService {
133
139
  }
134
140
  return activeRefreshToken;
135
141
  }
136
- async createNewRefreshToken(userId, deviceId, existingExpiresOn = null, orgId) {
137
- const expiresOn = existingExpiresOn ? existingExpiresOn : this.getExpiresOnFromDays(config.auth.refreshTokenExpirationInDays);
142
+ async createNewRefreshToken(userId, deviceId, orgId) {
143
+ const expiresOn = this.getExpiresOnFromDays(config.auth.refreshTokenExpirationInDays);
138
144
  const newRefreshToken = {
139
145
  _orgId: orgId,
140
146
  token: this.generateRefreshToken(),
@@ -11,7 +11,7 @@ export class MultiTenantApiService extends GenericApiService {
11
11
  }
12
12
  }
13
13
  prepareQuery(userContext, queryOptions, operations) {
14
- if (!config?.app?.isMultiTenant) {
14
+ if (!config?.app?.isMultiTenant || userContext?.user?._id === 'system') {
15
15
  return super.prepareQuery(userContext, queryOptions, operations);
16
16
  }
17
17
  if (!userContext || !userContext._orgId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.21",
3
+ "version": "0.1.22",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb or PostgreSQL",
6
6
  "scripts": {