brings frontend up to speed with flattened users

This commit is contained in:
James
2020-05-18 17:11:34 -04:00
parent 1603243da3
commit 17d25c8d2f
20 changed files with 89 additions and 101 deletions

View File

@@ -1,13 +1,12 @@
require('isomorphic-fetch');
const faker = require('faker');
const { username, password } = require('../tests/credentials');
const { email, password } = require('../tests/credentials');
/**
* @jest-environment node
*/
const config = require('../../demo/payload.config');
const { payload } = require('../../demo/server');
const url = config.serverURL;
@@ -17,7 +16,7 @@ describe('Users REST API', () => {
it('should prevent registering a first user', async () => {
const response = await fetch(`${url}/api/users/first-register`, {
body: JSON.stringify({
username: 'thisuser@shouldbeprevented.com',
email: 'thisuser@shouldbeprevented.com',
password: 'get-out',
}),
headers: {
@@ -32,7 +31,7 @@ describe('Users REST API', () => {
it('should login a user successfully', async () => {
const response = await fetch(`${url}/api/users/login`, {
body: JSON.stringify({
username,
email,
password,
}),
headers: {
@@ -59,7 +58,7 @@ describe('Users REST API', () => {
const data = await response.json();
expect(response.status).toBe(200);
expect(data.username).not.toBeNull();
expect(data.email).not.toBeNull();
});
it('should refresh a token and reset its expiration', async () => {
@@ -84,7 +83,7 @@ describe('Users REST API', () => {
const response = await fetch(`${url}/api/users/forgot-password`, {
method: 'post',
body: JSON.stringify({
username,
email,
}),
headers: {
'Content-Type': 'application/json',
@@ -100,7 +99,7 @@ describe('Users REST API', () => {
it('should allow a user to be created', async () => {
const response = await fetch(`${url}/api/users/register`, {
body: JSON.stringify({
username: `${faker.name.firstName()}@test.com`,
email: `${faker.name.firstName()}@test.com`,
password,
roles: ['editor'],
}),
@@ -114,7 +113,7 @@ describe('Users REST API', () => {
const data = await response.json();
expect(response.status).toBe(201);
expect(data).toHaveProperty('username');
expect(data).toHaveProperty('email');
expect(data).toHaveProperty('roles');
expect(data).toHaveProperty('createdAt');
});

View File

@@ -4,7 +4,7 @@ const defaultUser = {
singular: 'User',
plural: 'Users',
},
useAsTitle: 'username',
useAsTitle: 'email',
auth: {
tokenExpiration: 7200,
},

View File

@@ -6,7 +6,7 @@ const loginResolver = (config, collection) => async (_, args, context) => {
collection,
config,
data: {
username: args.username,
email: args.email,
password: args.password,
},
req: context,

View File

@@ -3,8 +3,8 @@ const { APIError } = require('../../errors');
const forgotPassword = async (args) => {
try {
if (!Object.prototype.hasOwnProperty.call(args.data, 'username')) {
throw new APIError('Missing username.');
if (!Object.prototype.hasOwnProperty.call(args.data, 'email')) {
throw new APIError('Missing email.');
}
let options = { ...args };
@@ -35,7 +35,7 @@ const forgotPassword = async (args) => {
let token = await crypto.randomBytes(20);
token = token.toString('hex');
const user = await Model.findOne({ username: data.username });
const user = await Model.findOne({ email: data.email });
if (!user) return;
@@ -53,7 +53,7 @@ const forgotPassword = async (args) => {
email({
from: `"${config.email.fromName}" <${config.email.fromAddress}>`,
to: data.username,
to: data.email,
subject: 'Password Reset',
html,
});

View File

@@ -30,9 +30,9 @@ const login = async (args) => {
data,
} = options;
const { username, password } = data;
const { email, password } = data;
const user = await Model.findByUsername(username);
const user = await Model.findByUsername(email);
if (!user) throw new AuthenticationError();
@@ -51,7 +51,7 @@ const login = async (args) => {
}
return signedFields;
}, {
username,
email,
});
const token = jwt.sign(

View File

@@ -33,7 +33,7 @@ const resetPassword = async (args) => {
data,
} = options;
const { username } = data;
const { email } = data;
const user = await Model.findOne({
resetPasswordToken: data.token,
@@ -60,7 +60,7 @@ const resetPassword = async (args) => {
}
return signedFields;
}, {
username,
email,
});
const token = jwt.sign(

View File

@@ -10,7 +10,7 @@ module.exports = (config, Model) => {
return new JwtStrategy(opts, async (token, done) => {
try {
const user = await Model.findByUsername(token.username);
const user = await Model.findByUsername(token.email);
return done(null, user);
} catch (err) {
return done(null, false);