2018-07-10 13:40:32 +00:00
import React from 'react' ;
import PropTypes from 'prop-types' ;
2018-10-23 21:39:48 +00:00
import { WebView , Platform , Dimensions } from 'react-native' ;
2018-07-10 13:40:32 +00:00
import { connect } from 'react-redux' ;
2018-10-23 21:39:48 +00:00
import { Navigation } from 'react-native-navigation' ;
2018-07-10 13:40:32 +00:00
import RocketChat from '../lib/rocketchat' ;
import I18n from '../i18n' ;
2018-11-14 21:42:03 +00:00
import { DARK _HEADER } from '../constants/headerOptions' ;
2018-07-10 13:40:32 +00:00
const userAgentAndroid = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1' ;
const userAgent = Platform . OS === 'ios' ? 'UserAgent' : userAgentAndroid ;
@ connect ( state => ( {
server : state . server . server
} ) )
2018-08-31 18:13:30 +00:00
export default class OAuthView extends React . PureComponent {
2018-10-23 21:39:48 +00:00
static options ( ) {
return {
2018-11-14 21:42:03 +00:00
... DARK _HEADER ,
2018-10-23 21:39:48 +00:00
topBar : {
2018-11-14 21:42:03 +00:00
... DARK _HEADER . topBar ,
2018-10-23 21:39:48 +00:00
leftButtons : [ {
id : 'cancel' ,
icon : Platform . OS === 'android' ? { uri : 'back' , scale : Dimensions . get ( 'window' ) . scale } : undefined ,
text : Platform . OS === 'ios' ? I18n . t ( 'Cancel' ) : undefined
} ]
}
} ;
2018-07-10 13:40:32 +00:00
}
static propTypes = {
2018-10-23 21:39:48 +00:00
componentId : PropTypes . string ,
2018-07-10 13:40:32 +00:00
oAuthUrl : PropTypes . string ,
server : PropTypes . string
}
constructor ( props ) {
super ( props ) ;
this . redirectRegex = new RegExp ( ` (?=.*( ${ props . server } ))(?=.*(credentialToken))(?=.*(credentialSecret)) ` , 'g' ) ;
2018-10-23 21:39:48 +00:00
Navigation . events ( ) . bindComponent ( this ) ;
2018-07-10 13:40:32 +00:00
}
2018-10-23 21:39:48 +00:00
navigationButtonPressed = ( { buttonId } ) => {
if ( buttonId === 'cancel' ) {
this . dismiss ( ) ;
2018-07-10 13:40:32 +00:00
}
}
2018-10-23 21:39:48 +00:00
dismiss = ( ) => {
const { componentId } = this . props ;
Navigation . dismissModal ( componentId ) ;
}
2018-07-10 13:40:32 +00:00
login = async ( params ) => {
try {
await RocketChat . login ( params ) ;
} catch ( e ) {
console . warn ( e ) ;
}
}
render ( ) {
2018-10-23 21:39:48 +00:00
const { oAuthUrl } = this . props ;
2018-07-10 13:40:32 +00:00
return (
< WebView
2018-09-25 19:28:42 +00:00
source = { { uri : oAuthUrl } }
2018-07-10 13:40:32 +00:00
userAgent = { userAgent }
onNavigationStateChange = { ( webViewState ) => {
const url = decodeURIComponent ( webViewState . url ) ;
if ( this . redirectRegex . test ( url ) ) {
const parts = url . split ( '#' ) ;
const credentials = JSON . parse ( parts [ 1 ] ) ;
this . login ( { oauth : { ... credentials } } ) ;
2018-10-23 21:39:48 +00:00
this . dismiss ( ) ;
2018-07-10 13:40:32 +00:00
}
} }
/ >
) ;
}
}