Beispiel #1
0
        public async Task <User> LoginAsyc(AuthenticationProviderKind provider, string token)
        {
            var user = await PostAsync <User>("login",
                                              new { Provider = provider.ToString(), Token = token });

            User = user;
            return(user);
        }
        public async Task <User> LoginAsyc(AuthenticationProviderKind provider, string token)
        {
            var db = new LunchContext(_dbOptions);

            // First, check if our demo user exists already. If so, log them back in.

            var user = await db.Users.FirstOrDefaultAsync(x => x.AuthenticationProviderKind == provider &&
                                                          x.AuthorizationToken == token);

            if (user != null)
            {
                User = user;
                return(user);
            }

            // Otherwise, we need to make a new demo user.

            // Load a preset list of demo users, except give each a unique Id
            // and authorization token so the login experience remains authentic.

            var users = (await ReadEmbeddedResourcesAsync <List <User> >(_usersResource))
                        .Select(x => new User
            {
                AuthenticationProviderId     = Guid.NewGuid().ToString(),
                AuthenticationProviderKind   = x.AuthenticationProviderKind,
                AuthorizationTokenExpiration = x.AuthorizationTokenExpiration,
                AuthorizationToken           = Guid.NewGuid().ToString(),
                Name     = x.Name,
                PhotoUrl = x.PhotoUrl
            }).ToList();

            // If our user logged in with a provider, use that information and fake all the rest.
            // Otherwise, create a fake profile for them too.

            if (provider != AuthenticationProviderKind.Demo)
            {
                user = new User
                {
                    AuthenticationProviderKind   = provider,
                    AuthorizationToken           = token,
                    AuthorizationTokenExpiration = new DateTime(2038, 1, 1)
                };
            }
            else
            {
                user = RandomAndRemove(users);
            }
            db.Users.Add(user);
            await db.SaveChangesAsync();

            // When we create a demo user, also generate them some random friends.

            for (int i = 0; i < users.Count; i++)
            {
                user.Friends.Add(users[i]);
            }
            await db.SaveChangesAsync();

            // Lastly, set up some invitations and lunches for them, so there's content when
            // they first launch the app.

            var hosts     = user.Friends.ToList();
            var locations = (await GetRestaurantsAsync("")).ToList();

            for (int i = 0; i < 4; i++)
            {
                var lunch = new Lunch
                {
                    Date     = AddDaysAtNoonToUtc(i + 1),
                    Host     = RandomAndRemove(hosts),
                    Location = RandomAndRemove(locations)
                };

                // Invite the user to lunch, plus a bunch of their friends as well
                // so the list of accepts/declines has something to show.

                lunch.Invitations.Add(new Invitation(lunch, user));
                foreach (var friend in user.Friends.OrderBy(x => _random.Next())
                         .Take(_random.Next(1, user.Friends.Count / 2)))
                {
                    lunch.Invitations.Add(new Invitation
                    {
                        Lunch    = lunch,
                        User     = friend,
                        Response = _random.Next(0, 2) == 0 ? InviteResponseKind.Accepted : InviteResponseKind.Declined
                    });
                }
                db.Lunches.Add(lunch);
                await db.SaveChangesAsync();
            }


            var userLunch = new Lunch
            {
                Host     = user,
                Location = RandomAndRemove(locations),
                Date     = AddDaysAtNoonToUtc(1),
                Notes    = "My first lunch!"
            };

            userLunch.Invitations = new ObservableCollection <Invitation>(user.Friends
                                                                          .OrderBy(x => _random.Next())
                                                                          .Take(_random.Next(1, user.Friends.Count))
                                                                          .Select(x => new Invitation(userLunch, x)));
            db.Lunches.Add(userLunch);

            await db.SaveChangesAsync();

            var dbTest  = new LunchContext(_dbOptions);
            var lunches = dbTest.Lunches.Include(x => x.Invitations).ToList();

            db.Entry(user).State = EntityState.Detached;
            User = user;
            return(user);
        }