Exemple #1
0
        public async Task <HttpResponse> CreateAccount(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            User   user        = JsonConvert.DeserializeObject <User>(requestBody);

            try
            {
                user = await AccountService.CreateAccount(user);

                string jwt;
                try
                {
                    jwt = JWTHandler.GetJWT(user);
                }
                catch (Exception e)
                {
                    log.LogError("Failed to generate token", e);
                    throw;
                }

                return(await GenerateResponse(jwt, user, req));
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemple #2
0
        public async Task <HttpResponse> Login(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            User   user        = JsonConvert.DeserializeObject <User>(requestBody);
            string jwt;

            try
            {
                User userFromRespons = await AccountService.Login(user);

                try
                {
                    jwt = JWTHandler.GetJWT(userFromRespons);
                    return(await GenerateResponse(jwt, userFromRespons, req));
                }
                catch (Exception e)
                {
                    log.LogError("Failed to generate token", e);
                    throw;
                }
            }
            catch (Exception)
            {
                var context  = req.HttpContext;
                var response = context.Response;
                response.StatusCode = 401;
                await response.WriteAsync("Failed to authorize");

                return(response);
            }
        }