authenticate

Ensures that a user is logged in.

If a valid session exists, the returned User object will contain all information about the logged in user. Otherwise, the response object will be used to redirect the user to the login page and an empty user object is returned.

@trusted
authenticate
(
HTTPServerRequest req
,
HTTPServerResponse res
,,
string url_prefix = ""
)

Parameters

req HTTPServerRequest

The request object of the incoming request

res HTTPServerResponse

The response object of the incoming request

api UserManAPI

A reference to the UserMan API

url_prefix string

Optional prefix to prepend to the login page path

Examples

This example uses the @before annotation supported by the vibe.web.web framework for a concise and statically defined authentication approach.

import vibe.http.router;
import vibe.http.server;
import vibe.web.web;

@requiresAuth
class MyWebService {
	@safe:
	private {
		UserManAPI m_api;
	}

	this(UserManAPI userman)
	{
		m_api = userman;
	}

	// this route can be accessed publicly (/)
	@noAuth
	void getIndex()
	{
		//render!"welcome.dt"
	}

	// the @authenticated attribute (defined below) ensures that this route
	// (/private_page) can only ever be accessed if the user is logged in
	@anyAuth
	void getPrivatePage(User user)
	{
		// render a private page with some user specific information
		//render!("private_page.dt", user);
	}

	@noRoute User authenticate(HTTPServerRequest req, HTTPServerResponse res)
	{
		return .authenticate(req, res, m_api);
	}
}

void registerMyService(URLRouter router, UserManAPI userman)
{
	router.registerUserManWebInterface(userman);
	router.registerWebInterface(new MyWebService(userman));
}

Meta