-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.cs
109 lines (69 loc) · 2.32 KB
/
Session.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using LIN.Access.Auth.Interfaces;
namespace LIN.Access.Auth;
public sealed class SessionAuth : IISession
{
/// <summary>
/// Modelo de la cuenta.
/// </summary>
public AccountModel Account { get; private set; } = new();
/// <summary>
/// Token.
/// </summary>
public string AccountToken { get; set; }
/// <summary>
/// Si la sesión es activa
/// </summary>
public static bool IsOpen => Instance.Account.Id > 0;
/// <summary>
/// Iniciar sesión.
/// </summary>
/// <param name="user">Usuario.</param>
/// <param name="password">Contraseña.</param>
public static async Task<(SessionAuth? Sesion, Responses Response)> LoginWith(string user, string password)
{
// Cierra la sesión Actual
CloseSession();
// Validación de user
var response = await Controllers.Authentication.Login(user, password);
if (response.Response != Responses.Success)
return (null, response.Response);
// Datos de la instancia
Instance.Account = response.Model;
Instance.AccountToken = response.Token;
return (Instance, Responses.Success);
}
/// <summary>
/// Recargar sesión.
/// </summary>
/// <param name="token">Token de acceso.</param>
/// <returns></returns>
public static async Task<(SessionAuth? Sesion, Responses Response)> LoginWith(string token)
{
// Cierra la sesión Actual
CloseSession();
// Validación de user
var response = await Controllers.Authentication.Login(token);
if (response.Response != Responses.Success)
return (null, response.Response);
// Datos de la instancia
Instance.Account = response.Model;
Instance.AccountToken = response.Token;
return (Instance, Responses.Success);
}
/// <summary>
/// Cierra la sesión.
/// </summary>
public static void CloseSession()
{
Instance.AccountToken = string.Empty;
Instance.Account = new();
}
//==================== Singleton ====================//
private static readonly SessionAuth _instance = new();
private SessionAuth()
{
AccountToken = string.Empty;
Account = new();
}
public static SessionAuth Instance => _instance;
}