예제 #1
0
        public async Task <IJResponse> Login(
            [FromServices] OpenRCT2org.IUserApi userApi,
            [FromServices] IUserSessionRepository userSessionRepository,
            [FromServices] DB.Abstractions.IUserRepository userRepository,
            [FromBody] JLoginRequest body)
        {
            try
            {
                Guard.ArgumentNotNull(body);
                Guard.ArgumentNotNull(body.user);
                Guard.ArgumentNotNull(body.password);
            }
            catch
            {
                return(JResponse.Error(JErrorMessages.InvalidRequest));
            }

            _logger.LogInformation("User login: {0}", body.user);

            OpenRCT2org.JUser orgUser;
            try
            {
                orgUser = await userApi.AuthenticateUser(body.user, body.password);
            }
            catch (OpenRCT2org.OpenRCT2orgException)
            {
                return(JResponse.Error(ErrorAuthenticationFailed));
            }

            var ourUser = await userRepository.GetUserFromOpenRCT2orgIdAsync(orgUser.userId);

            if (ourUser == null)
            {
                ourUser = new DB.Models.User()
                {
                    OpenRCT2orgId = orgUser.userId,
                    UserName      = orgUser.name
                };
                await userRepository.InsertUserAsync(ourUser);
            }

            string token = await userSessionRepository.CreateToken(orgUser.userId);

            return(new JLoginResponse()
            {
                status = JStatus.OK,
                user = orgUser.name,
                token = token
            });
        }