@@ -7,57 +7,127 @@ import {
7
7
StyleSheet ,
8
8
Image ,
9
9
View ,
10
+ AsyncStorage ,
10
11
} from 'react-native' ;
11
12
12
13
const AwesomeImage = require ( '../assets/icon.png' ) ;
13
14
15
+ const USERNAME = 'admin' ;
16
+ const PASSWORD = 'password' ;
17
+
14
18
class LoginScreen extends React . Component {
15
- state = { } ;
19
+ state = {
20
+ isLoggedIn : false ,
21
+ currentUser : '' ,
22
+ } ;
23
+
24
+ componentDidMount ( ) {
25
+ this . checkExistingSession ( ) ;
26
+ }
27
+
28
+ handleLogin = ( ) => {
29
+ const enteredUsername = this . usernameRef . _lastNativeText ;
30
+ const enteredPassword = this . passwordRef . _lastNativeText ;
31
+
32
+ if ( enteredPassword === PASSWORD && enteredUsername === USERNAME ) {
33
+ this . setState ( { isLoggedIn : true , currentUser : enteredUsername } ) ;
34
+ AsyncStorage . setItem ( 'currentUser' , enteredUsername ) ;
35
+ }
36
+ }
37
+
38
+ checkExistingSession = async ( ) => {
39
+ const currentUser = await AsyncStorage . getItem ( 'currentUser' ) ;
40
+ if ( currentUser ) {
41
+ this . setState ( { isLoggedIn : true , currentUser } ) ;
42
+ }
43
+ }
44
+
45
+ handleLogout = ( ) => {
46
+ AsyncStorage . removeItem ( 'currentUser' ) ;
47
+ alert ( 'Logged out successfully!' ) ;
48
+ this . setState ( { isLoggedIn : false , currentUser : '' } ) ;
49
+ }
16
50
17
51
render ( ) {
52
+ const { isLoggedIn, currentUser } = this . state ;
53
+
18
54
return (
19
- < KeyboardAvoidingView
20
- style = { styles . form }
21
- behavior = "padding"
22
- keyboardVerticalOffset = { 54 }
23
- >
24
- < Image
25
- source = { AwesomeImage }
26
- style = { styles . image }
27
- />
28
- < TextInput
29
- style = { styles . textInput }
30
- placeholder = "Username"
31
- maxLength = { 10 }
32
- autoCapitalize = "none"
33
- underlineColorAndroid = "transparent"
34
- />
35
- < TextInput
36
- style = { styles . textInput }
37
- placeholder = "Password"
38
- maxLength = { 32 }
39
- secureTextEntry
40
- autoCapitalize = "none"
41
- underlineColorAndroid = "transparent"
42
- />
43
- < View >
44
- < TouchableOpacity
45
- onPress = { ( ) => alert ( 'Login' ) }
46
- style = { styles . button }
47
- >
48
- < Text style = { styles . buttonText } >
49
- Login
50
- </ Text >
51
- </ TouchableOpacity >
52
- </ View >
53
- </ KeyboardAvoidingView >
55
+ < View style = { styles . container } >
56
+ { ! isLoggedIn
57
+ ? (
58
+ < KeyboardAvoidingView
59
+ style = { styles . form }
60
+ behavior = "padding"
61
+ keyboardVerticalOffset = { 54 }
62
+ >
63
+ < Image
64
+ source = { AwesomeImage }
65
+ style = { styles . image }
66
+ />
67
+ < TextInput
68
+ ref = { ( x ) => { this . usernameRef = x ; } }
69
+ style = { styles . textInput }
70
+ placeholder = "Username"
71
+ maxLength = { 10 }
72
+ autoCapitalize = "none"
73
+ underlineColorAndroid = "transparent"
74
+ onSubmitEditing = { ( ) => this . passwordRef . focus ( ) }
75
+ returnKeyType = "next"
76
+ />
77
+ < TextInput
78
+ ref = { ( x ) => { this . passwordRef = x ; } }
79
+ style = { styles . textInput }
80
+ placeholder = "Password"
81
+ maxLength = { 32 }
82
+ secureTextEntry
83
+ autoCapitalize = "none"
84
+ underlineColorAndroid = "transparent"
85
+ onSubmitEditing = { this . handleLogin }
86
+ returnKeyType = "done"
87
+ />
88
+ < View >
89
+ < TouchableOpacity
90
+ onPress = { this . handleLogin }
91
+ style = { styles . button }
92
+ >
93
+ < Text style = { styles . buttonText } >
94
+ Login
95
+ </ Text >
96
+ </ TouchableOpacity >
97
+ </ View >
98
+ </ KeyboardAvoidingView >
99
+ )
100
+ : (
101
+ < View style = { styles . form } >
102
+ < Text style = { styles . welcomeText } >
103
+ { `Welcome ${ currentUser } !` }
104
+ </ Text >
105
+ < Image
106
+ source = { AwesomeImage }
107
+ style = { styles . image }
108
+ />
109
+ < TouchableOpacity
110
+ style = { styles . logoutButton }
111
+ onPress = { this . handleLogout }
112
+ >
113
+ < Text style = { styles . logoutText } >
114
+ Log out
115
+ </ Text >
116
+ </ TouchableOpacity >
117
+ </ View >
118
+ )
119
+ }
120
+ </ View >
54
121
) ;
55
122
}
56
123
}
57
124
58
125
export default LoginScreen ;
59
126
60
127
const styles = StyleSheet . create ( {
128
+ container : {
129
+ flex : 1 ,
130
+ } ,
61
131
form : {
62
132
flex : 1 ,
63
133
paddingTop : 40 ,
@@ -89,4 +159,16 @@ const styles = StyleSheet.create({
89
159
buttonText : {
90
160
color : 'white' ,
91
161
} ,
162
+ welcomeText : {
163
+ fontSize : 16 ,
164
+ } ,
165
+ logoutButton : {
166
+ borderWidth : 1 ,
167
+ borderColor : 'purple' ,
168
+ borderRadius : 4 ,
169
+ padding : 4 ,
170
+ } ,
171
+ logoutText : {
172
+ color : 'purple' ,
173
+ } ,
92
174
} ) ;
0 commit comments