Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(project): name auth appropriatly and refactor
Browse files Browse the repository at this point in the history
BREAKING CHANGE: AuthService instance renamed to authService and Authentication instance renamed to authentication
  • Loading branch information
doktordirk committed Apr 2, 2016
1 parent c6af3e1 commit 9525976
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 115 deletions.
34 changes: 15 additions & 19 deletions src/app.fetch-httpClient.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class FetchConfig {
constructor(httpClient, clientConfig, authService, config) {
this.httpClient = httpClient;
this.clientConfig = clientConfig;
this.auth = authService;
this.authService = authService;
this.config = config.current;
}

Expand All @@ -27,46 +27,42 @@ export class FetchConfig {
* @return {{request: Function}}
*/
get interceptor() {
let auth = this.auth;
let config = this.config;
let client = this.httpClient;

return {
request(request) {
if (!auth.isAuthenticated() || !config.httpInterceptor) {
request: (request) => {
if (!this.authService.isAuthenticated() || !this.config.httpInterceptor) {
return request;
}
let token = auth.getCurrentToken();
let token = this.authService.getCurrentToken();

if (config.authHeader && config.authToken) {
token = `${config.authToken} ${token}`;
if (this.config.authHeader && this.config.authToken) {
token = `${this.config.authToken} ${token}`;
}

request.headers.set(config.authHeader, token);
request.headers.set(this.config.authHeader, token);

return request;
},
response(response, request) {
response: (response, request) => {
return new Promise((resolve, reject) => {
if (response.ok) {
return resolve(response);
}
if (response.status !== 401) {
return resolve(response);
}
if (!auth.isTokenExpired() || !config.httpInterceptor) {
if (!this.authService.isTokenExpired() || !this.config.httpInterceptor) {
return resolve(response);
}
if (!auth.getRefreshToken()) {
if (!this.authService.getRefreshToken()) {
return resolve(response);
}
auth.updateToken().then(() => {
let token = auth.getCurrentToken();
if (config.authHeader && config.authToken) {
token = `${config.authToken} ${token}`;
this.authService.updateToken().then(() => {
let token = this.authService.getCurrentToken();
if (this.config.authHeader && this.config.authToken) {
token = `${this.config.authToken} ${token}`;
}
request.headers.append('Authorization', token);
return client.fetch(request).then(resolve);
return this.client.fetch(request).then(resolve);
});
});
}
Expand Down
87 changes: 43 additions & 44 deletions src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,60 @@ import {authUtils} from './authUtils';

@inject(Authentication, OAuth1, OAuth2, BaseConfig)
export class AuthService {
constructor(auth, oAuth1, oAuth2, config) {
this.auth = auth;
this.oAuth1 = oAuth1;
this.oAuth2 = oAuth2;
this.config = config.current;
this.client = this.config.client;
this.isRefreshing = false;
constructor(authentication, oAuth1, oAuth2, config) {
this.authentication = authentication;
this.oAuth1 = oAuth1;
this.oAuth2 = oAuth2;
this.config = config.current;
this.client = this.config.client;
this.isRefreshing = false;
}

getMe(criteria) {
if (typeof criteria === 'string' || typeof criteria === 'number') {
criteria = {id: criteria};
}
return this.client.find(this.auth.getProfileUrl(), criteria);
}

getCurrentToken() {
return this.auth.getToken();
}

getRefreshToken() {
return this.auth.getRefreshToken();
return this.client.find(this.authentication.getProfileUrl(), criteria);
}

updateMe(body, criteria) {
if (typeof criteria === 'string' || typeof criteria === 'number') {
criteria = { id: criteria };
}
return this.client.update(this.auth.getProfileUrl(), criteria, body);
return this.client.update(this.authentication.getProfileUrl(), criteria, body);
}

getCurrentToken() {
return this.authentication.getToken();
}

getRefreshToken() {
return this.authentication.getRefreshToken();
}

isAuthenticated() {
let isExpired = this.auth.isTokenExpired();
let isExpired = this.authentication.isTokenExpired();
if (isExpired && this.config.autoUpdateToken) {
if (this.isRefreshing) {
return true;
}
this.updateToken();
}
return this.auth.isAuthenticated();
return this.authentication.isAuthenticated();
}

isTokenExpired() {
return this.auth.isTokenExpired();
return this.authentication.isTokenExpired();
}

getTokenPayload() {
return this.auth.getPayload();
return this.authentication.getPayload();
}

signup(displayName, email, password) {
let signupUrl = this.auth.getSignupUrl();
let signupUrl = this.authentication.getSignupUrl();
let content;

if (typeof arguments[0] === 'object') {
content = arguments[0];
} else {
Expand All @@ -72,7 +73,7 @@ export class AuthService {
return this.client.post(signupUrl, content)
.then(response => {
if (this.config.loginOnSignup) {
this.auth.setTokenFromResponse(response);
this.authentication.setTokenFromResponse(response);
} else if (this.config.signupRedirect) {
window.location.href = this.config.signupRedirect;
}
Expand All @@ -82,55 +83,53 @@ export class AuthService {
}

login(email, password) {
let loginUrl = this.auth.getLoginUrl();
let config = this.config;
let clientId = this.config.clientId;
let content = {};

if (typeof arguments[1] !== 'string') {
content = arguments[0];
} else {
content = {email: email, password: password};
if (clientId) {
content.client_id = clientId;
if (this.config.clientId) {
content.client_id = this.config.clientId;
}
}

return this.client.post(loginUrl, content)
return this.client.post(this.authentication.getLoginUrl(), content)
.then(response => {
this.auth.setTokenFromResponse(response);
if (config.useRefreshToken) {
this.auth.setRefreshTokenFromResponse(response);
this.authentication.setTokenFromResponse(response);
if (this.config.useRefreshToken) {
this.authentication.setRefreshTokenFromResponse(response);
}

return response;
});
}

logout(redirectUri) {
return this.auth.logout(redirectUri);
return this.authentication.logout(redirectUri);
}

updateToken() {
this.isRefreshing = true;
let loginUrl = this.auth.getLoginUrl();
let refreshToken = this.auth.getRefreshToken();
let clientId = this.config.clientId;
let refreshToken = this.authentication.getRefreshToken();
let content = {};

if (refreshToken) {
content = {grant_type: 'refresh_token', refresh_token: refreshToken};
if (clientId) {
content.client_id = clientId;
if (this.config.clientId) {
content.client_id = this.config.clientId;
}

return this.client.post(loginUrl, content)
return this.client.post(this.authentication.getLoginUrl(), content)
.then(response => {
this.auth.setRefreshToken(response);
this.auth.setToken(response);
this.authentication.setRefreshToken(response);
this.authentication.setToken(response);
this.isRefreshing = false;

return response;
}).catch((err) => {
this.auth.removeToken();
this.auth.removeRefreshToken();
this.authentication.removeToken();
this.authentication.removeRefreshToken();
this.isRefreshing = false;

throw err;
Expand All @@ -146,7 +145,7 @@ export class AuthService {

return provider.open(this.config.providers[name], userData || {})
.then(response => {
this.auth.setTokenFromResponse(response, redirect);
this.authentication.setTokenFromResponse(response, redirect);
return response;
});
}
Expand Down
58 changes: 31 additions & 27 deletions src/authUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ export class authUtils {
return typeof value !== 'undefined';
}

static isString(value) {
return typeof value === 'string';
}

static isObject(value) {
return value !== null && typeof value === 'object';
}

static isArray = Array.isArray

static isFunction(value) {
return typeof value === 'function';
}

static isBlankObject(value) {
return value !== null && typeof value === 'object' && !Object.getPrototypeOf(value);
}

static isArrayLike(obj) {
if (obj === null || authUtils.isWindow(obj)) {
return false;
}
}

static isWindow(obj) {
return obj && obj.window === obj;
}

static camelCase(name) {
return name.replace(/([\:\-\_]+(.))/g, function(_, separator, letter, offset) {
return offset ? letter.toUpperCase() : letter;
Expand All @@ -62,21 +90,11 @@ export class authUtils {
return obj;
}

static isString(value) {
return typeof value === 'string';
}

static isObject(value) {
return value !== null && typeof value === 'object';
}

static isArray = Array.isArray

static isFunction(value) {
return typeof value === 'function';
static addTokenPrefix(prefix = '', tokenNme) {
return prefix ? prefix + _ + tokenNme : tokenNme;
}

static joinUrl(baseUrl, url) {
static joinUrl(baseUrl = '', url = '') {
if (/^(?:[a-z]+:)?\/\//i.test(url)) {
return url;
}
Expand All @@ -94,20 +112,6 @@ export class authUtils {
return normalize(joined);
}

static isBlankObject(value) {
return value !== null && typeof value === 'object' && !Object.getPrototypeOf(value);
}

static isArrayLike(obj) {
if (obj === null || authUtils.isWindow(obj)) {
return false;
}
}

static isWindow(obj) {
return obj && obj.window === obj;
}

static extend(dst) {
return baseExtend(dst, slice.call(arguments, 1), false);
}
Expand Down
10 changes: 5 additions & 5 deletions src/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export class Authentication {
}

get refreshTokenName() {
return this.config.refreshTokenPrefix ? this.config.refreshTokenPrefix + '_' + this.config.refreshTokenName : this.config.refreshTokenName;
return authUtils.addTokenPrefix(this.config.refreshTokenPrefix, this.config.refreshTokenName);
}

get tokenName() {
return this.config.tokenPrefix ? this.config.tokenPrefix + '_' + this.config.tokenName : this.config.tokenName;
return authUtils.addTokenPrefix(this.config.tokenPrefix, this.config.tokenName);
}

getLoginRoute() {
Expand All @@ -27,15 +27,15 @@ export class Authentication {
}

getLoginUrl() {
return this.config.baseUrl ? authUtils.joinUrl(this.config.baseUrl, this.config.loginUrl) : this.config.loginUrl;
return authUtils.joinUrl(this.config.baseUrl, this.config.loginUrl);
}

getSignupUrl() {
return this.config.baseUrl ? authUtils.joinUrl(this.config.baseUrl, this.config.signupUrl) : this.config.signupUrl;
return authUtils.joinUrl(this.config.baseUrl, this.config.signupUrl);
}

getProfileUrl() {
return this.config.baseUrl ? authUtils.joinUrl(this.config.baseUrl, this.config.profileUrl) : this.config.profileUrl;
return authUtils.joinUrl(this.config.baseUrl, this.config.profileUrl);
}

getToken() {
Expand Down
10 changes: 5 additions & 5 deletions src/authorizeStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import {Redirect} from 'aurelia-router';

@inject(Authentication)
export class AuthorizeStep {
constructor(auth) {
this.auth = auth;
constructor(authentication) {
this.authentication = authentication;
}

run(routingContext, next) {
let isLoggedIn = this.auth.isAuthenticated();
let loginRoute = this.auth.getLoginRoute();
let isLoggedIn = this.authentication.isAuthenticated();
let loginRoute = this.authentication.getLoginRoute();

if (routingContext.getAllInstructions().some(i => i.config.auth)) {
if (!isLoggedIn) {
return next.cancel(new Redirect(loginRoute));
}
} else if (isLoggedIn && routingContext.getAllInstructions().some(i => i.fragment === loginRoute)) {
let loginRedirect = this.auth.getLoginRedirect();
let loginRedirect = this.authentication.getLoginRedirect();
return next.cancel(new Redirect(loginRedirect));
}

Expand Down
Loading

0 comments on commit 9525976

Please sign in to comment.