Store data on the device

Asked

Viewed 106 times

0

I am developing an APP with React-Native and has a problem that I am looking to solve, I make a communication with an API and it returns me a token for consumption of services when authenticating the user, I need to save this token from the user’s device to prevent it from logging in every time I open the app, someone can help me with such a question?

1 answer

0


In my projects I create an initial Activity with start name, in it I check if there is something saved in the cellular memory to do the automatic login, note my example:

public class start extends AppCompatActivity {

String login, senha;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.autenticacao_start);

    read(new View(this));
}

public void read(View view) {
    try {
        FileInputStream fin = openFileInput("nome_do_save");
        int c;
        String temp = "";
        while ((c = fin.read()) != -1) {
            temp = temp + Character.toString((char) c);
        }
        login = temp.split(";separador;")[0];
        senha = temp.split(";separador;")[1];
        logar();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        //Em caso de erro ou não existir eu movo o cliente para tela de login
        Intent it = new Intent(start.this, login.class);
        startActivity(it);
        finish();
        e.printStackTrace();
    }
}

public void logar() {
 //Sua lógica de login
}
}

This is how to read, now to save on the login screen I use:

public class login extends AppCompatActivity {

EditText editEmail1, editSenha1;
Button btnLogar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.autenticacao_login);

    editEmail1 = (EditText)findViewById(R.id.editEmail1);
    editSenha1 = (EditText)findViewById(R.id.editSenha1);
    btnLogar = (Button)findViewById(R.id.btnLogar);


    btnLogar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //chama método logar, se este retornar retorna que existe o usuário então eu chamo o save();
        }
    });
}

public void save(View view){
   String dados;
    dados = editEmail1.getText().toString() + ";wotonbest;" + editSenha1.getText().toString();
    try {
        FileOutputStream fOut = openFileOutput("nome_do_save",MODE_PRIVATE);
        fOut.write(dados.getBytes());
        fOut.close();

        Toast.makeText(getApplicationContext(), "Logado com sucesso!", Toast.LENGTH_SHORT).show();

        Intent it = new Intent(login.this, Main.class);
        startActivity(it);
        finish();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        //Toast.makeText(getApplicationContext(), "ERRO", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.