| | 1 | | using System.IdentityModel.Tokens.Jwt; |
| | 2 | | using Syki.Back.Features.Cross.GenerateJWT; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Features.Academic.CrossLogin; |
| | 5 | |
|
| 8 | 6 | | public class CrossLoginService(SykiDbContext ctx, GenerateJWTService service) : IAcademicService |
| | 7 | | { |
| | 8 | | public async Task<OneOf<CrossLoginOut, SykiError>> Login(Guid institutionId, CrossLoginIn data) |
| | 9 | | { |
| 8 | 10 | | var targetUser = await ctx.Users.AsNoTracking() |
| 8 | 11 | | .Where(c => c.InstitutionId == institutionId && c.Id == data.TargetUserId) |
| 8 | 12 | | .FirstOrDefaultAsync(); |
| | 13 | |
|
| 12 | 14 | | if (targetUser == null) return new UserNotFound(); |
| | 15 | |
|
| 4 | 16 | | var jwt = await service.Generate(targetUser.Email); |
| 4 | 17 | | var claims = new JwtSecurityToken(jwt).Claims.ToList(); |
| | 18 | |
|
| 4 | 19 | | return new CrossLoginOut |
| 4 | 20 | | { |
| 4 | 21 | | AccessToken = jwt, |
| 16 | 22 | | Name = claims.First(x => x.Type == "name").Value, |
| 20 | 23 | | Email = claims.First(x => x.Type == "email").Value, |
| 8 | 24 | | Id = Guid.Parse(claims.First(x => x.Type == "sub").Value), |
| 12 | 25 | | Role = Enum.Parse<UserRole>(claims.First(x => x.Type == "role").Value), |
| 4 | 26 | | }; |
| 8 | 27 | | } |
| | 28 | | } |