Skip to content

Commit

Permalink
added backend
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwolf08 committed Jun 7, 2020
1 parent 4705fec commit 77629ec
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 26 deletions.
20 changes: 12 additions & 8 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import {
OleoScript_700Bold,
} from '@expo-google-fonts/oleo-script';

import { AuthProvider, ThemeProvider } from './contexts';
import { AuthProvider, ThemeProvider, UserProvider } from './contexts';
import NavigationConatiner from './navigation';

// init firebase app

export default function App() {
let [fontsLoaded] = useFonts({
montserrat: Montserrat_400Regular,
Expand All @@ -33,13 +35,15 @@ export default function App() {
return <AppLoading />;
}
return (
<AuthProvider>
<AppearanceProvider>
<ThemeProvider>
<NavigationConatiner />
</ThemeProvider>
</AppearanceProvider>
</AuthProvider>
<UserProvider>
<AuthProvider>
<AppearanceProvider>
<ThemeProvider>
<NavigationConatiner />
</ThemeProvider>
</AppearanceProvider>
</AuthProvider>
</UserProvider>
);
}

Expand Down
69 changes: 68 additions & 1 deletion contexts/auth.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,78 @@
import React, { useState } from 'react';
import axios from 'axios';
import { AsyncStorage } from 'react-native';
import { useUser } from './user';

const AuthContext = React.createContext();

export const AuthProvider = ({ children }) => {
const [isAuthenticated, setisAuthenticated] = useState(false);
const [isLoading, setisLoading] = useState(false);
const { setUser } = useUser();

const login = async ({
enrollmentNumber,
password,
dateOfBirth,
batch,
year,
college,
}) => {
setisLoading(true);
let formData = new FormData();
formData.append('enrll', String(enrollmentNumber));
formData.append('psswd', String(password));
formData.append('dob', String(dateOfBirth));
formData.append('college', String(college));
formData.append('type', 'S');
await AsyncStorage.setItem(
'user',
JSON.stringify({
enrollmentNumber,
password,
dateOfBirth,
batch,
year,
college,
})
);
try {
let attendance = await axios({
method: 'post',
url: 'https://webkiosk-server.azurewebsites.net/api/attendance',
data: formData,
config: { headers: { 'Content-Type': 'multipart/form-data' } },
});
let datewiseattendance = await axios({
method: 'post',
url: 'https://webkiosk-server.azurewebsites.net/api/datewiseattendance',
data: formData,
config: { headers: { 'Content-Type': 'multipart/form-data' } },
});

let user = {
enrollmentNumber,
password,
dateOfBirth,
batch,
year,
college,
attendance: attendance.data,
datewiseattendance: datewiseattendance.data,
loggedIn: true,
};

await AsyncStorage.setItem('user', JSON.stringify(user));
setUser(user);
setisAuthenticated(true);
} catch (e) {
console.log(e);
}
};
return (
<AuthContext.Provider value={{ isAuthenticated, setisAuthenticated }}>
<AuthContext.Provider
value={{ isAuthenticated, setisAuthenticated, login, isLoading }}
>
{children}
</AuthContext.Provider>
);
Expand Down
10 changes: 9 additions & 1 deletion contexts/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { ThemeProvider, useTheme } from './theme';
import { UserProvider, useUser } from './user';
import { AuthProvider, useAuth } from './auth';

export { ThemeProvider, useTheme, AuthProvider, useAuth };
export {
ThemeProvider,
useTheme,
AuthProvider,
useAuth,
UserProvider,
useUser,
};
20 changes: 20 additions & 0 deletions contexts/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { useState } from 'react';

const UserContext = React.createContext();

export const UserProvider = ({ children }) => {
const [user, setUser] = useState(null);

return (
<UserContext.Provider
value={{
user,
setUser,
}}
>
{children}
</UserContext.Provider>
);
};

export const useUser = () => React.useContext(UserContext);
28 changes: 25 additions & 3 deletions navigation/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { useTheme, useAuth } from '../contexts';
import { useTheme, useAuth, useUser } from '../contexts';
import { createStackNavigator } from '@react-navigation/stack';
import { Login, MoreDetails } from '../screens/Auth/Login';
import { Ionicons } from '@expo/vector-icons';
import { Typography, Mixins } from '../styles';

import { HeaderTitle } from '../components';
import AppNavigator from './AppNavigator';
import { AsyncStorage } from 'react-native';
import { AppLoading } from 'expo';

const Stack = createStackNavigator();

export default ({ children, ...restProps }) => {
const { theme } = useTheme();
const { isAuthenticated } = useAuth();
const { setUser } = useUser();
const [loading, setloading] = useState(true);
const { isAuthenticated, setisAuthenticated } = useAuth();

useEffect(() => {
(async () => {
let user = await AsyncStorage.getItem('user');
if (!user) {
setisAuthenticated(false);
setloading(false);
return;
}
user = JSON.parse(user);
if (user?.loggedIn) {
setUser(user);
setisAuthenticated(true);
setloading(false);
}
})();
}, []);
if (loading) return <AppLoading />;
return (
<NavigationContainer {...restProps} theme={theme}>
{isAuthenticated ? (
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
"@react-navigation/native": "^5.5.1",
"@react-navigation/stack": "^5.4.2",
"@use-expo/font": "^2.0.0",
"axios": "^0.19.2",
"expo": "~37.0.3",
"expo-ads-admob": "~8.1.0",
"expo-asset": "~8.1.5",
"expo-camera": "~8.2.0",
"expo-constants": "~9.0.0",
"expo-font": "~8.1.0",
"expo-image-picker": "~8.1.0",
"expo-local-authentication": "~9.0.0",
"expo-web-browser": "~8.2.1",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
Expand Down
3 changes: 2 additions & 1 deletion screens/App/TimeTable/TimeTable.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from 'react';
import { StyleSheet, Text, View, ScrollView } from 'react-native';
import { useTheme } from '../../../contexts';
import { useTheme, useUser } from '../../../contexts';
import { Mixins, Typography } from '../../../styles';
const TimeTable = () => {
const {
theme: {
colors: { background, card, text, primary, black },
},
} = useTheme();
let { user } = useUser();
return (
<ScrollView>
<Text style={[styles.title, { color: text }]}>TimeTable</Text>
Expand Down
18 changes: 12 additions & 6 deletions screens/Auth/Login/Login.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React, { useState, useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import {
StatusBar,
StyleSheet,
Text,
View,
StatusBar,
TextInput,
TouchableOpacity,
View,
Keyboard,
} from 'react-native';

import { Typography, Mixins } from '../../../styles';
import { useTheme } from '../../../contexts';
import { Mixins, Typography } from '../../../styles';

const Login = ({ navigation }) => {
const {
Expand Down Expand Up @@ -84,7 +84,13 @@ const Login = ({ navigation }) => {
</TouchableOpacity>
<TouchableOpacity
disabled={disabled}
onPress={() => navigation.navigate('moredetails')}
onPress={() => {
Keyboard.dismiss();
navigation.navigate('moredetails', {
enrollmentNumber,
password,
});
}}
style={[
styles.logInButton,
{
Expand Down
31 changes: 25 additions & 6 deletions screens/Auth/Login/MoreDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import {
StatusBar,
TextInput,
TouchableOpacity,
ActivityIndicator,
Keyboard,
} from 'react-native';

import { Typography, Mixins } from '../../../styles';
import { useTheme, useAuth } from '../../../contexts';

const MoreDetails = () => {
const MoreDetails = ({ route }) => {
const {
theme: { colors },
} = useTheme();
const { setisAuthenticated } = useAuth();
const { login, isLoading } = useAuth();

// params from login screen
const { enrollmentNumber, password } = route.params;

//define states
const [dateOfBirth, setDateOfBirth] = useState(null);
Expand Down Expand Up @@ -106,7 +111,17 @@ const MoreDetails = () => {
/>
<TouchableOpacity
disabled={disabled}
onPress={() => setisAuthenticated(true)}
onPress={() => {
Keyboard.dismiss();
login({
enrollmentNumber,
password,
dateOfBirth,
batch,
year,
college,
});
}}
style={[
styles.logInButton,
{
Expand All @@ -115,9 +130,13 @@ const MoreDetails = () => {
},
]}
>
<Text style={[styles.logInText, { color: colors.text }]}>
Continue
</Text>
{isLoading ? (
<ActivityIndicator color='white' />
) : (
<Text style={[styles.logInText, { color: colors.text }]}>
Continue
</Text>
)}
</TouchableOpacity>
</View>
</View>
Expand Down
Loading

0 comments on commit 77629ec

Please sign in to comment.