Project

General

Profile

Use of AuthWidget

Added by Yosr Chebbi almost 10 years ago

Hello,

I have a home page (displayed using an xml file) from which I want to navigate to an authentication form. When I click to the button it brings me to a new page that contains nth, I don't quite understand where the problem is coming from. I was following the hangman example for my Session and User files.

Here is my code:

//main.C

Wt::WApplication *createApplication(const Wt::WEnvironment& env)

{

//here I set a bootstrap theme

app~~messageResourceBundle().use(app~~>appRoot() + "test");//loaded my xml file

new HomePage(app->root());

return app;

}

int main(int argc, char **argv) {

return WRun(argc, argv, &createApplication);

}

//Home.C

HomePage::HomePage(WContainerWidget *parent):

WContainerWidget(parent)

{

WApplication::instance()->useStyleSheet("resources/themes/bootstrap/css/style.css");

mainStack_ = new WStackedWidget();

addWidget(mainStack_);

out_= new Wt::WTemplate(Wt::WString::tr("home"),mainStack_);

out_->setInternalPathEncoding(true);

Wt::WPushButton *signIn = new Wt::WPushButton("Connexion");

out_->bindWidget("Connexion", signIn);

signIn->setStyleClass("btn btn-success");

signIn->setLink(Wt::WLink(Wt::WLink::InternalPath, "/signIn"));

//I binded the rest of my widgets here

WApplication::instance()->internalPathChanged()

.connect(this, &HomePage::handleInternalPath);

}

void HomePage::handleInternalPath(const std::string &internalPath)

{

if (internalPath == "/signIn"){

signInPage();

}

else

WApplication::instance()->setInternalPath("/", true);

}

void HomePage::signInPage(){

if (!signIn_)

signIn_ = new SignIn(mainStack_);

mainStack*->setCurrentWidget(signIn*);

}

//SignIn.C

SignIn::SignIn(WContainerWidget *parent):

WContainerWidget(parent)

{

session_.login().changed().connect(this, &SignIn::onAuthEvent);

Auth::AuthModel *authModel = new Auth::AuthModel(Session::auth(), session_.users(), this);

authModel->addPasswordAuth(&Session::passwordAuth());

authModel->addOAuth(Session::oAuth());

Auth::AuthWidget *authWidget = new Auth::AuthWidget(session_.login());

authWidget->setModel(authModel);

authWidget->setRegistrationEnabled(true);

authWidget->processEnvironment();

this->addWidget(authWidget);

}

void SignIn::onAuthEvent()

{

if (session_.login().loggedIn()) {

const Wt::Auth::User& u = session_.login().user();

Wt::log("notice")

<< "User " << u.id()

<< \" (\" << u.identity(Wt::Auth::Identity::LoginName) << ")"

<< \" logged in.";

} else

Wt::log("notice") << "User logged out.\";

}

Please, any help will be appreciated.

Best regards,

Yosr