Ejemplo n.º 1
0
        public async Task <CustomResponse <UserResponse> > UpdateUser(UpdateUserModel model)
        {
            var infos = new List <string>();

            var user = _context.AspNetUsers.FirstOrDefault(u => u.Id == model.UserId);

            #region Validate User

            if (user == null)
            {
                infos.Add("User not exist.");

                return(new CustomResponse <UserResponse> {
                    Message = infos
                });
            }
            ;

            #endregion

            user.Fullname     = string.IsNullOrEmpty(model.Fullname) ? user.Fullname : model.Fullname;
            user.MobileNumber = string.IsNullOrEmpty(model.MobileNumber) ? user.MobileNumber : model.MobileNumber;
            user.UpdatedAt    = DateTime.UtcNow;

            _context.Update(user);

            await _context.SaveChangesAsync();

            return(new CustomResponse <UserResponse> {
                Succeed = true,
                Data = _mapper.Map <UserResponse>(user)
            });
        }
Ejemplo n.º 2
0
        public async Task <CustomResponse <string> > SignOut(SignOutModel model)
        {
            var infos = new List <string>();

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

            var userSession = _context.AspNetUserSessions.FirstOrDefault(t => t.UserId == user.Id && t.DeviceName == model.UserAgent);

            if (userSession == null)
            {
                infos.Add("User don't have registered session");

                return(new CustomResponse <string> {
                    Message = infos
                });
            }

            userSession.Validity = false;

            _context.Update(userSession);

            await _context.SaveChangesAsync();

            await _signInManager.SignOutAsync();

            return(new CustomResponse <string> {
                Succeed = true
            });
        }
Ejemplo n.º 3
0
        public async Task <CustomResponse <TokenResource> > GenerateRefreshToken(RefreshTokenGetModel model)
        {
            var userSession = _context.AspNetUserSessions.FirstOrDefault(t => t.UserId == model.UserId && t.DeviceName == model.UserAgent);

            var token = Guid.NewGuid().ToString().Replace("-", "") + "." + model.UserId.Replace("-", "");

            var refreshTokenExpiredMins = _options.Value.RefreshTokenExpiryMins;

            var refreshedToken = await RefreshToken();

            return(new CustomResponse <TokenResource> {
                Succeed = true,
                Data = new TokenResource {
                    Token = refreshedToken.Token,
                    ExpiredAt = refreshedToken.ExpiredAt
                }
            });

            #region Local Methods

            async Task <TokenResource> RefreshToken()
            {
                if (userSession == null)   //Add
                {
                    var refreshToken = await _context.AspNetUserSessions.AddAsync(new AspNetUserSessions {
                        Id             = Guid.NewGuid().ToString(),
                        UserId         = model.UserId,
                        Token          = token,
                        TokenExpiredAt = DateTime.UtcNow.AddMinutes(refreshTokenExpiredMins),
                        Validity       = true,
                        DeviceName     = model.UserAgent,
                        AppVersion     = model.AppVersion,
                        ApiVersion     = model.ApiVersion
                    });

                    await _context.SaveChangesAsync();

                    return(new TokenResource {
                        Token = refreshToken.Entity.Token,
                        ExpiredAt = refreshToken.Entity.TokenExpiredAt
                    });
                }

                //Update
                userSession.Token          = token;
                userSession.TokenExpiredAt = DateTime.UtcNow.AddMinutes(refreshTokenExpiredMins);
                userSession.AppVersion     = model.AppVersion;
                userSession.ApiVersion     = model.ApiVersion;
                userSession.Validity       = true;

                _context.AspNetUserSessions.Update(userSession);

                await _context.SaveChangesAsync();

                return(new TokenResource {
                    Token = token,
                    ExpiredAt = DateTime.UtcNow.AddMinutes(refreshTokenExpiredMins)
                });
            }

            #endregion
        }