Package io.conducive.client.ui

Source Code of io.conducive.client.ui.LoginPresenter$MyEventBinder

package io.conducive.client.ui;

import com.ekuefler.supereventbus.EventBus;
import com.ekuefler.supereventbus.EventRegistration;
import com.ekuefler.supereventbus.Subscribe;
import io.conducive.client.ui.events.ClickLoginEvent;
import io.conducive.client.ui.events.ClickRegisterEvent;
import io.conducive.client.ui.events.FailureEvent;
import io.conducive.client.ui.events.UserAuthorizedEvent;
import io.conducive.client.ui.views.LoginView;
import io.conducive.shared.model.User;
import io.conducive.client.http.UserAPI;
import io.conducive.client.http.glue.HttpCallback;

import javax.inject.Inject;

/**
* This pane handles presentation of login & registration. Works in combination with server side UserResource.
*
* Fires UserAuthorizedEvent once a user is (in fact) authorized.
*
* @author Reuben Firmin
*/
public class LoginPresenter extends AbstractPresenter {

    public interface MyEventBinder extends EventRegistration<LoginPresenter> {}

    private final UserAPI api;
    private final EventBus eventBus;

    @Inject
    public LoginPresenter(final EventBus eventBus, final LoginView loginView, final UserAPI userAPI, final MyEventBinder eventBinder) {
        this.eventBus = eventBus;
        eventBus.register(this, eventBinder);
        this.api = userAPI;
        initWidget(loginView);
    }

    @Subscribe
    void tryLogin(ClickLoginEvent event) {
        final User user = new User(event.getUsername());
        user.setHash(event.getPassword());
        user.setSalt(event.getSalt());
        api.login(user, new HttpCallback<Boolean>(eventBus, Boolean.class) {
            @Override
            public void onSuccess(Boolean result) {
                if (result) {
                    eventBus.post(new UserAuthorizedEvent(user, false));
                } else {
                    eventBus.post(new FailureEvent("Credentials were not recognized. Maybe you want to register?"));
                }
            }
        });
    }

    @Subscribe
    void tryRegister(ClickRegisterEvent event) {
        final User user = new User(event.getUsername());
        user.setHash(event.getPassword());
        user.setSalt(event.getSalt());
        api.register(user, new HttpCallback<Boolean>(eventBus, Boolean.class) {
            @Override
            public void onSuccess(Boolean result) {
                if (result) {
                    eventBus.post(new UserAuthorizedEvent(user, true));
                } else {
                    eventBus.post(new FailureEvent("Registration wasn't accepted. Try another username."));
                }
            }
        });
    }
}
TOP

Related Classes of io.conducive.client.ui.LoginPresenter$MyEventBinder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.