Skip to content

Commit

Permalink
[Refactor] User auth response
Browse files Browse the repository at this point in the history
Changed response so it contains only information needed for the app.
References #61 and  #69.
  • Loading branch information
angel-penchev committed Feb 20, 2021
1 parent 17ed3f3 commit b8a4f20
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 55 deletions.
2 changes: 1 addition & 1 deletion server/services/core/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ export interface UserDatabaseController {

findByPhoneNumber(
phoneNumber: string,
): Promise<User>;
): Promise<User>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export default function buildPostAuthenticateSendSms({
return async function postAuthenticateSendSms(httpRequest: HttpRequest) {
try {
const phoneNumber = httpRequest.body['phoneNumber'];
const verificationInstance = await sendAuthenticationSmsCode(
const authenticationSmsStatus = await sendAuthenticationSmsCode(
phoneNumber,
);
return {
headers: {
'Content-Type': 'application/json',
},
statusCode: 201,
body: {verificationInstance},
statusCode: 200,
body: authenticationSmsStatus,
};
} catch (e) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export default function buildPostAuthenticateVerifyCode({
headers: {
'Content-Type': 'application/json',
},
statusCode: 201,
body: {verificationInstance},
statusCode: 200,
body: verificationInstance,
};
} catch (e) {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
SharedQueue,
SMSApi,
SMSVerificationInstance,
} from '../../../core/@types/global';
import config from '../../../core/config';

Expand All @@ -27,24 +26,18 @@ export default function buildSendAuthenticationSmsCode({
sharedQueue: SharedQueue;
}): Function {
return async function sendAuthenticationSmsCode(phoneNumber: string) {
// Internal parameter
let verificationInstance: SMSVerificationInstance;

// Emitting an 'USER_INVALID_NUMBER' event in shared queue on invalid number
try {
// Sending verification code
verificationInstance = await smsApi.sendCode(
phoneNumber,
'sms',
);
await smsApi.sendCode(phoneNumber, 'sms');
} catch (e) {
sharedQueue.emit([
config.inboundLoggerServiceQueue,
], {
subject: 'USER_INVALID_NUMBER',
body: {phoneNumber: phoneNumber, error: e.message},
});
return;
return {phoneNumber: phoneNumber, succeeded: false};
}

// Emitting an 'USER_SMS_SEND' event in shared queue on valid number
Expand All @@ -56,6 +49,6 @@ export default function buildSendAuthenticationSmsCode({
body: {phoneNumber: phoneNumber},
});

return verificationInstance;
return {phoneNumber: phoneNumber, succeeded: true};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,51 +55,45 @@ export default function buildVerifyAuthenticationSmsCode({
config.inboundLoggerServiceQueue,
], {
subject: 'USER_INVALID_CODE',
body: {phoneNumber: phoneNumber, error: e.message},
body: {phoneNumber: phoneNumber, error: 'Invalid verification code.'},
});
return {error: e.message};
return {error: 'Invalid verification code.'};
}

usersDatabase.findByPhoneNumber(verificationInstance.to).catch((_) => {
try {
// Creating user object
user = makeUser({
phoneNumber: verificationInstance.to,
});
normalizedUser = exportToNormalEntity(user);
// If user is not found in the database we create a new user and store it
return usersDatabase.findByPhoneNumber(
verificationInstance.to,
).catch((_) => {
// Creating user object
user = makeUser({
phoneNumber: verificationInstance.to,
});
normalizedUser = exportToNormalEntity(user);

// Inserts into database
usersDatabase.insert(normalizedUser).catch((e) => {
throw e;
});
// Inserts into database
usersDatabase.insert(normalizedUser).catch((e) => {
throw e;
});

// Notifying logger for a registered user
sharedQueue.emit([
config.inboundLoggerServiceQueue,
], {
subject: 'USER_REGISTERED',
body: {phoneNumber: phoneNumber},
});
} catch (e) {
sharedQueue.emit([
config.inboundLoggerServiceQueue,
], {
subject: 'USER_FAILED_REGISTRATION',
body: {phoneNumber: phoneNumber, error: e.message},
});
return {error: e.message};
}
});
// Notifying logger for a registered user
sharedQueue.emit([
config.inboundLoggerServiceQueue,
], {
subject: 'USER_REGISTERED',
body: {phoneNumber: phoneNumber},
});
return normalizedUser;
}).then((user) => {
// Emitting an 'USER_LOGGED_IN' event in shared queue on valid code
sharedQueue.emit([
config.inboundDeliveryServiceQueue,
config.inboundLoggerServiceQueue,
], {
subject: 'USER_LOGGED_IN',
body: {phoneNumber: phoneNumber},
});

// Emitting an 'USER_LOGGED_IN' event in shared queue on valid code
sharedQueue.emit([
config.inboundDeliveryServiceQueue,
config.inboundLoggerServiceQueue,
], {
subject: 'USER_LOGGED_IN',
body: {phoneNumber: phoneNumber},
return {'userId': user.id, 'userToken': user.token};
});

return verificationInstance;
};
}

0 comments on commit b8a4f20

Please sign in to comment.