Persistent cookies / "Remember me"

I know that the general purpose cookie needs are there, but the “remember me” checkbox for LoginForm would be generally useful, especially if it can be tweaked to remember just the username or both (for those who don’t need real security). In our case, we just want to remember the login name.

Is my best bet to just override LoginForm.getLoginHtml() to do this? I’d guess I’d add a checkbox with some javascript to write the cookie when checked and clear the cookie when unchecked. Is that the right approach? I’m using 6.3 nightly builds right now.

Here it is. To let everyone enjoy this, I attached the Vaadin add-on jar file that implements this feature with a callback API.

Just drop the jar into WEB-INF/lib and recompile the client-side widgetset (Vaadin plugin for Eclipse install a button on toolbar for this).

Here is a sample code:

// Label for cookie data
        final Label cookieTxt = new Label("<cookies not read>", Label.CONTENT_RAW);
        mainWindow.addComponent(cookieTxt);

        // BrowserCookies widget (by default reads the cookie values)
        final BrowserCookies cookies = new BrowserCookies();
        mainWindow.addComponent(cookies);

        // Listen when cookies are available and read them
        cookies.addListener(new BrowserCookies.UpdateListener() {

            public void cookiesUpdated(BrowserCookies bc) {
                String txt = "";
                for (String name : bc.getCookieNames()) {
                    txt += name + " = '" + bc.getCookie(name) + "'<br />";
                }
                cookieTxt.setValue(txt);
            }
        });

11210.jar (14.3 KB)

Hello,

I am using Vaadin 6.3.2 and BrowserCookies 1.0.2.

I set up completly new project and still example from the directory gives me empty site.

As I see (System.out.println) application is starting, on scripts on site starting but nothing happens, site is empty.

What is the problem??

Thanks,
Arthur.

After 2 wasted hours I discovered that disabling


private static final long serialVersionUID = -8873245242163572864L;

helps.

Also “cookies not read”


cookieTxt = new Label(“”, Label.CONTENT_RAW);

is threated as a html tag, and it is invisible.

But still main page is blank :(.


EDIT: OK, now it works. Instead of adding components to Panels, and Panels to mainWindow I added components to VerticalLayout and Horizontal layouts and then layouts to mainWindow.

You probably overcome all the problems already, but just a tip to everyone else testing with the simple cookie lister code: if it appears empty the reason might simply be that there are no cookies set in the browser. Try setting a cookie.

You mean helps on disabling the session serialization and reloads work? For this using the ‘restartApplication’ url parameter is a easier (and “the right”) way.

Ok, that makes sense although if everything works that should not even show. Changed that to the sample code.

Also, I now tested with Vaadin 6.3.3 and uploaded the new add-on version. So visit the Directory for samples/demos/download:


http://vaadin.com/directory#addon/6

Hi,

I think the biggest problem was that - I don’t know why (maybe I don’t know Vaadin so well) but - adding components to Panels generates blank page. I deleted panels, put VerticalPanel and HorizontalPanel indead and then I saw DateField for the first time. It should be there always, even when there are no cookies.

As I understand sample code (BrowserCookiesApplication) works for you so I wonder why panels are blank at my Firefox 3.6.3 (latest). There sould be no differences but…

(Now I see that you put ‘online demo’, the date picker (this small calendar) works now, it never shown in my modifed version.)

Hi, Sami! I use this widget, and… How I can save cookies manually? Can you implement this method? I would appreciate:)
Without this method I’m forced use cheats…

I’m sorry, now I don’t quite understand what you are looking for. Do you have already implemented that in some way? A piece of code might help. Thanks!

Hello!
I think that save cookies only on repaint - is no very good idea. In my task, I should save/remove a data (key and value) into cookies without repaint, and I need public methods save and restore cookies. Yes, it is not a bug, but it would convenient? how do you think?

and…
I have exception at cookies.put(cookie[0]
, cookie[1]
); without if(cookie.length >= 2) (with empty cookie value).
this is minor quick fix

@SuppressWarnings("unchecked")
    @Override
    public void changeVariables(Object source, Map variables) {
        super.changeVariables(source, variables);

        if (variables.containsKey("cookies")) {
            reload = false;
            cookies.clear();
            Object variable = variables.get("cookies");
            if (variable instanceof String[]) {
                String[] newCookies = (String[]
) variable;
                for (int i = 0; i < newCookies.length; i++) {
                    String[] cookie = newCookies[i]
.split(VALUE_SEPARATOR);
					if(cookie.length >= 2)						
						cookies.put(cookie[0]
, cookie[1]
);
					else
						cookies.remove(cookie[0]
);
                }
            }
            cookiesReadFromClient = true;
            fireCookiesUpdatedEvent();
        }

    }

perhaps it will be useful

Hi! Included this fix in the
1.0.4
.

Hi ,

nice add-on , what I need ,

in ur live demo , I set a cookie for 1 minute , but after 1 minute when refreshing the page it’s still there… ?!

Why I can’t edit my post? anyway I drop browsercookies-1.0.4 into lib dir , but when pressing “Compile Vaadin Widget” I get an error

“Select a widgetest file (…widgetest.gwt.xml) or a Vaadin project to compile.”

How I fix it? only drop the jar? or more things to do? in the live demo you configure into init-param something

and is it safe to save password into a cookie?

Hi! If you mean that the cookie contents is still listed in the demo, this is because the application session is still alive. Try to use the ?restartApplication parameter to reload the application and see what happens.

Hmm… Make sure you have activated the right project in the left pane. Sometimes the plugin does not find the project otherwise.

Well, no really. I mean cookies are readable in the browser and storing clear text passwords there would be easy to read there. However, other websites cannot read the cookies.

The safest way to make sure the plugin recognizes your project is to go in the Project Explorer view or the Package Explorer view and select you .gwt.xml file for your widgetset. The widgetset definition file is referenced by the WEB-INF/web.xml file – the location in web.xml should match your file.

Thanks for reply, although I found another way to use response/request using TFT add-on ( TPTApplication.getCurrentApplication () ) ,

I dont have .gwt.xml file either widgetset definition in web.xml , is both something I should have/create or it’s auto ?

Thanks for reply, although I found another way to use response/request using TFT add-on ( TPTApplication.getCurrentApplication () ) ,

I dont have .gwt.xml file either widgetset definition in web.xml , is both something I should have/create or it’s auto ?

Hi,

I’m using BrowserCookies v. 1.0.4, and I just noticed that the component doesn’t work for saving cookies unless I put it in my main window. I was trying to put the BrowserCookies component in my sub-window (actually, the login window), and I just can’t save a cookie (I can read them, but can’t add/update).

The problem is solved putting the component in my main window. It is not a big problem, but I think it would be nice to allow sub-windows to make their job without the blessings of the main one.

An other issue (here I go again, 90% of my posts are about that): the add-on is not 100% compatible with Google App Engine. The UpdateListener interface should extends Serializable, to allow clients to create anonymous listeners with no trouble.

For example, today GAE complains about this:


BrowserCookies cookies = new BrowserCookies();
cookies.addListener(new UpdateListener(){ //<-- not serializable!!!
@Override
public void cookiesUpdated(BrowserCookies browserCookies) {}
});

Instead, I have to create my own named class, make it implements UpdateListener and Serializable, and then use it:


private class BoringListener implements UpdateListener, Serializable {
@Override
public void cookiesUpdated(BrowserCookies browserCookies) {}
}
//...
BrowserCookies cookies = new BrowserCookies();
cookies.addListener(new BoringListener());

Hi,

I’m using BrowserCookies v. 1.0.4, and I just noticed that the component doesn’t work for saving cookies unless I put it in my main window. I was trying to put the BrowserCookies component in my sub-window (actually, the login window), and I just can’t save a cookie (I can read them, but can’t add/update).

The problem is solved putting the component in my main window. It is not a big problem, but I think it would be nice to allow sub-windows to make their job without the blessings of the main one.

An other issue (here I go again, 90% of my posts are about that =P): the add-on is not 100% compatible with Google App Engine. The UpdateListener interface should extends Serializable, to allow clients to create anonymous listeners with no trouble.

For example, today GAE complains about this:

BrowserCookies cookies = new BrowserCookies();
cookies.addListener(new UpdateListener(){ //<-- not serializable!!!
@Override
public void cookiesUpdated(BrowserCookies browserCookies) {}
});

Instead, I have to create my own named class, make it implements UpdateListener and Serializable, and then use it:

private class SerializableUpdateListener implements UpdateListener, Serializable {
@Override
public void cookiesUpdated(BrowserCookies browserCookies) {}
}
//...
BrowserCookies cookies = new BrowserCookies();
cookies.addListener(new SerializableUpdateListener());

Again, not a big problem, just an improvement that can be done. :wink:

Thanks.

Hmm. Actually, I cannot think why it does not work in subwindow as long as it added to the component hierarchy.
Overall, it is a little bit counterintuitive that components like this are added at all, but that is just how Vaadin works.

Right. This is a valid complaint :slight_smile: It should be an easy fix, and I try to find the time slot to fix that.