2023-03-15 13:38:06 +00:00
|
|
|
using InnovEnergy.App.Backend.Database;
|
|
|
|
using InnovEnergy.App.Backend.DataTypes;
|
|
|
|
using SQLite;
|
|
|
|
|
|
|
|
namespace InnovEnergy.App.Backend.Relations;
|
|
|
|
|
|
|
|
public class Session : Relation<String, Int64>
|
|
|
|
{
|
|
|
|
public static TimeSpan MaxAge { get; } = TimeSpan.FromDays(7);
|
|
|
|
|
|
|
|
[Unique ] public String Token { get => Left ; init => Left = value;}
|
|
|
|
[Indexed] public Int64 UserId { get => Right; init => Right = value;}
|
|
|
|
[Indexed] public DateTime LastSeen { get; set; }
|
|
|
|
|
2023-03-21 10:49:17 +00:00
|
|
|
[Ignore] public Boolean Valid => DateTime.Now - LastSeen < MaxAge
|
|
|
|
&& (User) is not null;
|
2023-03-15 13:38:06 +00:00
|
|
|
|
|
|
|
[Ignore] public User User => _User ??= Db.GetUserById(UserId)!;
|
|
|
|
|
|
|
|
|
|
|
|
private User? _User;
|
|
|
|
|
|
|
|
[Obsolete("To be used only by deserializer")]
|
|
|
|
public Session()
|
|
|
|
{}
|
|
|
|
|
|
|
|
public Session(User user)
|
|
|
|
{
|
|
|
|
_User = user;
|
|
|
|
Token = CreateToken();
|
|
|
|
UserId = user.Id;
|
|
|
|
LastSeen = DateTime.Now;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String CreateToken()
|
|
|
|
{
|
2023-11-14 11:30:57 +00:00
|
|
|
//var token = new Byte[24];
|
|
|
|
//Random.Shared.NextBytes(token);
|
|
|
|
//return Convert.ToBase64String(token).Replace("/","");
|
|
|
|
return Guid.NewGuid().ToString("N");
|
2023-03-15 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|