Пример #1
0
        public IActionResult AuthWithTelegram([FromBody] TelegramAuthModel model)
        {
            // todo Validate telegram request
            var group = _repository.GetGroupByName(model.Group);

            if (group == null)
            {
                return(BadRequest(new { message = "Group not exist!" }));
            }
            var authReuqest = new AuthenticateRequestTelegram
            {
                GroupId      = group.Id,
                TelegramUser = model.TelegramUser
            };
            var authResult = _userSerice.AuthenticateWithTelegram(authReuqest, ipAddress());

            if (authResult == null)
            {
                return(BadRequest("Group is not match user group!"));
            }

            setTokenCookie(authResult.RefreshToken);

            return(Ok(new AuthentificateDto
            {
                Id = authResult.Student.Id,
                Group = authResult.Student.Group.Group_Name,
                Username = authResult.Student.Username,
                FirstName = authResult.Student.FirstName,
                LastName = authResult.Student.LastName,
                Token = authResult.JwtToken,
                RefreshToken = authResult.RefreshToken
            }));
        }
Пример #2
0
        // todo користувачі можуть змінювати імена і юзернейми в телеграмі
        // todo додати можливість змінити групу
        // public AuthenticateResponse Authenticate(AuthenticateRequest model, string ipAddress)
        // {
        //     //get student from db by username
        //     // generate jwt token
        //     // generate refresh token
        //     // save refresh token
        // }

        public AuthenticateResponse AuthenticateWithTelegram(AuthenticateRequestTelegram model, string ipAddress)
        {
            // Check if student exists by id
            var student = _context.Students
                          .Include("Group")
                          .Include("RefreshTokens")
                          .SingleOrDefault(s => s.Telegram_Id == model.TelegramUser.id);

            if (student == null)
            {
                // if not exists register and auth
                student = new Student
                {
                    Id          = Guid.NewGuid(),
                    Username    = model.TelegramUser.username,
                    FirstName   = model.TelegramUser.first_name,
                    LastName    = model.TelegramUser.last_name,
                    GroupId     = model.GroupId,
                    Telegram_Id = model.TelegramUser.id
                };
                _context.Students.Add(student);
                _context.SaveChanges();
            }

            // if exists-  auth
            if (student.GroupId != model.GroupId)
            {
                return(null);
            }

            var jwt          = generateJwtToken(student);
            var refreshToken = generateRefreshToken(ipAddress);

            // save refresh token
            student.RefreshTokens.Add(refreshToken);
            _context.Update(student);
            _context.SaveChanges();


            return(new AuthenticateResponse {
                Student = student, JwtToken = jwt, RefreshToken = refreshToken.Token
            });
        }