<commit_before>import * as Router from 'koa-router';
import * as passport from 'koa-passport';
import inflector from './inflector';
import * as path from 'upath';
import {RestModel} from './RestModel';
/**
 * Reads the model that is requested based on the route
 * and appends model to ctx.state
 * @param {KoaContext} ctx
 * @param {KoaNext} next
 * @param {JSURL} ctx.model - reference attached. DEPRECATED.
 * @param {JSURL} ctx.state.model - reference attached.
 */
async function findmodel (ctx, next) {
	let pmodel = ctx.request.url.split('?')[0].split('/')[1];
	ctx.model = ctx.db[pmodel];
	ctx.state.model = ctx.state.db[pmodel];
	await next();
}
/**
 * Makes a route only accessible if user is logged in
 * and appends model to ctx.state
 * @param {KoaContext} ctx
 * @param {KoaNext} next
 * @return {Object} {status:401, body: null}
 */
async function protect (ctx, next) {
	if (ctx.isAuthenticated()) {
		await next();
	} else {
		await passport.authenticate('bearer', {
			session: false
		}, async function (err, user) {
			/* istanbul ignore if */
			if (err) {
				throw err;
			}
			if (user === false) {
				ctx.body = null;
				ctx.status = 401;
			} else {
				await next();
			}
		})(ctx, next);<commit_msg>fix: Authorization Bearer will login the user<commit_after>import * as Router from 'koa-router';
import * as passport from 'koa-passport';
import inflector from './inflector';
import * as path from 'upath';
import {RestModel} from './RestModel';
/**
 * Reads the model that is requested based on the route
 * and appends model to ctx.state
 * @param {KoaContext} ctx
 * @param {KoaNext} next
 * @param {JSURL} ctx.model - reference attached. DEPRECATED.
 * @param {JSURL} ctx.state.model - reference attached.
 */
async function findmodel (ctx, next) {
	let pmodel = ctx.request.url.split('?')[0].split('/')[1];
	ctx.model = ctx.db[pmodel];
	ctx.state.model = ctx.state.db[pmodel];
	await next();
}
/**
 * Makes a route only accessible if user is logged in
 * and appends model to ctx.state
 * @param {KoaContext} ctx
 * @param {KoaNext} next
 * @return {Object} {status:401, body: null}
 */
async function protect (ctx, next) {
	if (ctx.isAuthenticated()) {
		await next();
	} else {
		await passport.authenticate('bearer', {
			session: false
		}, async function (err, user) {
			/* istanbul ignore if */
			if (err) {
				throw err;
			}
			if (user === false) {
				ctx.body = null;
				ctx.status = 401;
			} else {
				await ctx.login();
			}
			await next();
		})(ctx, next);
{'fraction_non_alphanumeric': 0.15258964143426296, 'fraction_numerical': 0.006374501992031873, 'mean_word_length': 3.4600355239786857, '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': '4411711', 'n_tokens_mistral': 921, 'n_tokens_neox': 851, 'n_words': 271}