Example #1
0
        public static async Task <IActionResult> RiderCreate([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {
            // req.IsValidToken();

            //Rider user = await req.Content.ReadAsAsync<Rider>();

            string requestBody = new StreamReader(req.Body).ReadToEnd();
            Rider  user        = JsonConvert.DeserializeObject <Rider>(requestBody);

            await RiderRepository <Rider> .Initialize();

            if (user == null)
            {
                return((ActionResult) new OkObjectResult("User cannot be null or empty"));
            }

            if (string.IsNullOrEmpty(user.Email))
            {
                return((ActionResult) new OkObjectResult("An email address is needed for this request"));
            }

            var udb = RiderRepository <Rider> .GetItems($"Select * from RiderData u where u.Email = '{user.Email}'");

            if (udb != null && udb.Count() > 0)
            {
                return((ActionResult) new OkObjectResult("Rider already exist, please login or activate account to continue"));
            }

            if (string.IsNullOrEmpty(user.Password))
            {
                return((ActionResult) new OkObjectResult("A Password is needed for this request"));
            }

            user.Password = user.Password.EncodeString();

            user.LastModified = DateTime.Now;

            user.IsActivated = true;

            try
            {
                dynamic u = await RiderRepository <Rider> .CreateItemAsync(user);

                return((ActionResult) new OkObjectResult("Your account has been created. Admin will contact via the number provided to complete the activation process."));
            }
            catch (Exception ex)
            {
                return((ActionResult) new OkObjectResult("An error has occured"));
            }
        }
Example #2
0
        public static async Task <IActionResult> GetConnectedRiders([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req)
        {
            req.IsValidToken();
            try
            {
                // User user = await req.Content.ReadAsAsync<Driver>();

                await RiderRepository <Rider> .Initialize();

                var drivers = RiderRepository <Rider> .GetItems("Select * from RiderData d where d.Status <> 'busy' or d.Status <> 'offline' ");

                if (drivers == null || drivers.Count() <= 0)
                {
                    return((ActionResult) new OkObjectResult("No Avilable Riders In The System."));
                }

                return((ActionResult) new OkObjectResult(drivers));
            }
            catch (Exception ex)
            {
                return((ActionResult) new OkObjectResult(ex));
            }
        }
        public static async Task <IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req)
        {
            try
            {
                await DriverRepository <Driver> .Initialize();

                await RiderRepository <Rider> .Initialize();

                await UserRepository <User> .Initialize();

                string token = req.Query["token"];

                if (string.IsNullOrEmpty(token))
                {
                    return((ActionResult) new OkObjectResult("Please set the token feild as a query parameter '?token='"));
                }

                string decode = string.Empty;

                try
                {
                    decode = token.DecodeString();
                }
                catch (Exception)
                {
                    return((ActionResult) new OkObjectResult("Invalid base64 Token"));
                }


                var parts = decode.Split(':');

                //return req.CreateResponse(HttpStatusCode.OK, parts);

                if (parts == null || parts.Count() <= 0)
                {
                    return((ActionResult) new OkObjectResult("Token Is invalid"));
                }

                var email = parts[0];

                var password = parts[1];

                var type = parts[2];

                DisplayUser ds = null;

                if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(type))
                {
                    return((ActionResult) new OkObjectResult("Token failed"));
                }

                password = password.EncodeString();

                if (type.ToLower().Equals("user"))
                {
                    var u = UserRepository <User> .GetItems($"Select * from UserData u where u.Email = '{email}' and u.Password = '******' and u.IsActivated = true");

                    if (u == null || u.Count() <= 0)
                    {
                        return((ActionResult) new OkObjectResult("Failed To locate a user with the provide credentials or user is nnot yet activated"));
                    }

                    var fs = u.FirstOrDefault();

                    ds = new DisplayUser()
                    {
                        Firstname   = fs.Firstname,
                        Lastname    = fs.Lastname,
                        id          = fs.id,
                        DateCreated = fs.DateCreated,
                        Email       = fs.Email,
                        //IsLoggedIn = fs.IsLoggedIn,
                        IsActivated  = fs.IsActivated,
                        LastModified = fs.LastModified ?? null,
                        Phone        = fs.Phone
                                       //Status = fs.Status
                    };

                    var updateUser = await UserRepository <User> .GetItemAsync(ds.id);

                    if (updateUser != null)
                    {
                        updateUser.IsLoggedIn = true;

                        await UserRepository <User> .UpdateItemAsync(updateUser.id, updateUser);
                    }
                    //return req.CreateResponse(HttpStatusCode.OK, ds);
                }
                else if (type.ToLower().Equals("rider"))
                {
                    var u = RiderRepository <Rider> .GetItems($"Select * from RiderData u where u.Email = '{email}' and u.Password = '******' and u.IsActivated = true ");

                    if (u == null || u.Count() <= 0)
                    {
                        return((ActionResult) new OkObjectResult("Failed To locate a user with the provide credentials or user is nnot yet activated"));
                    }

                    var fs = u.FirstOrDefault();

                    ds = new DisplayUser()
                    {
                        Firstname    = fs.Firstname,
                        Lastname     = fs.Lastname,
                        id           = fs.id,
                        DateCreated  = fs.DateCreated,
                        Email        = fs.Email,
                        IsLoggedIn   = fs.IsLoggedIn,
                        IsActivated  = fs.IsActivated,
                        LastModified = fs.LastModified ?? null,
                        Phone        = fs.Phone,
                        Status       = fs.Status
                    };
                }
                else if (type.ToLower().Equals("driver"))
                {
                    var u = DriverRepository <Driver> .GetItems($"Select * from DriverData u where u.Email = '{email}' and u.Password = '******' and u.IsActivated = true");

                    if (u == null || u.Count() <= 0)
                    {
                        return((ActionResult) new OkObjectResult("Failed To locate a user with the provide credentials or user is nnot yet activated"));
                    }

                    var fs = u.FirstOrDefault();

                    ds = new DisplayUser()
                    {
                        Firstname    = fs.Firstname,
                        Lastname     = fs.Lastname,
                        id           = fs.id,
                        DateCreated  = fs.DateCreated,
                        Email        = fs.Email,
                        IsLoggedIn   = fs.IsLoggedIn,
                        IsActivated  = fs.IsActivated,
                        LastModified = fs.LastModified ?? null,
                        Phone        = fs.Phone,
                        Status       = fs.Status
                    };
                }
                else
                {
                    return((ActionResult) new OkObjectResult("invalid login type"));
                }

                if (ds == null)
                {
                    return((ActionResult) new OkObjectResult("Login failed no user located, sorry."));
                }


                try
                {
                    string key = Environment.GetEnvironmentVariable("Secret");

                    var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

                    var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

                    var header = new JwtHeader(credentials);

                    var claim = new[]
                    {
                        new Claim(ClaimTypes.NameIdentifier, ds.id),
                        new Claim(ClaimTypes.Name, email)
                    };

                    var tokens = new JwtSecurityToken(
                        issuer: "Phynix",
                        audience: "Phynix Inc",
                        claims: claim,
                        notBefore: DateTime.Now.AddMinutes(1),
                        expires: DateTime.Now.AddDays(2),
                        signingCredentials: credentials);

                    var handler = new JwtSecurityTokenHandler();

                    var tokenString = handler.WriteToken(tokens);



                    return((ActionResult) new OkObjectResult(new { Result = new { data = ds, token = tokenString } }));
                }
                catch (Exception ex)
                {
                    return((ActionResult) new OkObjectResult(ex));
                }
            }
            catch (Exception ex)
            {
                return((ActionResult) new OkObjectResult(ex));
            }
        }