Tesis 1.0.0
Loading...
Searching...
No Matches
SessionManager.java
Go to the documentation of this file.
1package com.example.food_front.utils;
2
3import android.content.Context;
4import android.content.SharedPreferences;
5import android.util.Log;
6
7public class SessionManager {
8 private static final String PREF_NAME = "user_session";
9 private static final String KEY_TOKEN = "token";
10 private static final String KEY_EMAIL = "email";
11 private static final String KEY_LAST_INIT_POINT = "last_init_point";
12
13 private SharedPreferences sharedPreferences;
14 private SharedPreferences.Editor editor;
15
16 public SessionManager(Context context) {
17 Context appContext = context.getApplicationContext();
18 sharedPreferences = appContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
19 editor = sharedPreferences.edit();
20 Log.d("SessionManager", "SessionManager creado con contexto: " + appContext);
21 }
22
23 // Guardar el token
24 public void saveToken(String token) {
25 editor.putString(KEY_TOKEN, token);
26 editor.apply();
27 Log.d("auth", "Token guardado despues del login:" + token );
28 }
29
30 // leer el token
31 public String getToken() {
32 return sharedPreferences.getString(KEY_TOKEN, null);
33 }
34
35 // Guardar el email
36 public void saveEmail(String email) {
37 editor.putString(KEY_EMAIL, email);
38 editor.apply();
39 Log.d("SessionManager", "Email guardado en SharedPreferences: " + email);
40 }
41
42 // Leer el email
43 public String getUserEmail() {
44 String email = sharedPreferences.getString(KEY_EMAIL, null);
45 Log.d("SessionManager", "getUserEmail() devuelve: " + email);
46 return email;
47 }
48
49 // Guardar la última URL de pago (init_point)
50 public void saveLastInitPoint(String initPoint) {
51 editor.putString(KEY_LAST_INIT_POINT, initPoint);
52 editor.apply();
53 Log.d("SessionManager", "init_point guardado: " + initPoint);
54 }
55
56 // Obtener la última URL de pago (init_point)
57 public String getLastInitPoint() {
58 return sharedPreferences.getString(KEY_LAST_INIT_POINT, null);
59 }
60
61 // Borrar la session
62 public void clearSession() {
63 editor.clear();
64 editor.apply();
65 }
66
67 // Método logout como alias de clearSession para mayor legibilidad
68 public void logout() {
70 Log.d("auth", "Sesión cerrada correctamente");
71 }
72}