Skip to content

Commit fece45a

Browse files
author
Kashish Grover
committed
creat basic auth flow using asyncstorage
1 parent 6b64cc8 commit fece45a

File tree

2 files changed

+119
-39
lines changed

2 files changed

+119
-39
lines changed

components/Header.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Header = ({ navigation, title, subtitle }) => (
88
<TouchableOpacity style={styles.backButton} onPress={() => navigation.goBack()}>
99
<Ionicons name="ios-arrow-back" size={24} color="purple" />
1010
</TouchableOpacity>
11-
<View style={styles.textContainer}>
11+
<View>
1212
{!!title && (
1313
<Text style={styles.title}>
1414
{title}
@@ -37,8 +37,6 @@ const styles = StyleSheet.create({
3737
paddingVertical: 8,
3838
left: 0,
3939
},
40-
textContainer: {
41-
},
4240
title: {
4341
fontWeight: '500',
4442
fontSize: 16,

screens/LoginScreen.js

Lines changed: 118 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,127 @@ import {
77
StyleSheet,
88
Image,
99
View,
10+
AsyncStorage,
1011
} from 'react-native';
1112

1213
const AwesomeImage = require('../assets/icon.png');
1314

15+
const USERNAME = 'admin';
16+
const PASSWORD = 'password';
17+
1418
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+
}
1650

1751
render() {
52+
const { isLoggedIn, currentUser } = this.state;
53+
1854
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>
54121
);
55122
}
56123
}
57124

58125
export default LoginScreen;
59126

60127
const styles = StyleSheet.create({
128+
container: {
129+
flex: 1,
130+
},
61131
form: {
62132
flex: 1,
63133
paddingTop: 40,
@@ -89,4 +159,16 @@ const styles = StyleSheet.create({
89159
buttonText: {
90160
color: 'white',
91161
},
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+
},
92174
});

0 commit comments

Comments
 (0)