Arrange half of the sliced potatoes in the dish. Season with salt and pepper and scatter half of the sliced onions evenly over the top.
Layer the remaining potatoes and onions, seasoning again lightly with salt and pepper.
In a saucepan over medium heat, melt the butter, whisk in flour and salt, and cook for about a minute until the raw flour taste is gone.
Slowly add milk, about ¼ cup at a time, whisking constantly to prevent lumps.
Cook until thickened, 3 to 5 minutes, then stir in the cheese until fully melted.
Pour the warm cheese sauce evenly over the layered potatoes, making sure it seeps down between the layers.
Bake for about 1 ½ hours, until the potatoes are tender and the sauce is bubbling.
Serve hot and enjoy.