Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
middleware::OAuth2 Class Reference

Middleware for handling OAuth2 authentication. More...

#include <OAuth2.hpp>

Public Member Functions

 OAuth2 (const OAuth2Config &config)
 Constructs an OAuth2 middleware with the given config.
 
std::function< void(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> login_handler ()
 Handler to redirect the user to the OAuth2 provider's consent page.
 
std::function< void(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> callback_handler (std::function< void(const nlohmann::json &user_info, http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> on_success, std::function< void(const std::string &error, http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> on_error)
 Handler to process the redirect callback, exchange the code for an access token, and fetch user info.
 

Static Public Member Functions

static OAuth2 google (const std::string &client_id, const std::string &client_secret, const std::string &redirect_uri)
 Pre-configured factory for Google OAuth2.
 
static OAuth2 github (const std::string &client_id, const std::string &client_secret, const std::string &redirect_uri)
 Pre-configured factory for GitHub OAuth2.
 

Detailed Description

Middleware for handling OAuth2 authentication.

Constructor & Destructor Documentation

◆ OAuth2()

middleware::OAuth2::OAuth2 ( const OAuth2Config config)

Constructs an OAuth2 middleware with the given config.

Parameters
configThe OAuth2 configuration.

Member Function Documentation

◆ callback_handler()

std::function< void(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> middleware::OAuth2::callback_handler ( std::function< void(const nlohmann::json &user_info, http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)>  on_success,
std::function< void(const std::string &error, http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)>  on_error 
)

Handler to process the redirect callback, exchange the code for an access token, and fetch user info.

Parameters
on_successCallback executed upon successful authentication.
on_errorCallback executed if an error occurs during authentication.
Returns
std::function A middleware handler for the callback route.

◆ github()

OAuth2 middleware::OAuth2::github ( const std::string &  client_id,
const std::string &  client_secret,
const std::string &  redirect_uri 
)
static

Pre-configured factory for GitHub OAuth2.

Parameters
client_idThe GitHub client ID.
client_secretThe GitHub client secret.
redirect_uriThe redirect URI.
Returns
OAuth2 A configured OAuth2 instance.

◆ google()

OAuth2 middleware::OAuth2::google ( const std::string &  client_id,
const std::string &  client_secret,
const std::string &  redirect_uri 
)
static

Pre-configured factory for Google OAuth2.

Parameters
client_idThe Google client ID.
client_secretThe Google client secret.
redirect_uriThe redirect URI.
Returns
OAuth2 A configured OAuth2 instance.

◆ login_handler()

std::function< void(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> middleware::OAuth2::login_handler ( )

Handler to redirect the user to the OAuth2 provider's consent page.

Returns
std::function A middleware handler for login redirection.

The documentation for this class was generated from the following files: