Exemple #1
0
        public async Task <ActionResult <string> > Post([FromBody] TokenGroup tokenGroup)
        {
            string username;

            try
            {
                RedditAPI reddit = new RedditAPI(System.Environment.GetEnvironmentVariable("VUE_APP_CLIENT_ID"), accessToken: tokenGroup.AccessToken);
                username = reddit.Account.Me.Name;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(Unauthorized(e.Message));
            }

            // No exceptions, token is valid and we have username
            // insert into database and store the primary key
            RedditUser user;

            try
            {
                string hashedUsername = RedditUser.HashUsername(username);
                user = _context.RedditUsers.Single(u => u.Username == hashedUsername);
            }
            catch (InvalidOperationException)
            {
                user = new RedditUser()
                {
                    Username = username
                };
                _context.Add(user);
                _context.SaveChanges();
            }
            // retrieve username using Reddit.NET and variables from .env
            // if successful, store hashed token and hashed username in tables
            // call AuthenticateUser(username, token)
            await AuthenticateUser(user.Id, tokenGroup.RefreshToken);

            return(NoContent());
        }