Login.js 4.8 KB

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