Skip to content
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
23 changes: 3 additions & 20 deletions template/api.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
const API = require('@ladjs/api');
const Graceful = require('@ladjs/graceful');
const Mongoose = require('@ladjs/mongoose');
const _ = require('lodash');
const ip = require('ip');

const config = require('./config');
const routes = require('./routes');
const i18n = require('./helpers/i18n');
const logger = require('./helpers/logger');
const passport = require('./helpers/passport');
const apiConfig = require('./config/api');

const api = new API({
routes: routes.api,
logger,
i18n,
passport
});
const api = new API(apiConfig);

if (!module.parent) {
const mongoose = new Mongoose(
_.merge(
{
logger
},
api.config.mongoose,
config.mongoose
)
);
const mongoose = new Mongoose({ ...api.config.mongoose, logger });

const graceful = new Graceful({
mongooses: [mongoose],
Expand Down
40 changes: 25 additions & 15 deletions template/app/controllers/api/v1/users.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
const Boom = require('@hapi/boom');
const _ = require('lodash');
const isSANB = require('is-string-and-not-blank');
const { select } = require('mongoose-json-select');

const sendVerificationEmail = require('../../../../helpers/send-verification-email');
const config = require('../../../../config');
const { Users } = require('../../../models');

async function create(ctx) {
const { body } = ctx.request;

if (!isSANB(body.password))
return ctx.throw(Boom.badRequest(ctx.translate('INVALID_PASSWORD')));
return ctx.throw(Boom.badRequest(ctx.translateError('INVALID_PASSWORD')));

// register the user
const query = { email: body.email };
const query = { email: body.email, locale: ctx.locale };
query[config.userFields.hasVerifiedEmail] = false;
query[config.userFields.hasSetPassword] = true;
query[config.userFields.pendingRecovery] = false;
const user = await Users.register(query, body.password);
query[config.lastLocaleField] = ctx.locale;

ctx.state.user = await Users.register(query, body.password);

// send a verification email
await user.sendVerificationEmail();
ctx.state.user = await sendVerificationEmail(ctx);

// send the response
const object = select(user.toObject(), Users.schema.options.toJSON.select);
object[config.userFields.apiToken] = user[config.userFields.apiToken];
const object = ctx.state.user.toObject();
object[config.userFields.apiToken] =
ctx.state.user[config.userFields.apiToken];
ctx.body = object;
}

async function retrieve(ctx) {
// since we already have the user object
// just send it over as a response
ctx.body = ctx.state.user;
ctx.body = ctx.state.user.toObject();
}

async function update(ctx) {
const { body } = ctx.request;

ctx.state.user.email = body.email;
ctx.state.user[config.passport.fields.givenName] =
body[config.passport.fields.givenName];
ctx.state.user[config.passport.fields.familyName] =
body[config.passport.fields.familyName];
ctx.state.user.avatar_url = body.avatar_url;
if (_.isString(body.email)) ctx.state.user.email = body.email;

if (_.isString(body[config.passport.fields.givenName]))
ctx.state.user[config.passport.fields.givenName] =
body[config.passport.fields.givenName];

if (_.isString(body[config.passport.fields.familyName]))
ctx.state.user[config.passport.fields.familyName] =
body[config.passport.fields.familyName];

if (_.isString(body.avatar_url)) ctx.state.user.avatar_url = body.avatar_url;

ctx.body = await ctx.state.user.save();
ctx.state.user = await ctx.state.user.save();
ctx.body = ctx.state.user.toObject();
}

module.exports = { create, retrieve, update };
8 changes: 4 additions & 4 deletions template/app/controllers/web/admin/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ async function list(ctx) {

async function retrieve(ctx) {
ctx.state.result = await Users.findById(ctx.params.id);
if (!ctx.state.result) throw new Error(ctx.translate('INVALID_USER'));
if (!ctx.state.result) throw ctx.translateError('INVALID_USER');
await ctx.render('admin/users/retrieve');
}

async function update(ctx) {
const user = await Users.findById(ctx.params.id);
if (!user) throw new Error(ctx.translate('INVALID_USER'));
if (!user) throw ctx.translateError('INVALID_USER');
const { body } = ctx.request;

user[config.passport.fields.givenName] =
Expand Down Expand Up @@ -68,7 +68,7 @@ async function update(ctx) {

async function remove(ctx) {
const user = await Users.findById(ctx.params.id);
if (!user) throw new Error(ctx.translate('INVALID_USER'));
if (!user) throw ctx.translateError('INVALID_USER');
await user.remove();
ctx.flash('custom', {
title: ctx.request.t('Success'),
Expand All @@ -86,7 +86,7 @@ async function remove(ctx) {

async function login(ctx) {
const user = await Users.findById(ctx.params.id);
if (!user) throw new Error(ctx.translate('INVALID_USER'));
if (!user) throw ctx.translateError('INVALID_USER');

ctx.logout();

Expand Down
Loading