Saving the login state to the session
A session is when a user logs in up to when the user logs out. Vibe.d provides a mechanism to uses a session.
When we want to retrieve a particular record from the database, we simply use the id field as the key to retrieve the record by passing the key to the next page. When we retrieve another record, we lose this key because we receive another key.
But when a user logs in, the log in state needs to be saved for the duration of the session so that the user can navigate from page to page without having to log in again and again.
If the user is logged in, we should save that state so that as the user navigates from page to page, the system can check if the user is already logged in and is authorized to access that particular page. We need a session for that.
A variable, such as a logged-in state, can be saved to this session facility. Vibe.d has the session store for this purpose, of which there are two kinds: MemorySessionStore and the database-based store. For this project, we will use MemorySessionStore.
Edit source\app.d:
import vibe.vibe;
import empcontrol;
void main()
{
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
settings.sessionStore = new MemorySessionStore;
auto router = new URLRouter;
router.get("*", serveStaticFiles("public/"));
router.registerWebInterface(new EmployeeController);
auto listener = listenHTTP(settings, router);
scope (exit) listener.stopListening();
runApplication();
}Edit source\empcontrol.d to add a User struct.
And edit postLogin() to save the logged-in state to the session using the m_user variable.
We created a User struct to hold the logged-in state
Then we declared a session variable named m_user of that struct type:
private SessionVar!(User, "user") m_user;
Then we changed the value of m_user inside postLogin() to save and start the session:
Changing the value of m_user automatically starts the session.
We indicated that index() will be called and passed the error if postLogin() encounters an error:
Then we changed the index() method to accept an _error variable if postLogin() encounters an error.
Edit views\index.dt to display the error, if there is any, returned by the enforce() function.
Compile, run and refresh the browser. Click on the Login menu item and input a non-existing admin user. If you get an error, that means the error-trapping mechanism is working.
Now let us make the other pages know about the login state.
Last updated