Ejemplo n.º 1
0
        public async Task<UserToken> Login(RequestAuthenticate requestAuthenticate)
        {
            var user = await repository.GetUser(requestAuthenticate.Username);
            if (user != null)
            {
                if (user.Password == requestAuthenticate.Password)
                {
                    var token = await tokenRepository.GetUserToken(requestAuthenticate.Username);
                    if (token != null)
                    {
                        return token;
                    }

                    string generatedToken = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                    var newToken = new UserToken
                    {
                        Username = requestAuthenticate.Username,
                        LastAccessed = DateTime.Now,
                        Source = requestAuthenticate.Source,
                        Token = generatedToken,

                    };
                    await tokenRepository.CreateSync(newToken);

                    return newToken;
                }
            }
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized)
            {
                Content = new StringContent("Invalid username or password"),
                ReasonPhrase = "Invalid username or password"
            });
        }
Ejemplo n.º 2
0
        public async Task<UserToken> SignUp(string email, string password, string firstname, string lastname)
        {
            var users = await repository.ListAll();
            if (users.Any(n => n.Email == email))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("User already exist."),
                    ReasonPhrase = "User with the same email address already exist."
                });
            }

            await repository.CreateSync(new User
            {
                Email = email,
                Password = password,
                FirstName = firstname,
                LastName = lastname
            });

            string generatedToken = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
            var newToken = new UserToken
            {
                Username = email,
                LastAccessed = DateTime.Now,
                Source = "mobile",
                Token = generatedToken,

            };
            await tokenRepository.CreateSync(newToken);

            return newToken;
        }