@5calls/react-components
Version:
React component library for 5 Calls webapp
183 lines (182 loc) • 7.21 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var auth0base = __importStar(require("auth0-js"));
var jwt_decode_1 = __importDefault(require("jwt-decode"));
var databaseConnection = 'Username-Password-Authentication';
var LoginService = /** @class */ (function () {
function LoginService(auth0Config) {
var _this = this;
this.signup = function (username, password) {
if (username === void 0) { username = ''; }
if (password === void 0) { password = ''; }
username = username || '';
password = password || '';
return new Promise(function (resolve, reject) {
try {
_this.auth0.redirect.signupAndLogin({
connection: databaseConnection,
email: username,
password: password
}, function (error) {
if (error) {
console.error('Auth0 LoginService.signup() error', error);
var results = error.description;
reject(results);
}
else {
resolve('');
}
});
}
catch (error) {
reject(error);
}
});
};
this.twitterLogin = function () {
if (_this.popup) {
// @ts-ignore: additional required parameters defined in Auth0 d.ts file is not used here
_this.auth0.popup.authorize({
connection: 'twitter',
}, function (err, authResult) {
// handled in handleAuthentication
});
}
else {
_this.auth0.authorize({
connection: 'twitter'
});
}
};
this.facebookLogin = function () {
if (_this.popup) {
// @ts-ignore
_this.auth0.popup.authorize({
connection: 'facebook',
}, function (err, authResult) {
// handled in handleAuthentication
});
}
else {
_this.auth0.authorize({
connection: 'facebook'
});
}
};
this.login = this.login.bind(this);
this.logout = this.logout.bind(this);
this.handleAuthentication = this.handleAuthentication.bind(this);
this.popup = auth0Config.popupAuth;
this.auth0 = new auth0base.WebAuth({
domain: auth0Config.domain,
clientID: auth0Config.clientId,
redirectUri: auth0Config.callbackUri,
audience: auth0Config.audience,
responseType: 'token id_token',
scope: 'openid profile email',
});
}
LoginService.prototype.checkAndRenewSession = function (profile, authToken, force) {
var _this = this;
if (force === void 0) { force = false; }
return new Promise(function (resolve, reject) {
if (profile !== undefined) {
// only act on people who are logged in
var expires = new Date(profile.exp * 1000);
var now = new Date();
if (expires < now || force) {
// try to renew automatically
_this.auth0.checkSession({}, function (error, result) {
if (error !== null) {
// not sure how this might happen, reject and log out in the app
reject(error);
}
else {
// otherwise we get the refreshed details back and update them
var authResponse = _this.decodeAndSetProfile(result);
resolve(authResponse);
}
});
}
else {
// we're good for now, don't do anything
var authResponse = { userProfile: profile, authToken: authToken };
resolve(authResponse);
}
}
else {
reject("no profile for check");
}
});
};
LoginService.prototype.isLoggedIn = function (user) {
if (user && user.idToken) {
return true;
}
return false;
};
LoginService.prototype.login = function (username, password) {
var _this = this;
if (username === void 0) { username = ''; }
if (password === void 0) { password = ''; }
return new Promise(function (resolve, reject) {
try {
_this.auth0.login({ realm: databaseConnection, username: username, password: password }, function (error) {
console.error('Auth0 LoginService.login() error', error);
if (error) {
var results = error.description;
reject(results);
}
else {
return resolve('');
}
});
}
catch (err) {
reject(err);
}
});
};
LoginService.prototype.logout = function () {
// The component using this service should handle
// clearing the user profile from the Redux state
};
LoginService.prototype.handleAuthentication = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.auth0.parseHash(function (error, authResult) {
if (!authResult) {
// tslint:disable-next-line:no-console
console.error('Auth0.parseHash() error', error);
reject(error);
}
else {
var authResponse = _this.decodeAndSetProfile(authResult);
// console.log('LoginService.handleAuthentication called. Auth response', authResponse);
resolve(authResponse);
}
});
});
};
LoginService.prototype.decodeAndSetProfile = function (auth0Hash) {
var userProfile;
var authToken = '';
if (auth0Hash.idToken) {
authToken = auth0Hash.idToken;
userProfile = jwt_decode_1.default(auth0Hash.idToken);
}
return { authToken: authToken, userProfile: userProfile };
};
return LoginService;
}());
exports.LoginService = LoginService;