How to prevent gwt app to go to login page after page refresh? - gwt

I have a gwt app with a login page and a main page. After login app goes to main page. What i want is if i refresh the page to stay in main page and not going to login page. I have read many things and i tried History Mechanish but no result. Here is my code:
#Override
public void onSuccess(Login result) {
if (result.getLoginCount() == 1) {
final VerticalPanel userPanel = new VerticalPanel();
Anchor logout = new Anchor("logout");
logout.addStyleName("user");
logout.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
loginPanel.setVisible(true);
tablePanel.setVisible(false);
addPanel.setVisible(false);
userPanel.setVisible(false);
}
});
Label user = new Label("Hi " + usernameBox.getText());
userPanel.add(user);
user.addStyleName("user");
userPanel.add(logout);
userPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
userPanel.setVisible(true);
usernameBox.setText("");
passwordBox.setText("");
RootPanel.get("user").add(userPanel);
loginPanel.setVisible(false);
tablePanel.setVisible(true);
addPanel.setVisible(true);
History.newItem("main");
History.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
if(History.getToken().equals("main")){
loginPanel.setVisible(false);
tablePanel.setVisible(true);
addPanel.setVisible(true);
}
}
});
}
i also tried:
String historyToken = event.getValue();
if(historyToken.substring(0 , 4).equals("main")){
loginPanel.setVisible(false);
tablePanel.setVisible(true);
addPanel.setVisible(true);
} else {
loginPanel.setVisible(true);
tablePanel.setVisible(false);
addPanel.setVisible(false);
}
Is this the right way to handle page refresh with History.addValueChangeHandler? I would appreciate any help.

GWT application is a single page application. It means that if your reload page, the state of your application will be lost. What you can do, is to use local storage to store same state data, but that is not a good idea for an authentication data.
I recommend you to refactor your code in a way that the authentication is done against the back end and your GWT client will recover it's state from back end data when user refreshes the page.

Related

Codename one. Facebook login issues

I am having problem with facebook login in my cn1 app. I followed all the instructions given here: http://www.codenameone.com/facebook-login.html. I create a new instance of FacebookConnect then I set the clientId, secret etc. Then I set a LoginCallback and call doLogin(). However when I tried to run it on my device the screen just loads and it never stops. It doesn't even call LoginFailed. It just never stopped loading.
Here is my code:
faceBookButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
fb.setClientId("XXXXXXXXXXXXXXXX");
fb.setClientSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
fb.setRedirectURI("");
fb.setCallback(new LoginCallback(){
#Override
public void loginFailed(String errorMessage) {
Dialog dg = new Dialog();
dg.setDisposeWhenPointerOutOfBounds(true);
dg.setTitle("Login succeeded");
dg.show();
}
#Override
public void loginSuccessful() {
String token = fb.getAccessToken().getToken();
Dialog dg = new Dialog();
dg.setDisposeWhenPointerOutOfBounds(true);
dg.setTitle("Login succeeded");
dg.show();
}
});
fb.doLogin();
}
});
Check out the full guide on facebook login here.
On login success you need to show a new Form and not a Dialog as you are moving away from the login screen of your application. There is a full end to end sample in the chat tutorial.

Link to last version of stateful page

I have a number of stateful pages with some state for each page. For example each page has a form that was submitted.
How can I organize a menu with links to last versions of these stateful pages? Should I store anywhere (may be in the session) reference to appropriate object for each page? If I use
onClick() { setResponsePage(MyPage.class); }
than I lose the previous state of the page. I want to link to last state of the page.
Each time the page is rendered store the page's id in the session.
int pageId = pageInstance.getPageId();
A list or stack data structure could be used to hold the identifiers.
You can implement the navigation menu using a repeater (RepeatingView or such) that creates a new link for each page id in the session.
In the link's click handler you can redirect the user as follows:
Page pageInstance = (Page) new PageProvider(pageId, null).getPageInstance();
setResponsePage(pageInstance);
pageId = this.getPageId(); // get current version of lets say a HomePage
//OnClick for Link
public void onClick()
{
WebPage homePageInstance =
(WebPage) new PageProvider(HomePage.pageId, null).getPageInstance();
setResponsePage(homePageInstance);
}
});
I make it passing int previousId parameter via constructor :
#MountPath(value = "editSomethingPage")
public class TestPage extends WebPage {
int previousPageId = 0;
public TestPage(int previousPage) {
super();
this.previousPageId = previousPage;
}
#Override
protected void onInitialize() {
super.onInitialize();
add(new Link("goBack") {
#Override
public void onClick() {
if (previousPageId != 0) {
setResponsePage((Page) AuthenticatedWebSession.get().getPageManager().getPage(previousPageId));
} else {
setResponsePage(PreviousPage.class);
}
}
});
}
}
It prevents creation of new page instance and keeps all states of page made by user.

How to properly implement facebook login for GWT facebook app?

I am creating an app on Facebook and am trying to figure out what the proper way to authenticate and log into the app is. I don't want it to be accessible when they log out of Facebook and currently using OAuth 2.0 the app still totally functions by going to apps.facebook.com/myappname, even though the top bar to log into Facebook is there.
Here is my onModuleLoad :
public void onModuleLoad() {
AuthRequest req = new AuthRequest(FACEBOOK_AUTH_URL, FACEBOOK_CLIENT_ID);
AUTH.login(req, new Callback<String, Throwable>() {
#Override
public void onSuccess(String token) {
f_token = token;
startAppAfterLogin();
}
#Override public void onFailure(Throwable caught) {
Window.Location.assign("https://www.facebook.com/login.php");
}
});
}
Obviously the startAppAfterLogin() is still being called when they have logged out, anyone have any ideas what to do?

confusion in running small history GWT application

When I am running a small login application consisting of history management, it works fine at my home where im using latest chrome and firefox versions and GWT 2.4
The same application when I run at my office works wild. I have used a Global static boolean variable which has correct value in the debug mode while it has wrong value when I run it normally. In Office Im using IE 7 and GWT 2.2
Also, onModuleLoad() is called only once at my home environment whereas it is called everytime when I type someURL#sometoken and press enter to change the internal page. When is onModuleLoad() called. Only once per session or evrytime user loads some page (or even token)?
Can anyone tell is this some problem due to IE 7 or GWT 2.2 or some other issue.
EDIT - Its very small app. Code ---
TestHistory.java
public class TestHistory implements EntryPoint, ValueChangeHandler<String> {
static boolean isLoggedIn = false;
static final String PAGENAME = "mainscreen";
public void onModuleLoad()
{
History.addValueChangeHandler(this);
String startToken = History.getToken();
System.out.println("onModuleLoad Called..... start token= -------"+startToken+"--------");
if(startToken.isEmpty())
History.newItem("login");
else
History.fireCurrentHistoryState(); //to execute onValueChange 1st time since 1st time history is not setup
}
#Override
public void onValueChange(ValueChangeEvent<String> event) {
String token = event.getValue();
System.out.println("onValueChange called with token = ***"+token+"***");
String args = "";
int question = token.indexOf("?");
if (question != -1) {
args = token.substring(question + 1);
token = token.substring(0, question);
}
if(!isLoggedIn)
{
if(token.isEmpty() || "login".equals(token)) //1st time opened the site normally
new Login().display(false, RootPanel.get());
else {
new Login().display(true, RootPanel.get());
}
}
else //User has logged in
{
if(token.isEmpty() || "login".equals(token))
{
if(isLoggedIn)
Window.alert("Ur already logged in!!!");
else
new Login().display(false, RootPanel.get());
}
else if("withdraw".equals(token))
new Withdraw().display(RootPanel.get(), args);
else if("deposit".equals(token))
new Deposit().display(RootPanel.get(), args);
else //token not clear
Window.alert("Unrecognized token=" + token);
}
}
}
Login.java
public class Login {
static final String PAGENAME = "login";
void display(final boolean hasTypedSomeToken,final Panel myPanel) //Process login
{
System.out.println("login display called");
Label displayLabel = new Label("This is the Login Page");
Label enterName = new Label("Enter ur name");
final TextBox txtName = new TextBox();
Label enterPasswd = new Label("Enter ur Passwd");
final TextBox txtPasswd = new TextBox();
Button btnLogIn = new Button("Login", new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
/* Real app will check DB. Here we r jst chckng d txt fields hv value */
if(txtName.getValue().length()>0 && txtPasswd.getValue().length()>0)
{
TestHistory.isLoggedIn = true;
if(hasTypedSomeToken) {
System.out.println("adsljasdlfjljkfsd");
History.fireCurrentHistoryState();
System.out.println("hoolala "+History.getToken());
}
else
{
myPanel.clear();
Label displayLabel = new Label("Thank U for logging. U can now access the application.");
myPanel.add(displayLabel);
}
}
}
});
myPanel.clear();
myPanel.add(displayLabel);
myPanel.add(enterName);
myPanel.add(txtName);
myPanel.add(enterPasswd);
myPanel.add(txtPasswd);
myPanel.add(btnLogIn);
}
}
Deposit.java
public class Deposit {
static final String PAGENAME = "deposit";
void display(Panel myPanel, String param)
{
System.out.println("deposit display called");
myPanel.clear();
Label displayLabel = new Label("This is the Deposit Page & ur parameter = "+param+")");
myPanel.add(displayLabel);
}
}
Class Withdraw is same as Deposit.
The problem Im facing is that once Im logged in I should be able to open all the internal pages which works perfectly at my home (and onModuleLoad() is called just once) whereas I have to log in everytime to open a internal page at my office (and onModuleLoad() is called evrytime)
onModuleLoad is called when the page is loaded, but:
pressing the enter key while in the address bar can reload the page in some browsers
changing the hash in the URL from outside the application (typing in the address bar, or using a bookmark) can confuse IE6/7; when GWT detects it, it reloads the page (have a look inside the HistoryImplIE6 class). Note that it does not happen when navigating in the history (this is what the hidden iframe is for)
Did you included the hidden iframe for history support in gwt in your html host page?
See http://code.google.com/intl/de-DE/webtoolkit/doc/latest/DevGuideCodingBasicsHistory.html#mechanism

continueToOriginalDestination does not bring me back to originating page

I am trying to sign into my application. First I throw the RestartResponseAtInterceptPageException (this is in a WicketPanel on my BasePage):
add(new Link<String>("signin") {
#Override
public void onClick() {
throw new RestartResponseAtInterceptPageException(SignIn.class);
}
});
The SignIn Page class contains a form (an inner private class) for the sign in, with the following submit button:
add(new Button("signinButton") {
#Override
public void onSubmit() {
final User user = model.getObject();
final boolean result = MySession.get().authenticate(user);
if (result) {
if (!continueToOriginalDestination()) {
setResponsePage(MySession.get().getApplication().getHomePage());
}
} else {
error("Authentication failed");
}
}
});
When this button is clicked and the user is successfully authenticated, I am not redirected to the page where I clicked on the signIn link but instead I stay on the SignIn page? I've tried debugging this, but haven't been able to find out where things go wrong.
I am glad for any hints that lead to my finding the error of my ways.
This is wicket 1.5.1 by the way.
Small Update because I got the hint I needed from the answer, there is still a bit of explaining to do. The solution looks like this:
add(new Link<String>("signin") {
#Override
public void onClick() {
setResponsePage(new SignIn(getPage()));
}
});
The SignIn class gets a constructor that takes a page obviously and I simply set that page as with setResponsePage to return to where I started without all the continueToOriginalDestination and exception throwing.
RestartResponseAtInterceptPageException is meant to be used to redirect to an interception page while rendering a page. For example, in the constructor of a Page class ProtectedPage, if there is no user signed in, you throw new RestartResponseAtInterceptPageException(SignIn.class). When the SignIn page calls continueToOriginalDestination(), the user is taken back to the original ProtectedPage destination.
Your use is not a typical use of RestartResponseAtInterceptPageException since you throw it in a link handler. Why don't you do a setResponsePage(SignIn.class) directly instead? If you really want to return to the exact page you were on when the "signin" link is clicked, you could also try changing it to:
add(new Link<String>("signin") {
#Override
public void onClick() {
setResponsePage(getPage());
throw new RestartResponseAtInterceptPageException(SignIn.class);
}
});