Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
feat: implement auth.check
Browse files Browse the repository at this point in the history
  • Loading branch information
oureta committed Jan 19, 2016
1 parent 4333ea5 commit 1f0868a
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 9 deletions.
55 changes: 52 additions & 3 deletions dist/auth.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.prelogin = prelogin;
exports.check = check;

var _fetch = require('./lib/fetch');

function prelogin() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var _cookie = require('cookie');

var _cookie2 = _interopRequireDefault(_cookie);

if (!args.login) return false;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function prelogin() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var login = args.login;

if (!login) return false;

return new Promise(function (resolve, reject) {
(0, _fetch.post)('auth/pre-login', { login: login }).then(function (response) {
return response.json();
}).then(function (response) {
if (response.success) {
return resolve(response.results);
}

return reject({ message: response.results });
});
});
}

function check() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var login = args.login;
var password = args.password;

if (!login || !password) return false;

return new Promise(function (resolve, reject) {
var playermeSession = undefined;

(0, _fetch.post)('auth/login', { login: login, password: password }).then(function (response) {
var cookies = _cookie2.default.parse(response.headers.get('set-cookie'));

// get subdomain
var matched = /^https?:\/\/([^\.]+)\./.exec(response.url);
var sessionName = 'playerme_session';

if (matched) {
var subdomain = matched[1];
sessionName = subdomain + '_' + sessionName;
}

playermeSession = cookies[sessionName];

return response.json();
}).then(function (response) {
if (response.success) {
var results = _extends({
playerme_session: playermeSession
}, response.results);

return resolve(results);
}

return reject({ message: response.results });
});
});
Expand Down
4 changes: 1 addition & 3 deletions dist/lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ function fetch(endpoint) {
return (0, _isomorphicFetch2.default)(url, _extends({
headers: JSON_HEADERS,
method: 'GET'
}, config)).then(function (response) {
return response.json();
});
}, config));
}

function post(endpoint, args) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"yargs": "^3.32.0"
},
"dependencies": {
"cookie": "^0.2.3",
"isomorphic-fetch": "^2.2.1"
}
}
45 changes: 43 additions & 2 deletions src/auth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { post } from './lib/fetch';
import Cookie from 'cookie';

export function prelogin(args = {}) {
if (!args.login) return false;

const { login } = args;

if (!login) return false;

return new Promise((resolve, reject) => {
post('auth/pre-login', { login })
.then(response => response.json())
.then((response) => {
if (response.success) {
return resolve(response.results);
Expand All @@ -16,3 +18,42 @@ export function prelogin(args = {}) {
});
});
}

export function check(args = {}) {
const { login, password } = args;

if (!login || !password) return false;

return new Promise((resolve, reject) => {
let playermeSession;

post('auth/login', { login, password })
.then((response) => {
const cookies = Cookie.parse(response.headers.get('set-cookie'));

// get subdomain
const matched = /^https?:\/\/([^\.]+)\./.exec(response.url);
let sessionName = 'playerme_session';

if (matched) {
const subdomain = matched[1];
sessionName = `${subdomain}_${sessionName}`;
}

playermeSession = cookies[sessionName];

return response.json();
}).then((response) => {
if (response.success) {
const results = {
playerme_session: playermeSession,
...response.results
};

return resolve(results);
}

return reject({ message: response.results });
});
});
}
2 changes: 1 addition & 1 deletion src/lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function fetch(endpoint, config = {}) {
method: 'GET',
...config
}
).then(response => response.json());
);
}

export function post(endpoint, args) {
Expand Down
43 changes: 43 additions & 0 deletions test-dist/auth/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

var _mocha = require('mocha');

var _chai = require('chai');

var _auth = require('../../dist/auth');

(0, _mocha.describe)('auth.check', function () {
var nonExistingUsername = process.env.NON_EXISTING_USERNAME;
var username = process.env.USERNAME;
var password = process.env.PASSWORD;

(0, _mocha.it)('should be defined and return false if parameter is not valid', function () {
_chai.assert.ok(_auth.check);
_chai.assert.ok(!(0, _auth.check)());
});

(0, _mocha.it)('should return a Promise if login parameter is given', function () {
_chai.assert.ok((0, _auth.check)({ login: username, password: password }) instanceof Promise);
});

(0, _mocha.it)('should resolve to an object with playerme_session if valid', function (done) {
(0, _auth.check)({ login: username, password: password }).then(function (user) {
_chai.assert.typeOf(user, 'object');
_chai.assert.ok(user.id);
_chai.assert.equal(user.username, username);
_chai.assert.ok(user.playerme_session);
done();
}).catch(function () {
done(new Error('env username and or password is incorrect'));
});
});

(0, _mocha.it)('should reject with an error if not valid', function (done) {
(0, _auth.check)({ login: nonExistingUsername, password: password }).then(function () {
done(new Error('env non-existing user exists'));
}).catch(function (error) {
_chai.assert.ok(error.message);
done();
});
});
});
43 changes: 43 additions & 0 deletions test/auth/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, it } from 'mocha';
import { assert } from 'chai';

import { check } from '../../dist/auth';

describe('auth.check', () => {
const nonExistingUsername = process.env.NON_EXISTING_USERNAME;
const username = process.env.USERNAME;
const password = process.env.PASSWORD;

it('should be defined and return false if parameter is not valid', () => {
assert.ok(check);
assert.ok(!check());
});

it('should return a Promise if login parameter is given', () => {
assert.ok(check({ login: username, password }) instanceof Promise);
});

it('should resolve to an object with playerme_session if valid', (done) => {
check({ login: username, password })
.then((user) => {
assert.typeOf(user, 'object');
assert.ok(user.id);
assert.equal(user.username, username);
assert.ok(user.playerme_session);
done();
})
.catch(() => {
done(new Error('env username and or password is incorrect'));
});
});

it('should reject with an error if not valid', (done) => {
check({ login: nonExistingUsername, password })
.then(() => {
done(new Error('env non-existing user exists'));
}).catch((error) => {
assert.ok(error.message);
done();
});
});
});

0 comments on commit 1f0868a

Please sign in to comment.