Arangodb: "User management" not working

Created on 30 Jun 2017  路  3Comments  路  Source: arangodb/arangodb

my environment running ArangoDB

I'm using the latest ArangoDB of the respective release series:

  • [ ] 2.8
  • [ ] 3.0
  • [x] 3.1
  • [ ] 3.2 Beta1
  • [ ] self-compiled devel branch

On this operating system:

  • [ ] DCOS on

    • [ ] AWS

    • [ ] Azure

    • [ ] own infrastructure

  • [x] Linux

    • [ ] Debian .deb

    • [x] Ubuntu .deb

    • [ ] SUSE .rpm

    • [ ] RedHat .rpm

    • [ ] Fedora .rpm

    • [ ] Gentoo

    • [ ] docker - official docker library

    • [ ] other:

  • [ ] Windows, version:
  • [ ] MacOS, version:

Foxx

Try to copy and paste the User Management example to an own test-service. After uploading the ZIP-file I got the following error:

Services: 3103. failed to invoke module File: /var/tmp/arangod/apps/tmp-1605-1486478250/index.js Cause: AssertionError: No session storage specified at sessionMiddleware (/usr/share/arangodb3/js/server/modules/@arangodb/foxx/sessions/index.js:42:3) at Object. (/var/tmp/arangod/apps/tmp-1605-1486478250/index.js:10:18) at Module._compile (/usr/share/arangodb3/js/common/bootstrap/modules.js:586:8) at Object.Module._extensions..js (/usr/share/arangodb3/js/common/bootstrap/modules.js:604:12) at Module.load (/usr/share/arangodb3/js/common/bootstrap/modules.js:539:36) at FoxxService.run (/usr/share/arangodb3/js/server/modules/@arangodb/foxx/service.js:435:14) at exports.routeService (/usr/share/arangodb3/js/server/modules/@arangodb/foxx/routing.js:153:40) at _validateService (/usr/share/arangodb3/js/server/modules/@arangodb/foxx/manager.js:1087:7) at Object.upgrade (/usr/share/arangodb3/js/server/modules/@arangodb/foxx/manager.js:1418:3) at Middleware. (/usr/share/arangodb3/js/apps/system/_admin/aardvark/APP/foxxes.js:97:29)

It doesn't work with collection-bases and JWT-based session-storage.

1 Question 3 Foxx

All 3 comments

Hello,

You need to modify your main file like that :


'use strict';
const db = require('@arangodb').db;
const joi = require('joi');
const createAuth = require('@arangodb/foxx/auth');
const createRouter = require('@arangodb/foxx/router');
const sessionsMiddleware = require('@arangodb/foxx/sessions');

const auth = createAuth();
const router = createRouter();
const users = module.context.collectionName('Users');

if (module.context.collectionPrefix !== 'internal_') {
  const sessions = sessionsMiddleware({
    storage: module.context.collection('Sessions'),
    transport: ['header', 'cookie']
  });

  module.context.use(sessions);

}
module.context.use(router);

router.get('/whoami', function (req, res) {
    try {
        const user = db._collection(users).document(req.session.uid);
        res.send({username: user.username});
    } catch (e) {
        res.send({username: null});
    }
})
.description('Returns the currently active username.');

router.post('/login', function (req, res) {
    // This may return a user object or null
    const user = db._collection(users).firstExample({
        username: req.body.username
    });
    const valid = auth.verify(
        // Pretend to validate even if no user was found
        user ? user.authData : {},
        req.body.password
    );
    if (!valid) res.throw('unauthorized');

    // Log the user in

    req.session.uid = user._key;
    req.sessionStorage.save(req.session);
    res.send({sucess: true});
})
.body(joi.object({
    username: joi.string().required(),
    password: joi.string().required()
}).required(), 'Credentials')
.description('Logs a registered user in.');

router.post('/logout', function (req, res) {
    if (req.session.uid) {
        req.session.uid = null;
        req.sessionStorage.save(req.session);
    }
    res.send({success: true});
})
.description('Logs the current user out.');

router.post('/signup', function (req, res) {
    const user = req.body;
    try {
        // Create an authentication hash
        user.authData = auth.create(user.password);
        delete user.password;
        console.log(user);
        const meta = db._collection(users).save(user);
        Object.assign(user, meta);
    } catch (e) {
        // Failed to save the user
        // We'll assume the UniqueConstraint has been violated
        res.throw('bad request', 'Username already taken', e);
    }
    // Log the user in
    req.session.uid = user._key;
    req.sessionStorage.save(req.session);
    res.send({success: true});
})
.body(joi.object({
    username: joi.string().required(),
    password: joi.string().required()
}).required(), 'Credentials')
.description('Creates a new user and logs them in.');

@AxelRHD did @julienwikart answer your question? can we close this?

@dothebart sorry... yes, can be closed.

Was this page helpful?
0 / 5 - 0 ratings