UNPKG

catreact

Version:

Catavolt Core React Components

102 lines (101 loc) 3.79 kB
/** * Created by rburson on 12/23/15. */ "use strict"; var React = require('react'); var catreact_core_1 = require('../core/catreact-core'); /* *************************************************** * Provide Login Support to other components *************************************************** */ exports.CvLogin = React.createClass({ mixins: [catreact_core_1.CvBaseMixin], componentDidMount: function () { /* @TODO - need to work on the AppContext to make the session restore possible */ //this.checkSession() }, checkSession: function () { var _this = this; var sessionContext = this.getSession(); if (sessionContext) { this.catavolt().refreshContext(sessionContext).onComplete(function (appWinDefTry) { if (appWinDefTry.isFailure) { var event_1 = { type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: 'Check session failed', messageObj: appWinDefTry.failure, type: catreact_core_1.CvMessageType.ERROR } }; _this.eventRegistry().publish(event_1, false); } else { _this.setState({ loggedIn: true }); } }); } }, getChildContext: function () { return this.getDefaultChildContext(); }, getDefaultProps: function () { return { loginListeners: [], renderer: null }; }, getSession: function () { var session = sessionStorage.getItem('session'); return session ? JSON.parse(session) : null; }, render: function () { if (this.props.renderer) { return this.props.renderer(this.getChildContext().cvContext, this._getCallbackObject()); } else { return null; } }, removeSession: function () { sessionStorage.removeItem('session'); }, storeSession: function (sessionContext) { sessionStorage.setItem('session', JSON.stringify(sessionContext)); }, _getCallbackObject: function () { var _this = this; return { isLoggedIn: function () { return _this.catavolt().isLoggedIn; }, login: function (gatewayHost, tenantId, clientType, userId, password) { _this._postLogin(_this.catavolt().login(gatewayHost, tenantId, clientType, userId, password)); }, loginDirectly: function (url, tenantId, clientType, userId, password) { _this.catavolt().login(url, tenantId, clientType, userId, password); } }; }, _postLogin: function (loginResult) { var _this = this; loginResult.onComplete(function (appWinDefTry) { if (appWinDefTry.isSuccess) { var eventRegistry = _this.eventRegistry(); var event_2 = { type: catreact_core_1.CvEventType.LOGIN, resourceId: _this.resourceIdForObject(appWinDefTry.success), eventObj: { appWinDef: appWinDefTry.success } }; eventRegistry.publish(event_2, _this.shouldCacheResult()); _this.props.loginListeners.forEach(function (listener) { listener(event_2); }); } else { var event_3 = { type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: 'Login failed', messageObj: appWinDefTry.failure, type: catreact_core_1.CvMessageType.ERROR } }; _this.eventRegistry().publish(event_3, false); } }); } });