102 lines
3.0 KiB
JavaScript
102 lines
3.0 KiB
JavaScript
const express = require('express');
|
|
const passport = require('passport');
|
|
|
|
const AuthService = require('../services/auth');
|
|
const ForbiddenError = require('../services/notifications/errors/forbidden');
|
|
const EmailSender = require('../services/email');
|
|
const wrapAsync = require('../helpers').wrapAsync;
|
|
|
|
const router = express.Router();
|
|
|
|
/**
|
|
* @swagger
|
|
* /api/auth/signin/admin:
|
|
* post:
|
|
* tags: [Auth]
|
|
* summary: Logs admin into the system using private key
|
|
* description: Logs admin into the system using private key
|
|
* requestBody:
|
|
* description: Set valid admin private key
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* required:
|
|
* - key
|
|
* properties:
|
|
* key:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: Successful login
|
|
* 400:
|
|
* description: Invalid key supplied
|
|
*/
|
|
router.post('/signin/admin', wrapAsync(async (req, res) => {
|
|
const payload = await AuthService.signinWithAdminKey(req.body.key);
|
|
res.status(200).send(payload);
|
|
}));
|
|
|
|
router.post('/signin/local', wrapAsync(async () => {
|
|
// Disabled
|
|
throw new ForbiddenError('auth.signinDisabled');
|
|
}));
|
|
|
|
router.post('/signin/code', wrapAsync(async () => {
|
|
// Disabled
|
|
throw new ForbiddenError('auth.signinDisabled');
|
|
}));
|
|
|
|
router.get('/me', passport.authenticate('jwt', {session: false}), (req, res) => {
|
|
if (!req.currentUser || !req.currentUser.id) {
|
|
throw new ForbiddenError();
|
|
}
|
|
|
|
const payload = req.currentUser;
|
|
delete payload.password;
|
|
res.status(200).send(payload);
|
|
});
|
|
|
|
router.put('/password-reset', wrapAsync(async () => {
|
|
throw new ForbiddenError('auth.disabled');
|
|
}));
|
|
|
|
router.put('/password-update', passport.authenticate('jwt', {session: false}), wrapAsync(async (req, res) => {
|
|
const payload = await AuthService.passwordUpdate(req.body.currentPassword, req.body.newPassword, req);
|
|
res.status(200).send(payload);
|
|
}));
|
|
|
|
router.post('/send-email-address-verification-email', passport.authenticate('jwt', {session: false}), wrapAsync(async () => {
|
|
throw new ForbiddenError('auth.disabled');
|
|
}));
|
|
|
|
router.post('/send-password-reset-email', wrapAsync(async () => {
|
|
throw new ForbiddenError('auth.disabled');
|
|
}));
|
|
|
|
router.post('/signup', wrapAsync(async () => {
|
|
throw new ForbiddenError('auth.signupDisabled');
|
|
}));
|
|
|
|
router.put('/profile', passport.authenticate('jwt', {session: false}), wrapAsync(async (req, res) => {
|
|
if (!req.currentUser || !req.currentUser.id) {
|
|
throw new ForbiddenError();
|
|
}
|
|
|
|
await AuthService.updateProfile(req.body.profile, req.currentUser);
|
|
const payload = true;
|
|
res.status(200).send(payload);
|
|
}));
|
|
|
|
router.put('/verify-email', wrapAsync(async () => {
|
|
throw new ForbiddenError('auth.disabled');
|
|
}));
|
|
|
|
router.get('/email-configured', (req, res) => {
|
|
const payload = EmailSender.isConfigured;
|
|
res.status(200).send(payload);
|
|
});
|
|
|
|
router.use('/', require('../helpers').commonErrorHandler);
|
|
|
|
module.exports = router; |