示例#1
0
        public async Task <ActionResult <UserWithToken> > RegisterUser([FromBody] User user)
        {
            try
            {
                if (user == null)
                {
                    return(BadRequest());
                }
                //Add users
                var result = await userRepository.AddUser(user);

                //load role for registered user
                user = await userRepository.GetUserByID(user.UserId);

                UserWithToken userWithToken = null;

                if (user != null)
                {
                    //Generate token
                    UserRefreshToken refreshToken = GenerateToken.GenerateRefreshToken();

                    //Add generated token to database
                    await userRepository.AddUserRefreshToken(user, refreshToken);


                    userWithToken = new UserWithToken(user);
                    userWithToken.RefreshToken = refreshToken.Token;
                }

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

                //sign your token here here..
                userWithToken.AccessToken = GenerateToken.GenerateAccessToken(user.UserId, jwtsettings);

                return(Ok(userWithToken));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
示例#2
0
        public async Task <ActionResult <UserWithToken> > Login([FromBody] User user)
        {
            try
            {
                user = await userRepository.ValidateUserFromEmailAndPasswords(user.Email, user.Password);

                UserWithToken userWithToken = null;

                if (user != null)
                {
                    //Generate token
                    UserRefreshToken refreshToken = GenerateToken.GenerateRefreshToken();

                    //Add generated token to database
                    await userRepository.AddUserRefreshToken(user, refreshToken);


                    userWithToken = new UserWithToken(user);
                    userWithToken.RefreshToken = refreshToken.Token;

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

                    //sign your token here here..
                    userWithToken.AccessToken = GenerateToken.GenerateAccessToken(user.UserId, jwtsettings);
                    return(Ok(userWithToken));
                }
                else
                {
                    return(NotFound("User not found"));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }