public async Task <RefreshResponseModel> UpdateAsync(Guid refresh)
        {
            var session = await _context.Sessions
                          .Include(x => x.User)
                          .FirstOrDefaultAsync(x => x.RefreshToken == refresh);

            if (session == null)
            {
                throw new ApiException("Невереный токен", 400);
            }

            if (session.ExpiresIn < DateTime.UtcNow)
            {
                throw new ApiException("Токен истек", 400);
            }

            session.RefreshToken = Guid.NewGuid();
            session.ExpiresIn    = DateTime.UtcNow.AddDays(EXPIRED_DAYS);
            session.UpdatedAt    = DateTime.UtcNow;

            await _context.SaveChangesAsync();

            string role  = (await _userManager.GetRolesAsync(session.User)).First();
            var    token = JWTHelper.Create(session.UserId, role, out long expiresIn);

            return(new RefreshResponseModel {
                RefreshToken = session.RefreshToken,
                AccessToken = token,
                ExpiresIn = expiresIn
            });
        }
Exemplo n.º 2
0
        public async Task <LoginResponseModel> RegisterAsync(RegisterModel model, string role)
        {
            if (model.Password != model.ConfirmPassword)
            {
                throw new ApiException("Пароли не совпадают", 400);
            }

            ApplicationUser user = _mapper.Map <ApplicationUser>(model);

            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                result = await _userManager.AddToRoleAsync(user, role);

                if (result.Succeeded)
                {
                    var token   = JWTHelper.Create(user.Id, role, out long expiresIn);
                    var refresh = await _sessionService.CreateAsync(user.Id);

                    await _connectionService.AddAsync(user.Id, model.PlayerId);

                    await _statisticsService.UpdateRegistersCountAsync();

                    return(new LoginResponseModel(user, token, refresh, expiresIn, Roles.CLIENT));
                }
            }

            throw new ApiException(result.Errors.First().Description, 400);
        }
Exemplo n.º 3
0
        public async Task <AdminLoginResponseModel> LogInAsync(AdminLogInRequestModel model)
        {
            await _sessionService.DeleteAsync();

            ApplicationUser user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                throw new ApiException("Неверный email", 400);
            }

            bool ok = await _userManager.CheckPasswordAsync(user, model.Password);

            if (!ok)
            {
                throw new ApiException("Неверный пароль", 400);
            }

            string role    = (await _userManager.GetRolesAsync(user)).First();
            string token   = JWTHelper.Create(user.Id, role, out long expiresIn);
            Guid   refresh = await _sessionService.CreateAsync(user.Id);

            if (model is LoginRequestModel clientModel)
            {
                await _connectionService.AddAsync(user.Id, clientModel.PlayerId);
            }

            return(new AdminLoginResponseModel(user, token, refresh, expiresIn, role, user.ManagedRestaurantId));
        }
Exemplo n.º 4
0
        public async Task <AuthResponse> Post([FromBody] AuthRequest authRequest)
        {
            var response           = new AuthResponse();
            var verifyUserResponse = await new Core.APICaller().PostAsync <VerifyUserResponse>(configuration["ApiUrls:User"], authRequest);

            if (verifyUserResponse.Authenticated)
            {
                response.Authenticated = true;
                response.TokenContent  = JWTHelper.Create(configuration, verifyUserResponse.UserName, verifyUserResponse.UserFullName);
            }
            return(response);
        }