Skip to content

Commit

Permalink
Add login and password change to users plugin. (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop authored Feb 15, 2018
1 parent 5c2faab commit 968f7ed
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/errors/NotFoundError.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UwaveError from './UwaveError';

export default class NotFoundError extends UwaveError {
public = true;
name = 'NotFoundError';
code = 'NOT_FOUND';
}
7 changes: 7 additions & 0 deletions src/errors/PasswordError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import UwaveError from './UwaveError';

export default class PasswordError extends UwaveError {
public = true;
name = 'PasswordError';
code = 'INCORRECT_PASSWORD';
}
12 changes: 12 additions & 0 deletions src/errors/UwaveError.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
export default class UwaveError extends Error {
/**
* Whether this error message should be shown to users.
*/
public = false;

/**
* Name.
*/
name = 'UwaveError';

/**
* Flag this as a custom error class.
*/
isUwaveError = true;
}
4 changes: 4 additions & 0 deletions src/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export default function userModel() {
return uw.acl.disallow(this, permissions);
}

updatePassword(password: string): Promise {
return uw.users.updatePassword(this, password);
}

getPlaylists(): Promise<Array> {
return uw.playlists.getUserPlaylists(this);
}
Expand Down
57 changes: 56 additions & 1 deletion src/plugins/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as bcrypt from 'bcryptjs';
import createDebug from 'debug';
import Page from '../Page';
import NotFoundError from '../errors/NotFoundError';
import PasswordError from '../errors/PasswordError';

const debug = createDebug('uwave:users');

Expand Down Expand Up @@ -51,6 +53,41 @@ export class UsersRepository {
return User.findById(id);
}

login({ type, ...params }) {
if (type === 'local') {
return this.localLogin(params);
}
return this.socialLogin(type, params);
}

async localLogin({ email, password }) {
const Authentication = this.uw.model('Authentication');

const auth = await Authentication.findOne({
email: email.toLowerCase(),
}).populate('user').exec();
if (!auth) {
throw new NotFoundError('No user was found with that email address.');
}

const correct = await bcrypt.compare(password, auth.hash);
if (!correct) {
throw new PasswordError('That password is incorrect.');
}

return auth.user;
}

async socialLogin(type, { profile }) {
const user = {
type,
id: profile.id,
username: profile.displayName,
avatar: profile.photos.length > 0 ? profile.photos[0].value : null,
};
return this.uw.users.findOrCreateSocialUser(user);
}

async findOrCreateSocialUser({
type,
id,
Expand Down Expand Up @@ -151,9 +188,27 @@ export class UsersRepository {
return user;
}

async updatePassword(id, password) {
const Authentication = this.uw.model('Authentication');

const user = await this.getUser(id);
if (!user) throw new NotFoundError('User not found.');

const hash = await encryptPassword(password);

const auth = await Authentication.findOneAndUpdate({
type: 'local',
user: user.id,
}, { hash });

if (!auth) {
throw new NotFoundError('No user was found with that email address.');
}
}

async updateUser(id, update = {}, opts = {}) {
const user = await this.getUser(id);
if (!user) throw new Error('User not found.');
if (!user) throw new NotFoundError('User not found.');

debug('update user', user.id, user.username, update);

Expand Down

0 comments on commit 968f7ed

Please sign in to comment.