Guest User

login

a guest
Jun 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.80 KB | None | 0 0
  1. import React, { Component } from "react";
  2. var AWS=require('aws-sdk/dist/aws-sdk-react-native');
  3. var creds=require('../Components/UserCredentials')
  4. import {GoogleSignin} from 'react-native-google-signin'
  5. import CognitoIdentityServiceProvider from 'aws-sdk/clients/cognitoidentityserviceprovider';
  6. import {
  7. Modal,
  8. ImageBackground,
  9. Image,
  10. ToastAndroid,
  11. View,
  12. BackAndroid,
  13. BackHandler,
  14. StatusBar,
  15. Dimensions,
  16. TextInput,
  17. TouchableHighlight,
  18. WebView,
  19. ActivityIndicator,
  20. AsyncStorage} from "react-native";
  21. import { LoginButton,LoginManager,AccessToken,GraphRequest,GraphRequestManager } from 'react-native-fbsdk'
  22. import LoadingScreen from './LoadingScreen'
  23. import SESCreds from './SESCreds'
  24. import {
  25. Container,
  26. Button,
  27. Spinner,
  28. Text,
  29. Form,
  30. Input,
  31. Item,
  32. Icon,
  33. Label,
  34. Header,
  35. Title,
  36. Body,
  37. Left,
  38. Right,
  39. CheckBox} from "native-base";
  40. import {
  41. CognitoUserPool,
  42. CognitoUserAttribute,
  43. CognitoUser,
  44. AuthenticationDetails
  45. } from 'react-native-aws-cognito-js';
  46. import CognitoCreds from './CognitoCreds'
  47. import { moderateScale } from "react-native-size-matters/lib/scalingUtils";
  48. class Login extends Component {
  49. constructor(){
  50. super();
  51. this.state={
  52. check:true,
  53. isLoading:false,
  54. username:'',
  55. password:'',
  56. verificationcode:'',
  57. newPass:'',
  58. fbdata:{},
  59. googleData:{},
  60. icon:'eye'
  61. }
  62. }
  63. sendMail(to)
  64. {
  65. var creds=new AWS.Credentials(SESCreds)
  66. AWS.config.update({credentials:creds})
  67. AWS.config.update({region:'us-east-1'})
  68. var params = {
  69. Destination: {
  70. ToAddresses: [
  71. to//'ayushmankd@live.com',
  72. ],
  73. },
  74. Message: {
  75. Body: {
  76. Html: {
  77. Charset: "UTF-8",
  78. Data:
  79. '<div style="background-color:lightgrey">'+
  80. '<table style="margin:2%">'+
  81. '<tr>'+
  82. '<td><img height="50" width="100" src="https://s3.amazonaws.com/imagesgiftalife/unnamed.jpg"/></td>'+
  83. '<td style="vertical-align:bottom;padding-left:5em"><i><u><b style="color:green">'+
  84. 'Welcome to the family</b></u></i></td>'+
  85. '</tr>'+
  86. '</table>'+
  87. '<div style="background-color:white;padding:2em">'+
  88. '<b>'+
  89. 'Thank you for using gift a life app. We are happy to welcome you as our ' +
  90. 'newest family member. With giftalife, you get nothing less than excellent '+
  91. 'and unparalleled service at the most reasonable rates ever! We aim at ' +
  92. 'giving people a chance to go green in an innovative way by giving gifts '+
  93. 'in the form of well-designed pots and samplings to their loved ones.'+
  94. '</b>'+
  95. '<p>'+
  96. 'What we basically are trying to implement is that why not start gifting ' +
  97. 'plants to each other on special occasions such as birthdays, anniversaries '+
  98. 'etc rather than those bedsheets,decorative etc we do every now and then ' +
  99. 'which are basically repacked and sent to someone else '+
  100. '</p>'+
  101. '<p>'+
  102. 'We have some other amazing features as well such as plant a tree in which '+
  103. 'team comes to your neighbourhood and help you plant a tree because generally '+
  104. 'people don\'t have the time to do so. One feature or item which I personally '+
  105. 'love is naturally air purifiers I.e we have certain plants which are absorb ' +
  106. 'the pollutants from the air and clean the air around our home,office etc.' +
  107. '</p>'+
  108. '<p style="margin-top:4em">'+
  109. 'To know more about our services and amenities, just refer to the app or ' +
  110. 'check out our Facebook page the link has been provided at the end of this '+
  111. 'email.' +
  112. '</p>'+
  113. '<p>'+
  114. '<b style="color:green">HAPPY PLANTING!!!</b>'+
  115. '</p>'+
  116. '</div>'+
  117. '<div style="margin:2em" align="center">'+
  118. '<a href="https://www.facebook.com/giftalifeindia/?ref=aymt_homepage_panel"><img style="border-radius:3em;padding:10pt" src="http://pre00.deviantart.net/8119/th/pre/i/2012/088/c/6/facebook_logo_by_oguzburaksipsak-d4ubonk.png" height="40" width="40"/></a>'+
  119. '<a href="https://twitter.com/Giftalifeindia?lang=en"><img style="border-radius:3em;padding:10pt" src="http://www.nta.ng/wp-content/uploads/2016/10/twitter-image-service.png" height="40" width="40"/></a>'+
  120. '<a href="https://www.instagram.com/giftalife/"><img style="border-radius:3em;padding:10pt" src="https://www.philosopher.eu/wp-content/uploads/2013/10/Instagram-logo.png" height="40" width="40"/></a>'+
  121. '</div>'+
  122. '</div>'
  123. },
  124. },
  125. Subject: {
  126. Charset: 'UTF-8',
  127. Data: 'Welcome to the Family!!'
  128. }
  129. },
  130. Source: 'info@giftalife.in',//'ayushmankd@gmail.com',
  131. };
  132. }
  133. _responseInfoCallback(error, result) {
  134. if (error) {
  135. alert('Error fetching data: ' + error.toString());
  136. } else {
  137. this.setState({fbdata:result})
  138. this.sendMail(result.email.toString())
  139. AsyncStorage.setItem('user',result.first_name.toString())
  140. this.props.navigation.navigate('Drawer')
  141. this.signUpFb()
  142. }
  143. }
  144. signUpGoogle(){
  145. var credsCognito=new AWS.Credentials(CognitoCreds)
  146. var client=new CognitoIdentityServiceProvider({
  147. region:'us-east-1',
  148. credentials:credsCognito
  149. });
  150. var params = {
  151. UserPoolId: creds.UserPoolId, /* required */
  152. Username: this.state.googleData.id, /* required */
  153. DesiredDeliveryMediums: [
  154. "EMAIL",
  155. ],
  156. MessageAction:'SUPPRESS',
  157. //TemporaryPassword: 'STRING_VALUE',
  158. UserAttributes: [
  159. {
  160. Name: 'email', /* required */
  161. Value: this.state.googleData.email
  162. },
  163. {
  164. Name:'email_verified',
  165. Value:"True"
  166. }
  167. ],
  168. };
  169. client.adminCreateUser(params, function(err, data) {
  170. if (err) console.log(err); // an error occurred
  171. else console.log(data); // successful response
  172. });
  173. }
  174. signUpFb(){
  175. var credsCognito=new AWS.Credentials(CognitoCreds)
  176. var client=new CognitoIdentityServiceProvider({
  177. region:'us-east-1',
  178. credentials:credsCognito
  179. });
  180. var params = {
  181. UserPoolId: creds.UserPoolId, /* required */
  182. Username: this.state.fbdata.id, /* required */
  183. DesiredDeliveryMediums: [
  184. "EMAIL",
  185. /* more items */
  186. ],
  187. MessageAction:'SUPPRESS',
  188. //TemporaryPassword: 'STRING_VALUE',
  189. UserAttributes: [
  190. {
  191. Name: 'email', /* required */
  192. Value: this.state.fbdata.email
  193. },
  194. {
  195. Name:'email_verified',
  196. Value:"True"
  197. }
  198. ],
  199. };
  200. client.adminCreateUser(params, function(err, data) {
  201. if (err)
  202. console.log(err); // an error occurred
  203. else
  204. console.log(data); // successful response
  205. });
  206. }
  207. googleAuth() {
  208. GoogleSignin.signIn().then((user) => {
  209. //console.log(user);
  210. AsyncStorage.setItem('token',user.accessToken)
  211. AsyncStorage.setItem('user',user.name)
  212. this.setState({googleData:user})
  213. this.signUpGoogle();
  214. this.sendMail(user.email.toString())
  215. this.props.navigation.navigate('Drawer');
  216. })
  217. .catch((err) => {
  218. alert(err);
  219. })
  220. .done();
  221. }
  222.  
  223. async setupGoogleSignin() {
  224. try {
  225. await GoogleSignin.hasPlayServices({ autoResolve: true });
  226. await GoogleSignin.configure({
  227. //iosClientId: settings.iOSClientId,
  228. //webClientId: settings.webClientId,
  229. //offlineAccess: false
  230. });
  231. const user = await GoogleSignin.currentUserAsync();
  232. AsyncStorage.setItem('token',user.accessToken)
  233. AsyncStorage.setItem('user',user.name)
  234. this.props.navigation.navigate('Drawer');
  235. }
  236. catch (err) {
  237. console.log(err);
  238. }
  239. }
  240. Check(){
  241. if(this.state.username===''||this.state.password==='')
  242. {
  243. ToastAndroid.show('Username/Password cannot be left Blank!!',ToastAndroid.SHORT)
  244. }
  245. else
  246. {
  247. this.SignIn()
  248. }
  249. }
  250. handleFB(superThis){
  251. LoginManager.logInWithReadPermissions(['public_profile','email']).then(
  252. function(result) {
  253. if (result.isCancelled) {
  254. console.log('Login cancelled');
  255. }
  256. else{
  257. AccessToken.getCurrentAccessToken().then(
  258. (data) => {
  259. const infoRequest = new GraphRequest(
  260. '/me',
  261. {
  262. parameters: {
  263. fields: {
  264. string: 'email,name,first_name,middle_name,last_name' // what you want to get
  265. },
  266. access_token: {
  267. string: data.accessToken.toString() // put your accessToken here
  268. }
  269. }
  270. },
  271. superThis._responseInfoCallback.bind(superThis),
  272. );
  273. new GraphRequestManager().addRequest(infoRequest).start();
  274. AsyncStorage.setItem('token',data.accessToken.toString());
  275. }
  276. )
  277. //superThis.props.navigation.navigate('Drawer')
  278. }
  279. },
  280. function(error){
  281. alert('Login fail with error: ' + error)
  282. },
  283. )
  284. }
  285. SignIn(){
  286. this.setState({isLoading:true})
  287. var userpool=new CognitoUserPool(creds)
  288. const authenticationDetails = new AuthenticationDetails({
  289. Username: this.state.username,
  290. Password: this.state.password
  291. });
  292. const cognitoUser = new CognitoUser({
  293. Username: this.state.username,
  294. Pool: userpool
  295. });
  296. cognitoUser.authenticateUser(authenticationDetails, {
  297. onSuccess: (result) => {
  298. AsyncStorage.setItem('token',result.getAccessToken().getJwtToken().toString())
  299. AsyncStorage.setItem('user',this.state.username)
  300. this.props.navigation.navigate('Drawer')
  301. },
  302. onFailure: (err) => {
  303. this.setState({isLoading:false})
  304. alert(err.toString())
  305. },
  306. mfaRequired: (codeDeliveryDetails) => {
  307. this.setState({isLoading:false})
  308. alert('mfaRequired', codeDeliveryDetails)
  309. }
  310. });
  311. }
  312. componentDidMount(){
  313. this.setupGoogleSignin();
  314. }
  315. async componentWillMount(){
  316. this.setState({isLoading:true})
  317. let token=await AsyncStorage.getItem('token')
  318. if(token!=null)
  319. this.props.navigation.navigate('Drawer')
  320. else
  321. this.setState({isLoading:false})
  322. }
  323. render() {
  324. if(this.state.isLoading)
  325. {
  326. return(
  327. <LoadingScreen />)
  328. }
  329. else{
  330. return (
  331. <Container>
  332. <StatusBar hidden={true} />
  333. <ImageBackground source={require('../../images/login-back.png')}
  334. style={{flex:1}}>
  335. <View style={{justifyContent:'center'}}>
  336. <View>
  337. <Image source={require('../../images/logo.png')} style={{
  338. alignSelf:'center',
  339. height:Dimensions.get('window').height*(85/592),
  340. width:Dimensions.get('window').width*(90/384),
  341. marginTop:'16%',
  342. margin:10,
  343. resizeMode:'stretch',
  344. borderRadius:20}}/>
  345. </View>
  346. <View style={{
  347. marginVertical:10,
  348. marginTop:'13%'
  349. }}>
  350. <Form>
  351. <Item style={{borderBottomWidth:0.5,borderBottomColor:'#fff',borderRadius:10,backgroundColor:'rgba(255,255,255,0.35)',marginRight:35,marginLeft:35}}>
  352. <Icon name='person' style={{marginLeft:10,color:'#fff'}}/>
  353. <Input
  354. placeholder="Username"
  355. placeholderTextColor={'rgba(255,255,255,0.5)'}
  356. style={{fontSize:moderateScale(20),color:'#fff'}}
  357. onChangeText={
  358. inputValue => this.setState({username:inputValue})
  359. }/>
  360. </Item>
  361. <Item style={{marginTop:10,borderRadius:10,backgroundColor:'rgba(255,255,255,0.35)',marginRight:35,marginLeft:35}}>
  362. <Icon name='key' style={{marginLeft:10,color:'#fff'}}/>
  363. <Input
  364. placeholder="Password"
  365. placeholderTextColor={'rgba(255,255,255,0.5)'}
  366. style={{fontSize:moderateScale(20),color:'#fff'}}
  367. secureTextEntry={this.state.icon=='eye'?true:false}
  368. onChangeText={inputValue => this.setState({password:inputValue})}/>
  369. <View style={{paddingRight:5}}>
  370. <Icon
  371. style={{color:'#fff'}}
  372. name={this.state.icon}
  373. onPress={()=>{
  374. if(this.state.icon=='eye')
  375. this.setState({icon:'eye-off'})
  376. else
  377. this.setState({icon:'eye'})
  378. }}/>
  379. </View>
  380. </Item>
  381. </Form>
  382. <TouchableHighlight style={{
  383. marginHorizontal:35,
  384. marginTop:10,
  385. backgroundColor:'#2daae1',
  386. paddingVertical:10,
  387. borderRadius:10}}
  388. onPress={()=>{this.Check()}}
  389. >
  390. <Text style={{alignSelf:'center',color:'#fff',fontSize:moderateScale(20)}}>Sign In</Text>
  391. </TouchableHighlight>
  392. <View style={{marginVertical:30,height:1,width:'50%',borderBottomWidth:0.5,borderBottomColor:'#fff',marginHorizontal:'25%'}}></View>
  393. <TouchableHighlight onPress={()=>this.handleFB(this)} style={{borderRadius:10,alignItems:'center',height:40,backgroundColor:'#43609C',width:'80%',marginHorizontal:'10%'}}>
  394. <View style={{flexDirection:'row'}}>
  395. <Icon name='logo-facebook' style={{paddingVertical:'3%',color:'#fff'}}/>
  396. <Text style={{color:'#fff',fontSize:moderateScale(16),paddingLeft:20,paddingVertical:'3%'}}>Sign in with Facebook</Text>
  397. </View>
  398. </TouchableHighlight>
  399. <TouchableHighlight onPress={this.googleAuth.bind(this)} style={{marginVertical:10,borderRadius:10,alignItems:'center',height:40,backgroundColor:'#d84b37',width:'80%',marginHorizontal:'10%'}}>
  400. <View style={{flexDirection:'row'}}>
  401. <Icon name='logo-google' style={{paddingVertical:'3%',color:'#fff'}}/>
  402. <Text style={{color:'#fff',paddingLeft:20,paddingVertical:'3%',fontSize:moderateScale(16)}}>Sign in with Google</Text>
  403. </View>
  404. </TouchableHighlight>
  405. </View>
  406. <View style={{flexDirection:'row',marginTop:'5%',justifyContent:'space-between',marginHorizontal:'10%'}}>
  407. <Text onPress={()=>{this.props.navigation.navigate('ForgotPassword')}}
  408. style={{fontSize:moderateScale(16),color:'#fff'}}>
  409. Forgot Password?
  410. </Text>
  411. <Text style={{color:'#fff',fontSize:moderateScale(16)}} onPress={()=>this.props.navigation.navigate('SignUp')}>
  412. New here?Sign Up
  413. </Text>
  414. </View>
  415. </View>
  416. </ImageBackground>
  417. </Container>
  418. );
  419. }
  420. }
  421. }
  422.  
  423. export default Login;
Add Comment
Please, Sign In to add comment