Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use server.decorate() instead of assignment #104

Closed
leesei opened this issue Jan 8, 2016 · 8 comments · Fixed by #107
Closed

Use server.decorate() instead of assignment #104

leesei opened this issue Jan 8, 2016 · 8 comments · Fixed by #107
Assignees

Comments

@leesei
Copy link

leesei commented Jan 8, 2016

See Eran's comment:
#103 (comment)

@jaw187
Copy link
Contributor

jaw187 commented Jan 8, 2016

Thanks @leesei I've seen it and have the work done locally.

@leesei
Copy link
Author

leesei commented Jan 11, 2016

Thanks.
Close this upon commit?

@jaw187
Copy link
Contributor

jaw187 commented Jan 11, 2016

👍 Struggling with how to access reply from within a function passed to server.decorate. Any suggestion?

@mtharrison
Copy link
Contributor

@jaw187 what are you trying to do, why do you need reply()? Can you show some code?

@jaw187
Copy link
Contributor

jaw187 commented Jan 14, 2016

@mtharrison All of the code I'm assigning to request.cookieAuth needs to be done using server.decorate, but since server.decorate has to be called before the extension point I'll only have access to request by setting the apply option to server.decorate

    server.ext('onPreAuth', (request, reply) => {

        request.cookieAuth = {
            set: function (session, value) {

                if (arguments.length > 1) {
                    const key = session;
                    Hoek.assert(key && typeof key === 'string', 'Invalid session key');
                    session = request.auth.artifacts;
                    Hoek.assert(session, 'No active session to apply key to');

                    session[key] = value;
                    return reply.state(settings.cookie, session);
                }

                Hoek.assert(session && typeof session === 'object', 'Invalid session');
                request.auth.artifacts = session;
                reply.state(settings.cookie, session);
            },
            clear: function (key) {

                if (arguments.length) {
                    Hoek.assert(key && typeof key === 'string', 'Invalid session key');
                    const session = request.auth.artifacts;
                    Hoek.assert(session, 'No active session to clear key from');
                    delete session[key];
                    return reply.state(settings.cookie, session);
                }

                request.auth.artifacts = null;
                reply.unstate(settings.cookie);
            },
            ttl: function (msecs) {

                const session = request.auth.artifacts;
                Hoek.assert(session, 'No active session to modify ttl on');
                reply.state(settings.cookie, session, { ttl: msecs });
            }
        };

        return reply.continue();
    });

I wrote some code where I included a function to register reply, but I'm not sure how that holds up with concurrent requests.

@hueniverse
Copy link
Contributor

You just need to decorate once and then you can override the value later. The point is to register to avoid namepsace issues.

@jaw187
Copy link
Contributor

jaw187 commented Jan 18, 2016

Ok, great!

@jaw187 jaw187 self-assigned this Jan 18, 2016
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants