const express = require('express') const router = express.Router() const auth = require('../session/auth') // const jwt = require('jsonwebtoken') module.exports = function (pCtx) { const services = require('./services')(pCtx) /** * @api {post} /session/login User Login * @apiName post:/session/login * @apiVersion 0.0.1 * @apiGroup Session * @apiDescription Api for User login * Credientials must be set in a json body * * @apiParam {String} user Username for login * @apiParam {String} pass Password for login * * @apiParamExample {json} Login Example: * { * "user": "User name", * "pass": "User plain password", * } * * @apiSuccess {Object} SessionCtx Context of successful login * @apiSuccess {String} SessionCtx.jwtAuth Authentification token to use for get one jwtAccess token - Should be saved locally and set in header authorization field * @apiSuccess {String} SessionCtx.user Just a reminder for user * * @apiError User:NotFound Username or email not found * @apiError User:WrongPassword User found but password does not match */ router.post('/login', services.login) /** * @api {post} /session/access User Acces * @apiName post:/session/access * @apiVersion 0.0.1 * @apiGroup Session * @apiDescription Get an jwtAccess from jwtAuth to access to all API with a short period * Futermore, jwtAuth token TTL is updated * * @apiParam {String} jwtAuth jwtAuth token generated from /session/login * * @apiParamExample {json} jwtAuth: * { * "jwtAuth": "[Authentification JWT token]", * } * * @apiSuccess {Object} SessionCtx Context of successful login * @apiSuccess {String} SessionCtx.jwtAuth Updated jwtAuth token * @apiSuccess {String} SessionCtx.jwtAccess Access token to use for each request to server - Should be saved locally and set in header authorization field * * @apiError User:NotFound Username or email not found * @apiError User:PasswordChanged User found but password does not match from login one * @apiError jwtAuth:Missing jwtAuth is missing in body request * @apiError jwtAuth:InvalidSignature Signature of jwtAuth is invalid * @apiError jwtAuth:JwtExpired Token is expired. A login try to login again * @apiError jwtAuth:CorruptedPayload Token payload is corrupted (server version changes, wrong userAgent ou client ip address) */ // router.post('/access', services.access) router.use(auth(pCtx)) /** * @api {get} /session/me About me * @apiName get:/session/me * @apiVersion 0.0.1 * @apiGroup Session * @apiDescription Get information about user of jwtAccess token * Common case: check if user has still has access rights * * @apiParam {Header} token jwtAccess token * * @apiSuccess {Object} res Response * @apiSuccess {String} res.user User information * * @apiUse SessionError */ router.get('/me', services.me) /** * @api {get} /session/me/filters My filters - Get * @apiName get:/session/me/filters * @apiVersion 0.0.1 * @apiGroup Session * @apiDescription Get all custom filters created by current user * * @apiParam {Header} token jwtAccess token * * @apiSuccess {Object} res Response * @apiSuccess {String} res.data All filters * * @apiUse SessionError */ router.get('/me/filters', services.filtersGet) /** * @api {post} /session/me/filters My filters - Create * @apiName post:/session/me/filters * @apiVersion 0.0.1 * @apiGroup Session * @apiDescription Create new custom filter for current user * * @apiParam (Header) {String} token jwtAccess token * @apiParam (Body) {Object} filter New Filter to create * @apiParam (Body) {String} filter.name Name of filter * @apiParam (Body) {Object} filter.rules Rules to build this filter * * @apiSuccess {Object} res Response * @apiSuccess {String} res.doc New Filter * * @apiUse SessionError */ router.post('/me/filters', services.filtersCreate) return router }