django and react native expo axios api username and password
react native code
import { useState, useEffect } from "react";
import axios from "axios";
import { TouchableOpacity, StyleSheet, View } from 'react-native'
import { Text } from 'react-native-paper'
import Background from '../components/Background'
import Logo from '../components/Logo'
import Header from '../components/Header'
import Button from '../components/Button'
import TextInput from '../components/TextInput'
import BackButton from '../components/BackButton'
import { theme } from '../core/theme'
import { emailValidator } from '../helpers/emailValidator'
import { passwordValidator } from '../helpers/passwordValidator'
export default function LoginScreen({ navigation }) {
const [email, setEmail] = useState({ value: '', error: '' })
const [password, setPassword] = useState({ value: '', error: '' })
//1
const [students, setStudents] = useState([])
useEffect(()=>{
async function getAllStudent() {
try {
const headerdata = { email: 'sa@g.com',password:'123' };
const response = await axios.post('http://10.0.2.2:8000/api/logincheck',headerdata)
console.log(response.data)
setStudents(response.data)
} catch (error) {
console.log(error)
}
}
getAllStudent()
}, [])
//1
const onLoginPressed = () => {
alert('hello 1')
const emailError = emailValidator(email.value)
const passwordError = passwordValidator(password.value)
if (emailError || passwordError) {
setEmail({ ...email, error: emailError })
setPassword({ ...password, error: passwordError })
if(email.value=='sa@g.com' && password.value=='123')
{
alert('login success');
}
else
{
alert('login failed');
}
return
}
navigation.reset({
index: 0,
routes: [{ name: 'Dashboard' }],
})
}
return (
<Background>
<BackButton goBack={navigation.goBack} />
<Logo />
<Header>Welcome back.</Header>
<TextInput
label="Email"
returnKeyType="next"
value={email.value}
onChangeText={(text) => setEmail({ value: text, error: '' })}
error={!!email.error}
errorText={email.error}
autoCapitalize="none"
autoCompleteType="email"
textContentType="emailAddress"
keyboardType="email-address"
/>
<TextInput
label="Password"
returnKeyType="done"
value={password.value}
onChangeText={(text) => setPassword({ value: text, error: '' })}
error={!!password.error}
errorText={password.error}
secureTextEntry
/>
<View style={styles.forgotPassword}>
<TouchableOpacity
onPress={() => navigation.navigate('ResetPasswordScreen')}
>
<Text style={styles.forgot}>Forgot your password?</Text>
</TouchableOpacity>
</View>
<Button mode="contained" onPress={onLoginPressed}>
Login
</Button>
<View style={styles.row}>
<Text>Don’t have an account? </Text>
<TouchableOpacity onPress={() => navigation.replace('RegisterScreen')}>
<Text style={styles.link}>Sign up</Text>
</TouchableOpacity>
</View>
</Background>
)
}
const styles = StyleSheet.create({
forgotPassword: {
width: '100%',
alignItems: 'flex-end',
marginBottom: 24,
},
row: {
flexDirection: 'row',
marginTop: 4,
},
forgot: {
fontSize: 13,
color: theme.colors.secondary,
},
link: {
fontWeight: 'bold',
color: theme.colors.primary,
},
})
---------------------------------------------------django views.py
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from django.http import JsonResponse
@csrf_exempt
def logincheck(request):
if request.method == 'POST':
title = request.POST.get('email')
dnote = request.POST.get('password')
#dnote.objects.create(
# email=email,
# password=password,
#)
return JsonResponse({"status": 'Success'})
----------------------------------------------------------------------
output browser
http://127.0.0.1:8000/api/logincheck
{"status": "Success"}