www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to return user name from vibed session?

reply Suliman <evermind live.ru> writes:
Vibed have method get for user session 
http://vibed.org/api/vibe.http.session/SessionStore

I set user name for session like this:
req.session.set("username", "admin");

But I can't understand how to get user name from it:

abstract std.variant.VariantN!(20) get(
   string id,
   string name,
   lazy std.variant.VariantN!(20) defaultVal
);

What does every string here mean? Could anybody to show example 
of usage?
Dec 10 2015
parent reply Adrian Matoga <epi atari8.info> writes:
On Thursday, 10 December 2015 at 11:36:20 UTC, Suliman wrote:
 Vibed have method get for user session 
 http://vibed.org/api/vibe.http.session/SessionStore

 I set user name for session like this:
 req.session.set("username", "admin");

 But I can't understand how to get user name from it:

 abstract std.variant.VariantN!(20) get(
   string id,
   string name,
   lazy std.variant.VariantN!(20) defaultVal
 );

 What does every string here mean? Could anybody to show example 
 of usage?
I think what you need is Session.get, not SessionStore.get: http://vibed.org/api/vibe.http.session/Session.get
Dec 10 2015
parent reply Suliman <evermind live.ru> writes:
On Thursday, 10 December 2015 at 12:21:40 UTC, Adrian Matoga 
wrote:
 On Thursday, 10 December 2015 at 11:36:20 UTC, Suliman wrote:
 Vibed have method get for user session 
 http://vibed.org/api/vibe.http.session/SessionStore

 I set user name for session like this:
 req.session.set("username", "admin");

 But I can't understand how to get user name from it:

 abstract std.variant.VariantN!(20) get(
   string id,
   string name,
   lazy std.variant.VariantN!(20) defaultVal
 );

 What does every string here mean? Could anybody to show 
 example of usage?
I think what you need is Session.get, not SessionStore.get: http://vibed.org/api/vibe.http.session/Session.get
Give me example please
Dec 10 2015
parent reply Suliman <evermind live.ru> writes:
On Thursday, 10 December 2015 at 12:30:51 UTC, Suliman wrote:
 On Thursday, 10 December 2015 at 12:21:40 UTC, Adrian Matoga 
 wrote:
 On Thursday, 10 December 2015 at 11:36:20 UTC, Suliman wrote:
 Vibed have method get for user session 
 http://vibed.org/api/vibe.http.session/SessionStore

 I set user name for session like this:
 req.session.set("username", "admin");

 But I can't understand how to get user name from it:

 abstract std.variant.VariantN!(20) get(
   string id,
   string name,
   lazy std.variant.VariantN!(20) defaultVal
 );

 What does every string here mean? Could anybody to show 
 example of usage?
I think what you need is Session.get, not SessionStore.get: http://vibed.org/api/vibe.http.session/Session.get
Give me example please
I found example in vibenews: writeln("USER Session: ", req.session.get!string("username")); but I can't understand why I should call it like this, but not like writeln("USER Session: ", req.session.get("username")); And it's look like it do not return username, or user name are set incorrectly: if(dbuser == "admin") // admin name hardcoded { _auth.isAuthorizated = true; req.session.set("username", "admin"); } else { req.session.set(request["username"].to!string, dbuser); //set current username in parameter of session name writeln("user name is: ", dbuser); }
Dec 10 2015
parent reply Suliman <evermind live.ru> writes:
Oh, my issue. Right variant of setting session is:

     if(dbuser == "admin") // admin name hardcoded
     {
        _auth.isAuthorizated = true;
        req.session.set("username", "admin");
     }
     else
     {
         req.session.set("username", dbuser); //set current 
username in parameter of session name
         writeln("username from req: ", 
request["username"].to!string);
         writeln("user name is: ", dbuser);
         writeln(request["username"].to!string, " == ", dbuser);
     }

But question about why I need to get session info like:

writeln("USER Session: ", req.session.get!string("username"));

is still actual.
Dec 10 2015
parent reply Mike Parker <aldacron gmail.com> writes:
On Thursday, 10 December 2015 at 13:23:29 UTC, Suliman wrote:

 But question about why I need to get session info like:

 writeln("USER Session: ", req.session.get!string("username"));

 is still actual.
When you have a template that looks like this: V get(V, K)(K key) {...} The compiler is able to deduce the type of K because of the argument 'key'. However, there's no way for it to know what V should be. You have to tell it explicitly when you instantiate it: auto str = get!string("key1"); auto i = get!int("key2");
Dec 10 2015
parent reply Suliman <evermind live.ru> writes:
On Thursday, 10 December 2015 at 13:34:02 UTC, Mike Parker wrote:
 On Thursday, 10 December 2015 at 13:23:29 UTC, Suliman wrote:

 But question about why I need to get session info like:

 writeln("USER Session: ", req.session.get!string("username"));

 is still actual.
When you have a template that looks like this: V get(V, K)(K key) {...} The compiler is able to deduce the type of K because of the argument 'key'. However, there's no way for it to know what V should be. You have to tell it explicitly when you instantiate it: auto str = get!string("key1"); auto i = get!int("key2");
So why for set both variant are work? req.session.set("username", "admin"); req.session.set!string("username", "admin");
Dec 10 2015
parent reply Suliman <evermind live.ru> writes:
On Thursday, 10 December 2015 at 13:43:58 UTC, Suliman wrote:
 On Thursday, 10 December 2015 at 13:34:02 UTC, Mike Parker 
 wrote:
 On Thursday, 10 December 2015 at 13:23:29 UTC, Suliman wrote:

 But question about why I need to get session info like:

 writeln("USER Session: ", req.session.get!string("username"));

 is still actual.
When you have a template that looks like this: V get(V, K)(K key) {...} The compiler is able to deduce the type of K because of the argument 'key'. However, there's no way for it to know what V should be. You have to tell it explicitly when you instantiate it: auto str = get!string("key1"); auto i = get!int("key2");
because set return void, and get return T?
Dec 10 2015
parent Mike Parker <aldacron gmail.com> writes:
On Thursday, 10 December 2015 at 13:46:19 UTC, Suliman wrote:

 because set return void, and get return T?
No, it has nothing to do with the return type declarations. It's about whether or not the compiler has enough information to deduce the types. V get(V, K)(K key, V defaultVal); auto i = 10; i = get("key", i); This works because the compiler sees that you have given it a string where it expects a K and an int where it expects a V, so it knows that K = string and V = int. On the other hand: V get(V, K)(K key); auto i = get("key"); What type is this supposed to return? Even if you do this: int i = get("key"); Obviously, the compiler knows that i is int, but that plays no role in template type deduction. Only the template declaration and instantiation do. So we have to explicitly tell the compiler what type V should be in this case by passing the type as a template argument. auto i = get!int("key");
Dec 10 2015