feat(login): add local users functionality (#591)

This commit is contained in:
Jakob Ankarhem
2021-01-14 13:03:12 +01:00
committed by GitHub
parent f17fa2a2db
commit 492e19df40
17 changed files with 866 additions and 97 deletions

View File

@@ -6,6 +6,7 @@ import { User } from '../entity/User';
import { hasPermission, Permission } from '../lib/permissions';
import { getSettings } from '../lib/settings';
import logger from '../logger';
import gravatarUrl from 'gravatar-url';
const router = Router();
@@ -19,13 +20,34 @@ router.get('/', async (_req, res) => {
router.post('/', async (req, res, next) => {
try {
const settings = getSettings().notifications.agents.email;
const body = req.body;
const userRepository = getRepository(User);
const passedExplicitPassword = body.password && body.password.length > 0;
const avatar = gravatarUrl(body.email);
if (!passedExplicitPassword && !settings.enabled) {
throw new Error('Email notifications must be enabled');
}
const user = new User({
email: req.body.email,
permissions: req.body.permissions,
avatar: body.avatar ?? avatar,
username: body.username ?? body.email,
email: body.email,
password: body.password,
permissions: body.permissions,
plexToken: '',
userType: body.userType,
});
if (passedExplicitPassword) {
await user?.setPassword(body.password);
} else {
await user?.resetPassword();
}
await userRepository.save(user);
return res.status(201).json(user.filter());
} catch (e) {