Skip to content

Commit

Permalink
featuture api (enable social signup): Signup social user and return u…
Browse files Browse the repository at this point in the history
…nique random password [finishes #167313352]
  • Loading branch information
Anguandia committed Oct 2, 2019
1 parent fb2b8fc commit dbb7430
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
30 changes: 23 additions & 7 deletions src/controllers/social.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import User from '../services/user.service';
import Helper from '../helpers/helper';
import randPass from '../helpers/passwordgen';
import dbService from '../services/db.service';

let data;

Expand All @@ -21,23 +23,38 @@ class Social {
static async login(req, res) {
let user;
let message;
let status = 200;
let registeredUser;
data = req.user;
const firstname = data.name ? data.name.givenName : data.displayName.split(' ')[0];
const lastname = data.name ? data.name.middleName || data.name.familyName : data.displayName.split(' ')[1];
const email = data.emails ? data.emails[0].value : '';
const username = `${firstname}.${lastname}`;
// check if user is in db
const registeredUser = await User.findOne(email, username);
const tempUser = await User.findOne(email, username);
if (data.provider === 'twitter') {
registeredUser = await dbService.getStat({
firstname: lastname.toLowerCase(), lastname: firstname.toLowerCase()
}, 'user')[0] || tempUser;
} else {
registeredUser = tempUser;
}
if (registeredUser) {
user = registeredUser;
message = 'Logged in successfully';
} else {
const password = Helper.hashPassword('password');
const pass = randPass();
const password = Helper.hashPassword(pass);
status = 201;
const newUser = {
firstname, lastname, email, username, password
};
user = await User.addUser(newUser);
message = 'login successful, account created with password password,please chane password on next login';
message = `Account created with password ${pass}, please change your password`;
if (data.provider === 'twitter') {
message += ' and update your email address';
console.log('cccc', message);
}
}
const payload = {
id: user.id,
Expand All @@ -46,16 +63,15 @@ class Social {
verified: user.verified
};
const token = Helper.generateToken(payload);
return res.status(200).json({
status: 200,
return res.status(status).json({
message,
status,
token,
data: {
firstname, lastname, username, email
},
token
});
}
}

export default Social;

10 changes: 10 additions & 0 deletions src/helpers/passwordgen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const randPass = () => {
let password = '';
const pattern = /[a-z].*[0-9].*/;
while (!pattern.test(password)) {
password = (Math.random() + 1).toString(36).substring(4);
}
return password;
};

export default randPass;
8 changes: 4 additions & 4 deletions src/services/db.service.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable require-jsdoc */
import models from '../models';

const { Stats, Article } = models;
const Models = { Stats, Article };
// const { Stats, Article, user } = models;
// const Models = { Stats, Article, user };
const conditon = where => ({ where });

class StatsService {
static async createStat(where, model) {
return Models[model].create(where);
return models[model].create(where);
}

static async getStat(where, model) {
return Models[model].findAll(conditon(where));
return models[model].findAll(conditon(where));
}
}

Expand Down
18 changes: 14 additions & 4 deletions test/test-mock-social.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,30 @@ describe('Social login tests', () => {
});
});

describe('New social be registered if consents', () => {
describe('Test social signup', () => {
before((done) => {
process.env.facebook = 'facebook_new';
process.env.google = 'google_new';
process.env.twitter = 'twitter_new';
done();
});
it('should notify of none existent of account', (done) => {
it('should notify register new social user', (done) => {
chai.request(server)
.get('/login/facebook')
.end((err, res) => {
if (err) { done(err); }
expect(res.status).to.be.equal(200);
expect(res.body.message).to.contain('account created');
expect(res.status).to.be.equal(201);
expect(res.body.message).to.contain('Account created');
done();
});
});
it('should remind of email update if using twitter', (done) => {
chai.request(server)
.get('/login/twitter')
.end((err, res) => {
if (err) { done(err); }
expect(res.status).to.be.equal(201);
expect(res.body.message).to.contain('and update your email');
done();
});
});
Expand Down

0 comments on commit dbb7430

Please sign in to comment.