Login.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import React, { useState } from 'react';
  2. import { ImageBackground, StyleSheet, View, TouchableOpacity, TextInput, Dimensions, Alert, } from 'react-native';
  3. import { ActivityIndicator, Text, } from 'react-native-paper'
  4. import 'react-native-get-random-values';
  5. import { Globals } from '../globals'
  6. import { Controller, useForm } from 'react-hook-form';
  7. import { authorize } from 'react-native-app-auth';
  8. const Login = ({navigation}) => {
  9. const config = {
  10. issuer: 'http://10.0.2.2:8000',
  11. clientId: 'mobileapp',
  12. redirectUrl: 'mobile.clicknpush://oidc-callback',
  13. scopes: ['openid'],
  14. dangerouslyAllowUnsecureHttpRequests: true,
  15. };
  16. const [loading, setLoading] = useState(false);
  17. const { control, handleSubmit, formState: {errors} } = useForm({
  18. mode: 'all'
  19. });
  20. const login = async (credentials) => {
  21. setLoading(true);
  22. try {
  23. const result = await authorize(config);
  24. } catch (error) {
  25. Alert.alert(error);
  26. }
  27. // try {
  28. // const response = await axios({
  29. // method: 'post',
  30. // url: Globals.API_HOST + '/api/login/',
  31. // timeout: 5000,
  32. // data: formData,
  33. // });
  34. // resultMessage = response.statusText;
  35. // } catch (error) {
  36. // resultMessage = error.toString();
  37. // }
  38. // dispatch({ type: "SUBMISSION_RECIEVED"});
  39. // navigation.navigate('RegisterResult', {result: resultMessage});
  40. }
  41. return(
  42. <ImageBackground style={styles.container} source={require('../assets/cover-dark.png')}>
  43. {!loading ?
  44. <View>
  45. <View style={styles.brandContainer}>
  46. <Text style={styles.title} >Welcome back!</Text>
  47. <Text style={styles.subtitle} >Please enter your credentials.</Text>
  48. </View>
  49. <View style={styles.textContainer}>
  50. <Controller
  51. control={control}
  52. render={({ field: { onChange, value }}) => (
  53. <TextInput
  54. mode="outlined"
  55. style={[styles.textInput, errors.identifier && styles.errorInput]}
  56. placeholder="Username or email"
  57. value={value}
  58. onChangeText={value => onChange(value)}
  59. ></TextInput>
  60. )}
  61. name="identifier"
  62. rules={{ required: true, }} />
  63. {errors.identifier?.type === "required" && <Text style={styles.errorText}>Username or password is required.</Text>}
  64. <Controller
  65. control={control}
  66. render={({ field: { onChange, value }}) => (
  67. <TextInput
  68. mode="outlined"
  69. style={[styles.textInput, errors.password && styles.errorInput]}
  70. placeholder="Password"
  71. value={value}
  72. onChangeText={value => onChange(value)}
  73. ></TextInput>
  74. )}
  75. name="password"
  76. rules={{ required: true, }} />
  77. {errors.password?.type === "required" && <Text style={styles.errorText}>Password is required.</Text>}
  78. </View>
  79. <View>
  80. <TouchableOpacity style={styles.loginBtn} onPress={handleSubmit(login)} >
  81. <Text style={{ color: 'white', fontSize: 20 }}>Login</Text>
  82. </TouchableOpacity>
  83. <Text style={styles.forgotPass}>Forgot password?</Text>
  84. </View>
  85. </View> :
  86. <View style={styles.brandContainer}>
  87. <Text style={[{marginTop: 70, marginLeft: 20}, styles.title]} >Hold on while we sign you in.</Text>
  88. <ActivityIndicator style={{marginTop: 200}} size="large" animating={true} color="red"/>
  89. </View>}
  90. </ImageBackground>
  91. )
  92. }
  93. const styles = StyleSheet.create({
  94. container: {
  95. position: 'absolute',
  96. width: Dimensions.get('window').width,
  97. height: Dimensions.get('window').height,
  98. flex: 1,
  99. flexDirection: 'column',
  100. resizeMode: 'cover',
  101. },
  102. brandContainer: {
  103. height: 100,
  104. marginVertical: 50,
  105. flexDirection: 'column',
  106. justifyContent: 'space-between'
  107. },
  108. title: {
  109. fontFamily: 'RacingSansOne-Regular',
  110. marginHorizontal: 25,
  111. marginTop: 30,
  112. color: 'white',
  113. fontSize: 20
  114. },
  115. subtitle: {
  116. fontFamily: 'RacingSansOne-Regular',
  117. marginHorizontal: 25,
  118. marginTop: 30,
  119. color: 'white',
  120. fontSize: 15
  121. },
  122. textContainer: {
  123. marginHorizontal: 20,
  124. marginBottom: 30,
  125. borderRadius: 50,
  126. },
  127. textInput: {
  128. marginVertical: 7,
  129. paddingLeft: 30,
  130. borderRadius: 50,
  131. overflow: 'hidden',
  132. backgroundColor: 'white',
  133. height: 50
  134. },
  135. errorText: {
  136. alignSelf: 'flex-end',
  137. color: 'red',
  138. fontSize: 15
  139. },
  140. errorInput: {
  141. borderWidth: 2,
  142. borderColor: 'red'
  143. },
  144. loginBtn: {
  145. marginHorizontal: 20,
  146. borderRadius: 50,
  147. justifyContent: 'center',
  148. backgroundColor: '#df3f3f',
  149. margin: 5,
  150. height: 50,
  151. alignItems: 'center'
  152. },
  153. forgotPass: {
  154. marginTop: 20,
  155. color: 'white',
  156. alignSelf: 'center',
  157. margin: 5,
  158. height: 50,
  159. },
  160. btnContainer: {
  161. marginHorizontal: 20,
  162. borderRadius: 50,
  163. justifyContent: 'center',
  164. backgroundColor: '#df3f3f',
  165. margin: 5,
  166. height: 50,
  167. alignItems: 'center'
  168. },
  169. })
  170. export default Login;