public async Task SignUpAfterVerify([FromBody] Account acc)
        {
            var filter = Builders <Account> .Filter.Eq("_id", acc.username);

            var update = Builders <Account> .Update.Set("connected", true);

            acc.password = encryptService.Encrypt(acc.password);
            var task2 = accounts.UpdateOneAsync(filter, update);
            var task3 = accounts.InsertOneAsync(acc);

            memoryService.OnlineTrack[acc.username]          = new(300000);
            memoryService.OnlineTrack[acc.username].Elapsed += (s, e) =>
            {
                var filter = Builders <Account> .Filter.Eq("_id", acc.username);

                var update = Builders <Account> .Update.Set("connected", false);

                accounts.UpdateOne(filter, update);
                memoryService.OnlineTrack[acc.username].Stop();
                memoryService.OnlineTrack.Remove(acc.username);
                memoryService.OnlineTrack[acc.username].Close();
            };
            memoryService.OnlineTrack[acc.username].Enabled = true;
            await task2;
            await task3;
        }
        public async Task <IActionResult> SignUp([FromBody] Account account)
        {
            var acc = dbContext.Accounts.Find(account.username);

            if (acc is null)
            {
                account.password = encryptionService.Encrypt(account.password);
                dbContext.Accounts.Add(account);
                await dbContext.SaveChangesAsync();

                return(Ok());
            }
            else
            {
                return(StatusCode(403, "Your username has been taken"));
            }
        }
Exemple #3
0
        public IActionResult SignIn([FromBody] Account account)
        {
            var acc = accounts.Find(s => s.username == account.username).FirstOrDefault();

            if (acc is null)
            {
                return(NotFound("Your username does not exist"));
            }
            if (acc.password != encryptionService.Encrypt(account.password))
            {
                return(Unauthorized("Your password is incorrect"));
            }
            return(Ok());
        }
        public async Task <IActionResult> SignUpAfterVerify(int code, [FromBody] Account acc)
        {
            if (memoryService.ResetAccounts.ContainsKey(acc.username))
            {
                if (code == memoryService.ResetAccounts[acc.username])
                {
                    memoryService.ResetAccounts.Remove(acc.username);
                    memoryService.TimerReset[acc.username].Enabled = false;
                    memoryService.TimerReset[acc.username].Close();
                    memoryService.TimerReset.Remove(acc.username);
                    memoryService.VerifiedUsers.Add(acc.username);
                }
                else
                {
                    return(StatusCode(403, "Your verification code is incorrect"));
                }
            }
            else
            {
                return(NotFound("Your verification code has expired"));
            }
            if (memoryService.VerifiedUsers.Contains(acc.username))
            {
                var filter = Builders <Account> .Filter.Eq("_id", acc.username);

                var update = Builders <Account> .Update.Set("online", true);

                acc.password = encryptService.Encrypt(acc.password);
                Thread t = new(() => { walls.CreateCollection(acc.username); });
                t.Start();
                Thread t2 = new(() => { accounts.UpdateOne(filter, update); });
                t2.Start();
                Thread t3 = new(() => { accounts.InsertOne(acc); });
                t3.Start();
                var task = Task.Factory.StartNew(() =>
                {
                    t.Join();
                    t2.Join();
                    t3.Join();
                });
                memoryService.OnlineTrack[acc.username]          = new(600000);
                memoryService.OnlineTrack[acc.username].Elapsed += (s, e) =>
                {
                    var filter = Builders <Account> .Filter.Eq("_id", acc.username);

                    var update = Builders <Account> .Update.Set("online", false);

                    accounts.UpdateOne(filter, update);
                    memoryService.OnlineTrack[acc.username].Stop();
                    memoryService.OnlineTrack.Remove(acc.username);
                    memoryService.OnlineTrack[acc.username].Close();
                };
                memoryService.OnlineTrack[acc.username].Enabled = true;
                var accesstoken = ObjectId.GenerateNewId().ToString();
                memoryService.AccessTokens[acc.username] = accesstoken;
                memoryService.CreateSessionTracker(acc.username);
                memoryService.VerifiedUsers.Remove(acc.username);
                await task;
                return(Ok(accesstoken));
            }
            return(Unauthorized("Your account hasn't been verified"));
        }
        public async Task <SignResult> JoinRoom(string roomid, string username, [FromBody] string password)
        {
            var filter = Builders <Room> .Filter.Eq("_id", roomid);

            var room = rooms.Find(filter).FirstOrDefault();

            if (room is not null)
            {
                if (!memoryService.ChatRooms.ContainsKey(room.Id))
                {
                    memoryService.ChatRooms[room.Id]    = client.GetDatabase(room.Id);
                    memoryService.RoomAccounts[room.Id] = memoryService.ChatRooms[room.Id].GetCollection <Account>("accounts");
                    memoryService.RoomMessages[room.Id] = memoryService.ChatRooms[room.Id].GetCollection <Message>("messages");
                }
                if (room.users.Contains(username))
                {
                    if (!room.state)
                    {
                        if (room.password == encryptService.Encrypt(password))
                        {
                            return(new SignResult()
                            {
                                success = true
                            });
                        }
                        return(new SignResult()
                        {
                            success = false, err = "Incorrect password"
                        });
                    }
                    return(new SignResult()
                    {
                        success = true
                    });
                }
                else
                {
                    room.password = string.Empty;
                    var userfilter = Builders <Account> .Filter.Eq("_id", username);

                    var userupdate = Builders <Account> .Update.AddToSet <string>("rooms", roomid);

                    var update = Builders <Room> .Update.Set("users", room.users);

                    if (!room.state)
                    {
                        if (room.password == encryptService.Encrypt(password))
                        {
                            var usermesstask = memoryService.ChatRooms[roomid].CreateCollectionAsync(username);
                            var usertask     = accounts.UpdateOneAsync(userfilter, userupdate);
                            room.users.Add(username);
                            var task = memoryService.RoomAccounts[roomid].InsertOneAsync(new Account()
                            {
                                username = username, connected = false
                            });
                            var task1 = rooms.UpdateOneAsync(filter, update);
                            memoryService.ChatRooms[roomid].CreateCollection(username);
                            await usermesstask;
                            await usertask;
                            await task;
                            await task1;
                            return(new SignResult()
                            {
                                success = true
                            });
                        }
                        return(new SignResult()
                        {
                            success = false, err = "Incorrect password"
                        });
                    }
                    var usertask1 = accounts.UpdateOneAsync(userfilter, userupdate);
                    room.users.Add(username);
                    var task2 = memoryService.RoomAccounts[roomid].InsertOneAsync(new Account()
                    {
                        username = username, connected = false
                    });
                    var task3 = rooms.UpdateOneAsync(filter, update);
                    memoryService.ChatRooms[roomid].CreateCollection(username);
                    await usertask1;
                    await task2;
                    await task3;
                    return(new SignResult()
                    {
                        success = true
                    });
                }
            }
            return(new SignResult()
            {
                success = false, err = "RoomId does not exist"
            });
        }