The following is a script from “Attack in Garland,” which aired on March 26, 2017. Anderson Cooper is the correspondent. Graham Messick and Steve McCarthy, producers. Jack Weingart, associate producer.

Wednesday’s terror attack in London by a man who had been known to British law enforcement is just the latest reminder of how difficult it is to prevent an attack before it takes place. Here in the U.S., in just the past three years, more than 100 people have been arrested for ISIS-related crimes. The FBI devotes significant resources to identifying potential terrorists and sometimes spends years tracking them. The terror attack in Garland, Texas, two years ago was the first claimed by ISIS on U.S. soil.

It’s mostly been forgotten because the two terrorists were killed by local cops before they managed to murder anyone. In looking into what happened in Garland, we were surprised to discover just how close the FBI was to one of the terrorists. Not only had the FBI been monitoring him for years, there was an undercover agent right behind him when the first shots were fired.

Terrorists behind 2015 Garland, Texas, attack. CBS News

The target of the attack was an event taking place in this conference center on May 3, 2015. A self-described free speech advocate named Pamela Geller was holding a provocative contest, offering a cash-prize for the best drawing of the prophet Muhammad, whose depiction is considered sacrilege by some Muslims. Security outside was heavy. There were dozens of police, a SWAT team, and snipers.

“The Garland attack is essentially the first opening salvo when it comes to attacks on the homeland.”

More than 100 people were gathered inside and the event was ending when two terrorists drove up to a checkpoint manned by a Garland police officer and a school security guard. This grainy image shows both law-enforcement personnel standing next to an unmarked police car seconds before the attack. Bruce Joiner, the security guard, was unarmed.

Bruce Joiner: It’s like they pull up, stop, and the doors open.

Anderson Cooper: Do you remember seeing the weapon?

Bruce Joiner: Oh, yeah. Definitely saw their weapon. And that’s when I locked onto his face ‘cause he’s got this smile.

Anderson Cooper: He was literally smiling?

Bruce Joiner: Yeah, like, “I got ya. I got ya.”

The two terorrists opened fire with automatic rifles. Joiner dove for cover, but was shot in the leg. Officer Greg Stevens, returned fire with his handgun. Police nearby ran toward the scene.

Eyewitness video: And right here (expletive) just started shooting at this convention!

When this video was recorded by a passerby, both terrorists had been mortally wounded by Officer Stevens, and were laying on the ground next to their car.

Eyewitness video: They still shootin man! …

A SWAT team shot them both in the head.

Bruce Joiner: Because they kept moving and they weren’t sure there were explosives involved they had to shoot them.

Anderson Cooper: How quick did all of this happen?

Bruce Joiner: Oh, it’s a matter of seconds. I would say 20, 30 seconds. It’s very quick.

The next day as the FBI picked through the crime scene, the evidence showed Garland police had prevented a massacre. The terrorists brought six guns, hundreds of rounds of ammunition, bulletproof and tactical vests, and Xeroxed copies of the black flag of ISIS. They were identified as 31-year-old Elton Simpson and 34-year-old Nadir Soofi. Just hours before the attack they had sent this tweet pledging allegiance to ISIS. But Simpson was already well-known to the FBI.

He grew up in the suburbs of Chicago, and moved to Phoenix, Arizona, in middle school. He briefly played college basketball before dropping out and converting to Islam when he was 20.

According to leaders of the Phoenix mosque he attended, Simpson was well-liked and soft-spoken.

Usama Shami: He was always asking questions, attending lectures.

Usama Shami, president of the Islamic Community Center of Phoenix CBS News

Usama Shami is president of the Islamic Community Center of Phoenix. People here thought so much of the young Muslim convert, who took the name “Ibrahim,” that he was included in the mosque’s promotional video in 2007.

Elton Ibrahim Simpson: When you come together and you pray five times a day with the brothers and you’re reminded about the hereafter…

But at the time of this interview, Simpson had already become interested in radical Islam, and the Phoenix FBI, which was investigating one of his friends, hired an informant, a Sudanese refugee named Dabla Deng, to check Simpson out.

Anderson Cooper: There are informants inside the mosque?

Usama Shami: Yeah. I mean the whole case with Elton Simpson was with an informant that he was befriending Elton and taping his conversations.

Dabla Deng spent three years pretending to be Simpson’s friend, and was paid $132,000 by the FBI. He taped more than 1,500 hours of their conversations and finally recorded him talking about traveling overseas to wage jihad. Simpson lied to the FBI about it and got three years probation.

Usama Shami: When he found out that this guy was spying on him, and taping him and then finding out that the government was doing that, I think something clicked in him. And the mosque, we couldn’t do anything. Because we don’t know what he did.

Anderson Cooper: He felt that the mosque had abandoned him?

Usama Shami: Yes. And he felt that a lot of people had abandoned him. And that’s why he stopped coming to the mosque.

He moved into this Phoenix apartment complex with Nadir Soofi, who he knew from the mosque. Soofi had just had a bitter break-up and the pizza parlor he owned was going out of business. It was here in this apartment that Simpson and Soofi began closely following the rise of ISIS, reaching out to their supporters online, and acquiring weapons for a terrorist attack.

Seamus Hughes: Simpson and Soofi knew what they were getting into and I think they likely knew they were going to die.

Seamus Hughes tracks the online activities of ISIS sympathizers in the U.S. He served at the National Counter Terrorism Center, and is currently deputy director of George Washington University’s “Program On Extremism,” where he also trains FBI agents on how to identify American jihadis.

Anderson Cooper: Why is the Garland attack so significant?

Seamus Hughes: The Garland attack is essentially the first opening salvo when it comes to attacks on the homeland.

Anderson Cooper: Attacks in the United States?

Seamus Hughes: Attacks in the United States. These low-level attacks by ones and twos of people who are drawn to the ideology and decide to act.

Seamus Hughes: Ya gotta make sense of it all. So what you do is you bring it all together and put it on a board and say who’s connected to who.

Using an old-fashioned law enforcement tool, Hughes maps out ISIS’ online tentacles into the United States.

Seamus Hughes: So you have the two attackers, Soofi and Simpson. They’re also talking to Mohammed Miski, who’s an ISIS recruiter in Somalia.

Anderson Cooper: This is somebody in Somalia who they’re talking to online--

Seamus Hughes: Uh-huh. Yep. Through an encrypted app, Surespot. They’re also talking to Junaid Hussein.

Anderson Cooper: And he’s in Raqqa?

Seamus Hughes: He’s in Raqqa.

Raqqa is ISIS’ stronghold in Syria. Hughes calls Junaid Hussain an “ISIS rock star,” a British citizen, who communicated online with English-speaking recruits worldwide. He was killed in a U.S. drone strike a year-and-a-half ago. “Miski,” an American living in Somalia, tweeted this link about the “draw Muhammad contest,” in Garland, Texas, and direct-messaged Elton Simpson urging him to attack it.

Seamus Hughes: The most interesting part about this is we’re in a hybrid time, right. Before we used to be worried about these network attacks, think of 9/11 with the hijackers training for years and then coming over here. And then, we had lone actor attacks, individuals who were kind of drawn to this and decided to act. Now, we’re in this weird moment in between, where you have a number of individuals in Raqqa, reaching out to Americans in Ohio, New York, and other places and saying, “So here’s the knife you should use. Here’s the address of the local U.S. military officer and do what you can.”

Anderson Cooper: Do you think Elton Simpson would have launched this attack if it wasn’t for people in ISIS overseas who were online whispering in his ear?

Seamus Hughes: I think the folks whispering in his ear was a big part of it.

The FBI closed the case on Elton Simpson in 2014, only to re-open it several weeks before the attack because of statements he made on social media.

Seamus Hughes: It speaks to a larger problem the FBI has, which is you have an individual who pops into your radar in 2006, but doesn’t commit an attack until 2015. So do you want the FBI to watch this individual for nine years?

After the attack, Phoenix FBI agents became convinced the two men hadn’t acted alone, and began investigating Elton Simpson’s friends. They arrested this man Abdul Malik Abdul Kareem, a 43-year-old convert to Islam who grew up in Philadelphia, and accused him of funding the attack, as well as training and encouraging Simpson and Soofi.

Witnesses at Abdul-Kareem’s trial testified the three men watched ISIS execution videos together and discussed attacking a military base or the 2015 Super Bowl in Glendale, Arizona. Abdul-Kareem denied taking part in any discussions about a terror attack and says he rejected his friend’s growing radicalization.

He was found guilty on multiple counts and sentenced to 30 years in prison. But his attorney Dan Maynard continued to investigate, and uncovered new evidence the FBI was much closer to the Garland attack than anyone realized.

Anderson Cooper: After the trial, you discovered that the government knew a lot more about the Garland attack than they had let on?

Dan Maynard: That’s right. Yeah. After the trial we found out that they had had an undercover agent who had been texting with Simpson, less than three weeks before the attack, to him “Tear up Texas.” Which to me was an encouragement to Simpson.

The man he’s talking about was a special agent of the FBI, working undercover posing as an Islamic radical. The government sent attorney Dan Maynard 60 pages of declassified encrypted messages between the agent and Elton Simpson – and argued “Tear up Texas” was not an incitement. But Simpson’s response was incriminating, referring to the attack against cartoonists at the French magazine Charlie Hebdo: “bro, you don’t have to say that...” He wrote “you know what happened in Paris… so that goes without saying. No need to be direct.”

But it turns out the undercover agent did more than just communicate online with Elton Simpson. In an affidavit filed in another case the government disclosed that the FBI undercover agent had actually “traveled to Garland, Texas, and was present… at the event.”

Dan Maynard: I was shocked. I mean I was shocked that the government hadn’t turned this over. I wanted to know when did he get there, why was he there?

And this past November, Maynard was given another batch of documents by the government, revealing the biggest surprise of all. The undercover FBI agent was in a car directly behind Elton Simpson and Nadir Soofi when they started shooting. This cell-phone photo of school security guard Bruce Joiner and police officer Greg Stevens was taken by the undercover agent seconds before the attack.

Anderson Cooper: The idea that he’s taking photograph of the two people who happen to be attacked moments before they’re attacked.

Dan Maynard: It’s stunning.

Anderson Cooper: I mean, talk about being in the right or the wrong place at the right or the wrong time.

Dan Maynard: The idea that he’s right there 30 seconds before the attack happens is just incredible to me.

Anderson Cooper: What would you want to ask the undercover agent?

“I can’t tell you whether the FBI knew the attack was gonna occur. I don’t like to think that they let it occur. But it is shocking to me that an undercover agent sees fellas jumping out of a car and he drives on.”

Dan Maynard: I would love to ask the undercover agent-- Are these the only communications that you had with Simpson? Did you have more communications with Simpson? How is it that you ended up coming to Garland, Texas? Why are you even there?

We wanted to ask the FBI those same questions. But the bureau would not agree to an interview. All the FBI would give us was this email statement. It reads: “There was no advance knowledge of a plot to attack the cartoon drawing contest in Garland, Texas.”

If you’re wondering what happened to the FBI’s undercover agent, he fled the scene but was stopped at gunpoint by Garland police. This is video of him in handcuffs, recorded by a local news crew. We’ve blurred his face to protect his identity.

Dan Maynard: I can’t tell you whether the FBI knew the attack was gonna occur. I don’t like to think that they let it occur. But it is shocking to me that an undercover agent sees fellas jumping out of a car and he drives on. I find that shocking.

Anderson Cooper: That he didn’t try to stop--

Dan Maynard: He didn’t try to stop ‘em. Or he didn’t do something. I mean, he’s an agent, for gosh sakes.

Anderson Cooper: If this attack had gone a different way, and lots of people had been killed, would the fact that an undercover FBI agent was on the scene have become essentially a scandal?

Seamus Hughes: It woulda been a bigger story. I think you would have seen congressional investigations and things like that. Lucky for the FBI and for the participants in the event you know, here in Texas, you know, everyone’s a good shot there.

The FBI’s actions around this foiled attack offer a rare glimpse into the complexities faced by those fighting homegrown extremism. Today, the battle often begins online where identifying terrorists can be the difference between a massacre, and the one that never occurred in Garland, Texas.

Anderson Cooper: People brag about stuff. People talk big. One of the difficulties for the FBI is trying to figure out who’s just talking and who actually may execute an attack.

Seamus Hughes: That’s the hardest part when you talk about this, right. There’s a lot of guys who talk about how great ISIS is. It’s very hard to tell when someone crosses that line. And in most of the cases, you see the FBI has some touchpoint with those individuals beforehand. There had been an assessment, a preliminary investigation or a full investigation. It’s just very hard to know when somebody decides to jump.