Пример #1
0
        public async Task <object> Register([FromBody] RegisterDTO model)
        {
            var user = new ApplicationUser
            {
                UserName = model.Email,
                Email    = model.Email
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                await _signInManager.SignInAsync(user, false);

                var refreshToken = GenerateRefreshToken();

                RefreshUserToken userToken = CreateRefreshUserToken(refreshToken, user);
                await UnitOfWork.UserTokens.AddAsync(userToken);

                await UnitOfWork.CompleteAsync();

                return(new
                {
                    token = await GenerateJwtToken(model.Email, user),
                    refresh_token = refreshToken
                });
            }
            else
            {
                return(BadRequest(result.Errors.Any() ? result.Errors.First().Description : "Invalid signup. Please try again.")); // fix this
            }
            throw new ApplicationException("UNKNOWN_ERROR");
        }
Пример #2
0
        protected virtual async Task <IActionResult> PasswordLogin(LoginDTO login, ModelStateDictionary modelState)
        {
            var result = await _signInManager.PasswordSignInAsync(login.Email, login.Password, false, false);

            if (result.Succeeded)
            {
                string refreshToken = GenerateRefreshToken();
                var    appUser      = _userManager.Users.SingleOrDefault(r => r.Email == login.Email);

                RefreshUserToken userToken = CreateRefreshUserToken(refreshToken, appUser);
                await UnitOfWork.UserTokens.AddAsync(userToken);

                await UnitOfWork.CompleteAsync();

                return(Ok(new
                {
                    token = await GenerateJwtToken(login.Email, appUser),
                    refresh_token = refreshToken
                }));
            }
            else
            {
                return(Unauthorized());
            }
        }
Пример #3
0
        protected virtual async Task <IActionResult> RefreshUserTokenLogin(LoginDTO login, ModelStateDictionary modelState)
        {
            RefreshUserToken existingToken = await UnitOfWork.UserTokens.SingleOrDefaultAsync(p => p.RefreshToken == login.Refresh_Token);

            if (existingToken == null)
            {
                return(Unauthorized());
            }

            ApplicationUser appUser = _userManager.Users.SingleOrDefault(e => e.Id == existingToken.UserId);

            if (appUser == null)
            {
                return(NotFound());
            }

            UnitOfWork.UserTokens.Remove(existingToken);
            string refreshToken = GenerateRefreshToken();
            await UnitOfWork.UserTokens.AddAsync(CreateRefreshUserToken(refreshToken, appUser));

            await UnitOfWork.CompleteAsync();

            return(Ok(new
            {
                token = await GenerateJwtToken(existingToken.User.Email, appUser),
                refresh_token = refreshToken
            }));
        }