setting loginRedirect

374 views
Skip to first unread message

therac25

unread,
Jan 7, 2011, 7:10:42 AM1/7/11
to Lift
Hi

I'm trying to set the URL a user gets redirected to when logging in.
By default loginRedirect is Empty:

object loginRedirect extends SessionVar[Box[String]](Empty)

In my User object I tried:

println(loginRedirect.is)
loginRedirect.set(Full("/user_home"))
println(loginRedirect.is)

the console shows:

Empty
Empty

so loginRedirect is still Empty.
What am I missing ?

Thanks

Emmanuel

David Pollak

unread,
Jan 7, 2011, 9:09:27 AM1/7/11
to lif...@googlegroups.com

You're making the call outside the scope of an HTTP request... there's no session, thus there's no place to put a SessionVar.
 

Thanks

Emmanuel

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics

therac25

unread,
Jan 7, 2011, 3:07:52 PM1/7/11
to Lift
Hi David

1) I was trying to override ProtoUser default behaviour so a user that
logs in is redirected to a specific page (rather than to the homepage)
How should I go about doing that ? Should I override the login
method ?

2) I was wondering, if I am outside the scope of an HTTP request
shouldn't .set() throw an error instead of failing silently ?

Thanks

Emmanuel



On Jan 8, 3:09 am, David Pollak <feeder.of.the.be...@gmail.com> wrote:
> On Fri, Jan 7, 2011 at 4:10 AM, therac25 <th3ra...@googlemail.com> wrote:
> > Hi
>
> > I'm trying to set the URL a user gets redirected to when logging in.
> > By default loginRedirect is Empty:
>
> >  object loginRedirect extends SessionVar[Box[String]](Empty)
>
> > In my User object I tried:
>
> >    println(loginRedirect.is)
> >    loginRedirect.set(Full("/user_home"))
> >    println(loginRedirect.is)
>
> > the console shows:
>
> > Empty
> > Empty
>
> > so loginRedirect is still Empty.
> > What am I missing ?
>
> You're making the call outside the scope of an HTTP request... there's no
> session, thus there's no place to put a SessionVar.
>
>
>
>
>
> > Thanks
>
> > Emmanuel
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Lift" group.
> > To post to this group, send email to lif...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > liftweb+u...@googlegroups.com<liftweb%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/liftweb?hl=en.
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net
> Beginning Scalahttp://www.apress.com/book/view/1430219890

David Pollak

unread,
Jan 7, 2011, 5:01:30 PM1/7/11
to lif...@googlegroups.com
On Fri, Jan 7, 2011 at 12:07 PM, therac25 <th3r...@googlemail.com> wrote:
Hi David

1) I was trying to override ProtoUser default behaviour so a user that
logs in is redirected to a specific page (rather than to the homepage)
How should I go about doing that ? Should I override the login
method ?


override def homePage = "/foo/bar"
 
2) I was wondering, if I am outside the scope of an HTTP request
shouldn't .set() throw an error instead of failing silently ?

To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

therac25

unread,
Jan 7, 2011, 7:38:40 PM1/7/11
to Lift
Ok, so homePage is really the user's homepage and not the site
homepage...

works great, thanks David

Emmanuel

On Jan 8, 11:01 am, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> On Fri, Jan 7, 2011 at 12:07 PM, therac25 <th3ra...@googlemail.com> wrote:
> > Hi David
>
> > 1) I was trying to override ProtoUser default behaviour so a user that
> > logs in is redirected to a specific page (rather than to the homepage)
> > How should I go about doing that ? Should I override the login
> > method ?
>
> override def homePage = "/foo/bar"
>
> > 2) I was wondering, if I am outside the scope of an HTTP request
> > shouldn't .set() throw an error instead of failing silently ?
>
> https://www.assembla.com/spaces/liftweb/tickets/830-liftrules-setting...
> > <liftweb%2Bunsu...@googlegroups.com<liftweb%252Bunsubscribe@googlegroup s.com>

Nuanda

unread,
Mar 5, 2011, 10:49:30 AM3/5/11
to lif...@googlegroups.com
Hi,

I'd like to dynamically redirect the user post-authentication to their originally sought URL (using Lift 2.2).  I've used some ideas from this thread and from an old thread:


The code I have used in my MetaMegaProtoUser is shown below.  It's working fine to capture the refererURL (into the loginRedirect SessionVar defined by ProtoUser) by overriding "login".  Sadly when I go to reference this Box in "homePage" (for user redirection) it is always Empty.  Is this because Lift is switching user Session contexts pre and post login event ?

Thanks!


   /** 
    * We tweak the user's post-login "homepage" and inject a once-off use of our recorded referer path 
    * so the auth'd user ends up where they were going. 
    */
   override def homePage = {
      val originPath = loginRedirect openOr "/"        //  <-- Problem, always empty !
      loginRedirect.remove()  // Clear after this once off usage in the auth lifecycle.
      logger.info("Indicating user homepage as [" + originPath + "]")
      originPath
   }

   /** 
    * Extend the login method to capture the user's origin URL for post-auth redirection (into a SessionVar for this user).
    */
   override def login = {
      for (refererPath <- S.referer if loginRedirect.isEmpty) {
         loginRedirect.set(Full(refererPath))
      }
      logger.info("Login referer path is [" + loginRedirect.is + "]")    // Captures nicely.
      super.login
   }
}

Peter Robinett

unread,
Mar 5, 2011, 2:11:46 PM3/5/11
to lif...@googlegroups.com
I actually just did this today. In my case, I'm using a user-provided callback URL, if there is one. I just copied and then edited the login method:

def login = {  
  // ...
          case Full(user) => logUserIn(user, () => {  
            S.notice(S.??("logged.in"))
            val redir = S.param("callback_url") match {
              case Full(url) => {
                loginRedirect(Empty)  
                url
              }
              case _ => homePage
            }
            S.redirectTo(redir)  
          })
          case _ => S.error(S.??("invalid.credentials"))
        }
      }
    }
  // ...
}

Note that my User model has a lot of code from ProtoUser but doesn't actually extend it, so your mileage may vary.

Peter

Jeppe Nejsum Madsen

unread,
Mar 7, 2011, 2:43:44 AM3/7/11
to lif...@googlegroups.com
Nuanda <davee...@gmail.com> writes:

> Hi,
>
> I'd like to dynamically redirect the user post-authentication to their
> originally sought URL (using Lift 2.2). I've used some ideas from this
> thread and from an old thread:

I think the proto user code (at least in 2.3) does this by default?

I know we implemented our own, but have now removed it...

/Jeppe

Naftoli Gugenheim

unread,
Mar 7, 2011, 4:21:00 PM3/7/11
to lif...@googlegroups.com
It used to but it broke. There was a thread discussing how to fix it.


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.

Jeppe Nejsum Madsen

unread,
Mar 8, 2011, 2:58:29 AM3/8/11
to lif...@googlegroups.com
Naftoli Gugenheim <nafto...@gmail.com> writes:

> It used to but it broke. There was a thread discussing how to fix it.

Hmm seems fixed now, works for me at least....

/Jeppe

Message has been deleted

Damian H

unread,
Apr 22, 2011, 12:01:48 PM4/22/11
to lif...@googlegroups.com
Hi Jeppe

>> I think the proto user code (at least in 2.3) does this by default?
I've just been playing around trying to work out what you meant by 'by default'.

One way I got it to work was by adding net.liftweb.protouser.loginFirst as a LocParam:

Menu(Loc("My Searches", List("My Searches") -> false, "My Searches",  User.loginFirst)),

However that didn't quite do what I wanted as it didn't show the My Searches item in the menu if the user wasn't logged in. I wanted the user to have the option to click on the My Searches even if he wasn't logged in but then be redirected to login page and then automatically onto the My Searches page after login.

So instead, in the My Searches snippet, I did something like:

User.currentUser.map({user =>
< code to do stuff .....>
}) openOr  { 
       val uri = S.uriAndQueryString
S.notice("Log in to see your searches")
S.redirectTo(User.loginPageURL, ()=>{User.loginRedirect.set(uri)} )
}

Is that how you do it?

Thanks

Damian

Jeppe Nejsum Madsen

unread,
Apr 26, 2011, 6:48:19 AM4/26/11
to lif...@googlegroups.com
Damian H <damia...@gmail.com> writes:

> Hi Jeppe
>
>>> I think the proto user code (at least in 2.3) does this by default?
> I've just been playing around trying to work out what you meant by 'by
> default'.
>

> I got it to work by adding net.liftweb.protouser.loginFirst as a LocParam:
>
> Menu(Loc("Save", List("save") -> false, "Save", User.loginFirst)),


>
> Is that how you do it?

Yes. But I don't have the need to display the menu items before login
which, as you found out, doesn't work here (unless you manually create
the links)

/Jeppe

Matthew Pocock

unread,
Apr 27, 2011, 10:03:58 AM4/27/11
to lif...@googlegroups.com
In my case, I want to allow users to browse my site, and at any time log in, at which point they do the password dance and then should be taken back to the page they where at beforehand, but now the page is rendered in 'logged in' mode. I'm not sure how to achieve this - right now logging in redirects my users to their personal home page.

Matthew

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Matthew Pocock
(0191) 2566550

Jeppe Nejsum Madsen

unread,
Apr 27, 2011, 3:58:07 PM4/27/11
to lif...@googlegroups.com
Matthew Pocock <turingate...@gmail.com> writes:

> In my case, I want to allow users to browse my site, and at any time log in,
> at which point they do the password dance and then should be taken back to
> the page they where at beforehand, but now the page is rendered in 'logged
> in' mode. I'm not sure how to achieve this - right now logging in redirects
> my users to their personal home page.

I see. I'm not sure how to accomplish this, but my gut feeling is to
keep the access control in the sitemap and change the default sitemap
rendering....

Not sure how involved it would be to create your own Menu snippet that
renders the sitemap even if you don't have access to the Locs (and if
the information is readily available)

/Jeppe

Naftoli Gugenheim

unread,
Apr 27, 2011, 10:38:19 PM4/27/11
to lif...@googlegroups.com
Is the issue redirecting back to the previous page, or showing different content depending on whether you're logged in? It sounds like two distinct issues.


/Jeppe

Matthew Pocock

unread,
Apr 28, 2011, 5:26:01 AM4/28/11
to lif...@googlegroups.com
Oh, I think I got the two mixed up somehow. I'm interested in having control over the redirecting, particularly in getting back to the page where the login was initiated from. I do also have login-dependent content, but I've got that covered.

M

Jeppe Nejsum Madsen

unread,
Apr 28, 2011, 5:44:52 AM4/28/11
to lif...@googlegroups.com
Yeah, I think there's some confusion here....

On Thu, Apr 28, 2011 at 11:26 AM, Matthew Pocock
<turingate...@gmail.com> wrote:
> Oh, I think I got the two mixed up somehow. I'm interested in having control
> over the redirecting, particularly in getting back to the page where the
> login was initiated from.

This should work out the box if you use SiteMap and ProtoUser.

You may not be able to render the menu items using the standard
snippets, but try to, without signing in, manually go to a URL that is
only visible to authorized users. This should take you to login and,
upon successful login, back to the requested page. At least this works
for me and I don't remember doing anything fancy.....

/Jeppe

Matthew Pocock

unread,
Apr 28, 2011, 6:09:37 AM4/28/11
to lif...@googlegroups.com
On 28 April 2011 10:44, Jeppe Nejsum Madsen <je...@ingolfs.dk> wrote:
Yeah, I think there's some confusion here....


My bad.
In my case I'm not putting some URLs behind passwords and having others public. All pages are visible to all users, but you can log in and then you may be allowed to edit some of the pages. If you aren't logged in (or logged in without privileges), then the editing UI isn't in the page you are looking at. If you are, then there are ajax forms and comet and things that manage editing.

M


/Jeppe

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Jeppe Nejsum Madsen

unread,
Apr 28, 2011, 8:00:28 AM4/28/11
to lif...@googlegroups.com
On Thu, Apr 28, 2011 at 12:09 PM, Matthew Pocock
<turingate...@gmail.com> wrote:
> On 28 April 2011 10:44, Jeppe Nejsum Madsen <je...@ingolfs.dk> wrote:
>>
>> Yeah, I think there's some confusion here....
>>
>
> My bad.
>
>>
>> On Thu, Apr 28, 2011 at 11:26 AM, Matthew Pocock
>> <turingate...@gmail.com> wrote:
>> > Oh, I think I got the two mixed up somehow. I'm interested in having
>> > control
>> > over the redirecting, particularly in getting back to the page where the
>> > login was initiated from.
>>
>> This should work out the box if you use SiteMap and ProtoUser.
>>
>> You may not be able to render the menu items using the standard
>> snippets, but try to, without signing in, manually go to a URL that is
>> only visible to authorized users. This should take you to login and,
>> upon successful login, back to the requested page. At least this works
>> for me and I don't remember doing anything fancy.....
>
> In my case I'm not putting some URLs behind passwords and having others
> public. All pages are visible to all users, but you can log in and then you
> may be allowed to edit some of the pages. If you aren't logged in (or logged
> in without privileges), then the editing UI isn't in the page you are
> looking at. If you are, then there are ajax forms and comet and things that
> manage editing.
> M

Ahh ok. Makes sense. I don't think this is supported ootb, see recent
threads an RBAC in Lift.

For now you can create a LoggedIn snippet which will render it's body
if you're logged in and return NodeSeq.Empty if your'e not. Wrap all
your editing content with this snippet and you should be good to go
:-)

/Jeppe

Matthew Pocock

unread,
Apr 28, 2011, 8:24:43 AM4/28/11
to lif...@googlegroups.com
> In my case I'm not putting some URLs behind passwords and having others
> public. All pages are visible to all users, but you can log in and then you
> may be allowed to edit some of the pages. If you aren't logged in (or logged
> in without privileges), then the editing UI isn't in the page you are
> looking at. If you are, then there are ajax forms and comet and things that
> manage editing.
> M

Ahh ok. Makes sense. I don't think this is supported ootb, see recent
threads an RBAC in Lift.

For now you can create a LoggedIn snippet which will render it's body
if you're logged in and return NodeSeq.Empty if your'e not. Wrap all
your editing content with this snippet and you should be good to go
:-)

Yup, I have a snippet that handles the conditional display of editing elements. What I'm failing to get working is that when the user clicks on the login link, after logging in they are then dumped back at the URL they clicked that link from.

M


/Jeppe

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Jeppe Nejsum Madsen

unread,
Apr 28, 2011, 9:17:07 AM4/28/11
to lif...@googlegroups.com
On Thu, Apr 28, 2011 at 2:24 PM, Matthew Pocock
<turingate...@gmail.com> wrote:
>> > In my case I'm not putting some URLs behind passwords and having others
>> > public. All pages are visible to all users, but you can log in and then
>> > you
>> > may be allowed to edit some of the pages. If you aren't logged in (or
>> > logged
>> > in without privileges), then the editing UI isn't in the page you are
>> > looking at. If you are, then there are ajax forms and comet and things
>> > that
>> > manage editing.
>> > M
>>
>> Ahh ok. Makes sense. I don't think this is supported ootb, see recent
>> threads an RBAC in Lift.
>>
>> For now you can create a LoggedIn snippet which will render it's body
>> if you're logged in and return NodeSeq.Empty if your'e not. Wrap all
>> your editing content with this snippet and you should be good to go
>> :-)
>
> Yup, I have a snippet that handles the conditional display of editing
> elements. What I'm failing to get working is that when the user clicks on
> the login link, after logging in they are then dumped back at the URL they
> clicked that link from.
> M


Ok , sorry for being dense. Should have followed the discussion more closely :-)

How about creating your own Ajax link with a function that does the
same thing as the loginFirst LocParam:

def loginFirst = If(
loggedIn_? _,
() => {
import net.liftweb.http.{RedirectWithState, RedirectState}
val uri = S.uriAndQueryString
RedirectWithState(
loginPageURL,
RedirectState( ()=>{loginRedirect.set(uri)})
)
}
)

Capture current location, redirect to login page, upon successful
redirect, set the loginRedirect session var....

/Jeppe

Reply all
Reply to author
Forward
0 new messages