Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify email #47

Merged
merged 5 commits into from
Feb 5, 2017
Merged
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
2 changes: 2 additions & 0 deletions packages/common/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { EMAIL_ONLY } from './passwordSignupFields';

export default {
sendVerificationEmail: false,
sendEnrollmentEmail: false,
sendWelcomeEmail: false,
forbidClientAccountCreation: false,
restrictCreationByEmailDomain: null,
loginExpirationInDays: 90,
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export type UserObjectType = {
username: ?string,
email: ?string,
id: ?string,
profile: ?Object
profile: ?Object,
services: ?Object
};

export type CreateUserType = {
Expand Down
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@accounts/common": "^0.0.2",
"apollo-errors": "^1.2.1",
"bcryptjs": "^2.4.0",
"crypto": "^0.0.3",
"jsonwebtoken": "^7.2.1",
"jwt-decode": "^2.1.0",
"lodash": "^4.16.4"
Expand Down
29 changes: 24 additions & 5 deletions packages/server/src/AccountsServer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import { isString, isPlainObject } from 'lodash';
import { isString, isPlainObject, find } from 'lodash';
import jwt from 'jsonwebtoken';
import {
AccountsError,
Expand Down Expand Up @@ -242,8 +242,27 @@ export class AccountsServer {
removeEmail(userId: string, email: string): Promise<void> {
return this.db.removeEmail(userId, email);
}
verifyEmail(userId: string, email: string): Promise<void> {
return this.db.verifyEmail(userId, email);
async verifyEmail(token: string): Promise<void> {
const user = await this.db.findUserByEmailVerificationToken();
if (!user) {
throw new AccountsError({
message: 'Verify email link expired',
});
}
const tokenRecord = find(user.services.email.verificationTokens,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add services to the UserObjectType

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimMikeladze will all dbInterfaces return the user object formatted in the same way ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they should.

(t: Object) => t.token === token);
if (!tokenRecord) {
throw new AccountsError({
message: 'Verify email link expired',
});
}
const emailRecord = find(user.emails, (e: Object) => e.address === tokenRecord.address);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to make another modification to the UserObjecType and the places it is returned at, since it has a string email key. Perhaps this could be an explicit call to a this.findEmailByToken function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be the only place we will need this function, I don't think it's necessary to make a special function.

if (!emailRecord) {
throw new AccountsError({
message: 'Verify email link is for unknown address',
});
}
await this.db.verifyEmail(user.id, emailRecord);
}
setPassword(userId: string, newPassword: string): Promise<void> {
return this.db.setPasssword(userId, newPassword);
Expand Down Expand Up @@ -303,8 +322,8 @@ const Accounts = {
removeEmail(userId: string, newEmail: string): Promise<void> {
return this.instance.removeEmail(userId, newEmail);
},
verifyEmail(userId: string, email: string): Promise<void> {
return this.instance.verifyEmail(userId, email);
verifyEmail(token: string): Promise<void> {
return this.instance.verifyEmail(token);
},
setPassword(userId: string, newPassword: string): Promise<void> {
return this.instance.setPassword(userId, newPassword);
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/DBInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface DBInterface {
findUserByEmail(email: string) : Promise<?UserObjectType>,
findUserByUsername(username: string) : Promise<?UserObjectType>,
findUserById(userId: string) : Promise<?UserObjectType>,
findUserByEmailVerificationToken(token: string) : Promise<?UserObjectType>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the name of the function

setUsername(userId: string, newUsername: string) : Promise<void>,
addEmail(userId: string, newEmail: string, verified: boolean) : Promise<void>,
removeEmail(userId: string, email: string) : Promise<void>,
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/tokens.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// @flow
import jwt from 'jsonwebtoken';
import crypto from 'crypto';

export const generateEmailToken = (length: Int = 43) => crypto.randomBytes(length).toString('hex');

export const generateAccessToken = ({
secret,
Expand Down