Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ module.exports = {
'max-lines-per-function': ['error', { max: 75, skipComments: true }],
'no-underscore-dangle': 0,
'react/jsx-props-no-spreading': 0,
'react/prop-types': 0
'react/prop-types': 0,
'no-unused-vars': 'off',
//'@typescipt-eslint/no-unused-vars': ['error'],
'prettier/prettier': ['error', { endOfLine: 'auto' }]
}
};
3 changes: 2 additions & 1 deletion src/models/AuthCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const authCodeSchema: Schema<AuthCodeDocument> = new Schema<AuthCodeDocument>(
*/
{
// Here's an example of how to add a field to the schema.
exampleField: { required: true, type: String, unique: false }
phoneNumber: { required: true, type: String, unique: true },
value: { default: AuthUtils.generateOTP, required: true, type: Number }
},
{ timestamps: true }
);
Expand Down
24 changes: 11 additions & 13 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import AuthUtils from '../utils/AuthUtils';
import { Model } from '../utils/constants';
import { AuthTokens, BaseModel } from '../utils/types';


interface IUser extends BaseModel {
email?: string;

Expand Down Expand Up @@ -64,20 +63,18 @@ export type UserDocument = Document<{}, {}, IUser> &

const userSchema: Schema<UserDocument> = new Schema<UserDocument>(
{

email: { required: false, sparse: true, type: String, unique: true},
firstName: { required: false, type: String},
instagramUrl: { required: false, type: String},
lastName: { required: false, type: String},
linkedInUrl: { required: false, type: String},
profilePictureKey: { required: false, type: String},
phoneNumber : { required: true, type: String, unique: true},
twitterUrl: { required: false, type: String},


email: { required: false, sparse: true, type: String, unique: true },
firstName: { required: false, type: String },
instagramUrl: { required: false, type: String },
lastName: { required: false, type: String },
linkedInUrl: { required: false, type: String },
phoneNumber: { required: true, type: String, unique: true },
profilePictureKey: { required: false, type: String },
// We shouldn't be returning the refreshToken when fetching a user from
// the database, since that is sensitive information.
refreshToken: { required: false, select: false, type: String}
refreshToken: { required: false, select: false, type: String },

twitterUrl: { required: false, type: String }
},
{
timestamps: true,
Expand All @@ -90,6 +87,7 @@ type TokenArgs = {
date: number;
id: string;
};

/*
* Creates and returns a new access and refresh token for the user. It also
* persists the refresh token to the database, so we can associate the token
Expand Down
2 changes: 1 addition & 1 deletion src/routes/LoginRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import TestUtils from '../utils/TestUtils';
* npm run test LoginRoute
* - Delete this comment.
*/
describe.skip('POST /login', () => {
describe('POST /login', () => {
test('If the phone number is not valid, should return a 400.', async () => {
await TestUtils.agent
.post('/login')
Expand Down
35 changes: 21 additions & 14 deletions src/routes/LoginRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,22 @@ type LoginBody = Pick<UserDocument, 'phoneNumber'>;
export default class LoginRoute extends BaseRoute<boolean> {
constructor() {
super({
/**
* TODO: (7.01)
* - Replace null with the correct route type from the RouteMethod enum
* in the constants.ts file.
* - Fill in the path string with the appropriate path to this endpoint.
* - Delete this comment.
*/
method: null,
path: '/'
method: RouteMethod.POST,
path: '/login'
});
}

/**
* Validate the following inputs:
* - body.phoneNumber
*/
middleware() {
/**
* TODO: (7.02)
* - Add a validation the returned array ensureing that the phoneNumber
* field in the body is a valid US phone number.
*/
return [];
return [
body('phoneNumber')
.isMobilePhone('en-US')
.withMessage('This is not a valid phone number')
];
}

/**
Expand All @@ -56,16 +49,30 @@ export default class LoginRoute extends BaseRoute<boolean> {
* - Send a text to the user with the code.
*/
// TODO: (7.03) Get the phone number from the request body.
const { phoneNumber } = req.body;

// TODO: (7.03) We should delete all codes that previously existed for the
// user.
await AuthCode.deleteMany({ phoneNumber });

// TODO: (7.03) Create a new AuthCode document in the database.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const authCode: AuthCodeDocument = await AuthCode.create({ phoneNumber });

// TODO: (7.03) Send a text to the user.
const wasTextSent: boolean = await TextService.sendText({
message: 'Your OTP code: $(authCode.value)',
to: phoneNumber
});

// TODO: (7.03) If the text was not sent, throw a new RouteError with status
// code 500.
if (!wasTextSent) {
throw new RouteError({
message: 'Failed to send OTP code text, please try again',
statusCode: 500
});
}

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/UpdateMeRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import TestUtils from '../utils/TestUtils';
* npm run test UpdateMe
* - Delete this comment.
*/
describe.skip('PATCH /me', () => {
describe('PATCH /me', () => {
test('If the user is not authenticated, should return a 401.', async () => {
await TestUtils.agent.patch('/me').expect(401);
});
Expand Down
21 changes: 18 additions & 3 deletions src/routes/UpdateMeRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export default class UpdateMeRoute extends BaseRoute<UserDocument> {
* - Fill in the path string with the appropriate path to this endpoint.
* - Delete this comment.
*/
authenticated: false,
method: null,
path: '/'
authenticated: true,
method: RouteMethod.PATCH,
path: '/me'
});
}

Expand All @@ -54,6 +54,21 @@ export default class UpdateMeRoute extends BaseRoute<UserDocument> {
.isURL()
.withMessage('The Instagram URL must be a valid URL.'),

body('linkedInUrl')
.if((value: string) => Utils.isDefined(value) && !!value.length)
.isURL()
.withMessage('The LinkedIn URL must be a valid URL.'),

body('twitterUrl')
.if((value: string) => Utils.isDefined(value) && !!value.length)
.isURL()
.withMessage('The Twitter URL must be a valid URL.'),

body('lastName')
.if((value: string) => Utils.isDefined(value))
.isLength({ min: 1 })
.withMessage('Last name cannot be empty.'),

multer().single('profilePicture')
];
}
Expand Down
33 changes: 17 additions & 16 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "commonjs",
"outDir": "./dist",
"target": "es2018"
},
"include": ["src/**/*.ts"],
"exclude": [
"node_modules",
"src/**/*.test.ts",
"./*.config.ts",
"./jest.*.ts"
],
"typeRoots": ["./node_modules/@types"]
}
{
"compilerOptions": {
"esModuleInterop": true,
"module": "commonjs",
"outDir": "./dist",
"target": "es2018"

},
"include": ["src/**/*.ts"],
"exclude": [
"node_modules",
"src/**/*.test.ts",
"./*.config.ts",
"./jest.*.ts"
],
"typeRoots": ["./node_modules/@types"]
}