Merge branch 'master' of https://github.com/NodeBB/NodeBB
commit
7069735aa2
@ -0,0 +1,30 @@
|
||||
post:
|
||||
tags:
|
||||
- utilities
|
||||
summary: verify login credentials
|
||||
description: |
|
||||
This route accepts a username/password or email/password pair (dependent on forum settings), returning a standard user object if credentials are validated successfully.
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
username:
|
||||
type: string
|
||||
example: admin
|
||||
password:
|
||||
type: string
|
||||
example: '123456'
|
||||
responses:
|
||||
'200':
|
||||
description: credentials successfully validated
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
$ref: ../components/schemas/Status.yaml#/Status
|
||||
response:
|
||||
$ref: ../components/schemas/UserObject.yaml#/UserObjectSlim
|
@ -0,0 +1,51 @@
|
||||
'use strict';
|
||||
|
||||
const user = require('../../user');
|
||||
const authenticationController = require('../authentication');
|
||||
const slugify = require('../../slugify');
|
||||
const helpers = require('../helpers');
|
||||
|
||||
const Utilities = module.exports;
|
||||
|
||||
Utilities.ping = {};
|
||||
Utilities.ping.get = (req, res) => {
|
||||
helpers.formatApiResponse(200, res, {
|
||||
pong: true,
|
||||
});
|
||||
};
|
||||
|
||||
Utilities.ping.post = (req, res) => {
|
||||
helpers.formatApiResponse(200, res, {
|
||||
uid: req.user.uid,
|
||||
received: req.body,
|
||||
});
|
||||
};
|
||||
|
||||
Utilities.login = (req, res) => {
|
||||
res.locals.continueLogin = async (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
|
||||
const userslug = slugify(username);
|
||||
const uid = await user.getUidByUserslug(userslug);
|
||||
let ok = false;
|
||||
try {
|
||||
ok = await user.isPasswordCorrect(uid, password, req.ip);
|
||||
} catch (err) {
|
||||
if (err.message === '[[error:account-locked]]') {
|
||||
return helpers.formatApiResponse(429, res, err);
|
||||
}
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
const userData = await user.getUsers([uid], uid);
|
||||
helpers.formatApiResponse(200, res, userData.pop());
|
||||
} else {
|
||||
helpers.formatApiResponse(403, res);
|
||||
}
|
||||
};
|
||||
res.locals.noScriptErrors = (req, res, err, statusCode) => {
|
||||
helpers.formatApiResponse(statusCode, res, new Error(err));
|
||||
};
|
||||
|
||||
authenticationController.login(req, res);
|
||||
};
|
@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const router = require('express').Router();
|
||||
const middleware = require('../../middleware');
|
||||
const controllers = require('../../controllers');
|
||||
const routeHelpers = require('../helpers');
|
||||
|
||||
const setupApiRoute = routeHelpers.setupApiRoute;
|
||||
|
||||
module.exports = function () {
|
||||
// The "ping" routes are mounted at root level, but for organizational purposes, the controllers are in `utilities.js`
|
||||
|
||||
setupApiRoute(router, 'post', '/login', [middleware.checkRequired.bind(null, ['username', 'password'])], controllers.write.utilities.login);
|
||||
|
||||
return router;
|
||||
};
|
Loading…
Reference in New Issue