<commit_before>'use strict';
var Foxx = require('org/arangodb/foxx');
var controller = new Foxx.Controller(applicationContext);
var Credentials = require('./models/credentials');
var UserProfile = require('./models/userProfile');

controller.activateAuthentication({
  sessionStorageApp: 'sessions',
  cookieName: 'sid',
  type: 'cookie'
});

function getAuthenticator() {
  return Foxx.requireApp('auth').auth;
}

function getUserStorage() {
  return Foxx.requireApp('users').userStorage;
}

function NotAnAdmin() {}
NotAnAdmin.prototype = new Error();

function isAdmin(req) {
  var userData = req.session.get('userData');
  if (!userData || !userData.admin) throw new NotAnAdmin();
}

/** Login route
 *
 * Attempts to log the user in using username and password auth.
 */
controller.post('/login', function(req, res) {
  var credentials = req.params('credentials');
  var user = getAuthenticator().login(
    req.session,
    getUserStorage().resolve(credentials.get('username')),
    credentials.get('password')
  );
  if (user) {
    req.session.save();
    res.json({success: true, user: user.get('userData')});
  } else {
    res.status(403);
    res.json({success: false, error: 'Invalid password or unknown username.'});
  }
})
.bodyParam('credentials', 'Authentication credentials.', Credentials)
.summary('AuthenticuserProfile')
.notes('Attempts to log the user in with username and password.');

/** Registration route
 *
 * Demonstrates creating a new account and logging the user in manually.
 */
controller.post('/register', function(req, res) {
  var credentials = req.params('credentials');
  var userProfile = req.params('profile');
  var userData = userProfile.forDB();
  userData.username = credentials.get('username');
  var users = getUserStorage();
  var user = users.create(userData);
  getAuthenticator().setPassword(user, credentials.get('password'));
  user.save();
  // now log the user in
  req.session.setUser(user);
  req.session.save();
  res.json({success: true, user: user.get('userData'), users: users.list()});
})
.bodyParam('credentials', 'Username and password', Credentials)
.bodyParam('profile', 'User profile data', UserProfile)
.summary('Register')
.notes('Create a new account and log the user in.');

/** Logout route
 *
 * Logs the user out by destroying their session. Always creates a new
 * session to make sure cookies are overwritten.
 */
controller.logout('/logout', function(req, res) {
  res.json({success: true});
})
.summary('De-authenticate')
.notes('Wipes the session data for the active session.');

/** Registered user list
 *
 * Demonstrates fetching the list of usernames from the user storage.
 */
controller.get('/users', function(req, res) {
  res.json({users: getUserStorage().list()});
})
.summary('Registered users')
.notes('Returns a list of all known usernames.');

/** Get current user
 *
 * If the user is logged in, returns the user's userData. Otherwise
 * returns null for the userData.
 */
controller.get('/whoami', function(req, res) {
  if (!req.session.get('uid')) {
    res.json({user: null});
  } else {
    res.json({user: req.session.get('userData') || {}});
  }
})
.summary('Session user status')<commit_msg>Wording.<commit_after>'use strict';
var Foxx = require('org/arangodb/foxx');
var controller = new Foxx.Controller(applicationContext);
var Credentials = require('./models/credentials');
var UserProfile = require('./models/userProfile');

controller.activateAuthentication({
  sessionStorageApp: 'sessions',
  cookieName: 'sid',
  type: 'cookie'
});

function getAuthenticator() {
  return Foxx.requireApp('auth').auth;
}

function getUserStorage() {
  return Foxx.requireApp('users').userStorage;
}

function NotAnAdmin() {}
NotAnAdmin.prototype = new Error();

function isAdmin(req) {
  var userData = req.session.get('userData');
  if (!userData || !userData.admin) throw new NotAnAdmin();
}

/** Login route
 *
 * Attempts to log the user in using username and password auth.
 */
controller.post('/login', function(req, res) {
  var credentials = req.params('credentials');
  var user = getAuthenticator().login(
    req.session,
    getUserStorage().resolve(credentials.get('username')),
    credentials.get('password')
  );
  if (user) {
    req.session.save();
    res.json({success: true, user: user.get('userData')});
  } else {
    res.status(403);
    res.json({success: false, error: 'Invalid password or unknown username.'});
  }
})
.bodyParam('credentials', 'Authentication credentials.', Credentials)
.summary('AuthenticuserProfile')
.notes('Attempts to log the user in with username and password.');

/** Registration route
 *
 * Demonstrates creating a new account and logging the user in manually.
 */
controller.post('/register', function(req, res) {
  var credentials = req.params('credentials');
  var userProfile = req.params('profile');
  var userData = userProfile.forDB();
  userData.username = credentials.get('username');
  var users = getUserStorage();
  var user = users.create(userData);
  getAuthenticator().setPassword(user, credentials.get('password'));
  user.save();
  // now log the user in
  req.session.setUser(user);
  req.session.save();
  res.json({success: true, user: user.get('userData'), users: users.list()});
})
.bodyParam('credentials', 'Username and password', Credentials)
.bodyParam('profile', 'User profile data', UserProfile)
.summary('Register')
.notes('Create a new account and log the user in.');

/** Logout route
 *
 * Logs the user out by deleting their session. Always creates a new
 * session to make sure cookies are overwritten.
 */
controller.logout('/logout', function(req, res) {
  res.json({success: true});
})
.summary('De-authenticate')
.notes('Wipes the session data for the active session.');

/** Registered user list
 *
 * Demonstrates fetching the list of usernames from the user storage.
 */
controller.get('/users', function(req, res) {
  res.json({users: getUserStorage().list()});
})
.summary('Registered users')
.notes('Returns a list of all known usernames.');

/** Get current user
 *
 * If the user is logged in, returns the user's userData. Otherwise
 * returns null for the userData.
 */
controller.get('/whoami', function(req, res) {
  if (!req.session.get('uid')) {
    res.json({user: null});
  } else {
    res.json({user: req.session.get('userData') || {}});
  }
})
.summary('Session user status')
{'fraction_non_alphanumeric': 0.15916575192096596, 'fraction_numerical': 0.0009408812921436412, 'mean_word_length': 4.704830053667263, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1228988', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1891, 'n_words': 531}