简体   繁体   English

如何在具有 Django 后端的 React Native 应用程序上保持用户登录(即使手机关闭)?

[英]How to keep a user logged in (even when phone turns off ) on a React Native app with Django backend?

I am beginning to code with react native and I am trying my first app.我开始使用 react native 进行编码,并且正在尝试我的第一个应用程序。 I am in the process of making the login/signup of the app.我正在制作应用程序的登录/注册过程。 I managed to log the user in but I cannot seem to figure out how to keep the user logged in when I close the app.我设法让用户登录,但我似乎无法弄清楚当我关闭应用程序时如何让用户保持登录状态。 How can I keep a user logged in to the app even when the app is closed?即使应用程序关闭,如何让用户保持登录到应用程序?

This is what I have-这就是我所拥有的——

login.js登录.js

import { StatusBar } from 'expo-status-bar'
import { StyleSheet, Text, View, FlatList, Image, Button, Pressable, ScrollView  } from 'react-native';
import React, {useState, useEffect} from 'react'
import { TextInput } from 'react-native-gesture-handler';






export default function Login(props) {

  const message = props.navigation.getParam('message', null)
  const [ username, setUsername ] = useState("")
  const [ password, setPassword ] = useState("")

  

  const log = () => {

    fetch(`http://192.168.5.223:8000/home/login/`, {
      method: 'POST',
      headers: {
          "Content-Type": 'application/json'
         },
      body: JSON.stringify({ username: username, password: password}),
  })
  .then( res => res.json())
  .then( res => {
    console.log(res)
    
    if (res.valid === true){
      
      if (res.set === true){
        props.navigation.navigate("Home", {"user": username})
      } else {
        props.navigation.navigate("Profile", {"user": username})
      }
      
    } else {
      props.navigation.navigate("Login", {'message': "username/password are incorrect"})
    }
    

  })
  .catch( error => console.log(error))
  


  



  }

  const sign = () => {

    props.navigation.navigate("Signup")

  }

  return (
    <View style={styles.container}>
      <ScrollView style={styles.scroll} >
      <View style={styles.main}>
      <Text style={styles.error}>{message}</Text>  
      <Text style={styles.label}>
        Username: 
      </Text>
      <TextInput style={styles.input} placeholder="Username" 
        onChangeText={ text => setUsername(text)} value={username}
        autoCapitalize={'none'}
         />
      
      <Text style={styles.label}>Password:</Text>
      <TextInput style={styles.input} placeholder="Password" onChangeText={ text => setPassword(text)}
        value={password} secureTextEntry={true} autoCapitalize={'none'}
      />

      
      <Button onPress={ () => log()} title="Login"></Button>
      </View>
      </ScrollView>
      <View style={styles.footer}>
        <Button onPress={ () => sign()} title="Don't have an acount? Sign up now" />
      </View>
      <StatusBar style="auto"/>
    </View>


  )


}

Login.navigationOptions = screenProps => ({
  headerLeft: () => null,
  gestureEnabled: false,
  headerStyle: {
    backgroundColor: 'black'
  },
  headerTintColor: 'white',

})

If you want your a user session to be persisted when the user closes and reopens the app you can use the AsyncStorage API.如果您希望在用户关闭并重新打开应用程序时保留用户 session,您可以使用 AsyncStorage API。

See for instance this question Persistent User Session on React Native App参见例如这个问题Persistent User Session on React Native App

You need to store a token/cookie that will be used by your server to authenticate the user.您需要存储一个令牌/cookie,您的服务器将使用该令牌/cookie 对用户进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 cordova 迁移到 React Native 时如何保持用户登录 - How to keep user logged in when migrating from cordova to react native 如何使用 Django 后端保持用户登录 React 应用程序 - How to keep user logged in in React app with a Django back-end 如何让用户以react-native身份登录(Aka,状态持久性) - How to keep user logged in (Aka, state persistency) in react-native 即使应用程序在后台,如何保持后台计时器在本机反应中运行 - How to keep a background timer running in react native even when app is in the background 如何让用户登录 firebase 7.17.1 并做出反应? - How to keep user logged in with firebase 7.17.1 and react? 如何在 Flask 和 React 中的页面刷新之间保持用户登录 - How to keep user logged in between page refreshes in Flask and React 在 React - Express 中关闭浏览器后如何保持用户登录? - How to keep user logged after browser closing in React - Express? 使用 axios 将当前登录用户从 Django 后端发送到 React 前端 - Send current logged in user from Django backend to React frontend using axios 用户登录React Native后如何更改初始屏幕? - How to change initial screen after user is logged in in react native? 如何在反应原生中仅显示来自firestore的登录用户的帖子? - How to show posts of only the logged in user from firestore in react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM