# Conditionally setting a cookie help

**URL:** <https://www.ruby-forum.com/t/conditionally-setting-a-cookie-help/242359>\
**Category:** NGINX\
**Created:** [June 18, 2015, 5:30pm UTC](https://www.ruby-forum.com/t/conditionally-setting-a-cookie-help/242359 "2015-06-18T17:30:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [June 18, 2015, 5:30pm UTC](https://www.ruby-forum.com/t/conditionally-setting-a-cookie-help/242359/1 "2015-06-18T17:30:10Z")

</div>

Hi All,

I’m having trouble setting a cookie conditionally based upon  
an upstream variable The hope is to cache an auth token in an  
encrypted session and only go to the backend auth token generator once.

I have something like this but it seems set-cookie happens no matter  
what,  
so I alternate between ‘my\_login=1848430=’ and ‘my\_login=’.

```
location = /auth {
    set_decode_base32 $b32 $cookie_my_login;
    set_decrypt_session $auth_tok $b32;

    if ($auth_tok != '') {
        return 200;
    }

    include fastcgi_params;
    fastcgi_pass unix:/tmp/fcgi_auth_tok_gen.sock;
}

location / {
    root /var/www;
    index index.html index.htm;

    auth_request /auth;
    auth_request_set $new_auth_tok $upstream_http_auth_tok;

    if ($new_auth_tok != false) {
        set_encrypt_session $enc_auth_tok $new_auth_tok;
        set_encode_base32 $b32 $enc_auth_tok;
        add_header Set-Cookie 'my_login=$b32';
    }
}

```

Ideas?
